Dynamics 365 – Get Entity Metadata using C#

4 Comments

In Dynamics 365, we can retrieve metadata from the platform through code. In this example, we will get information regarding an entity and its attributes.

First, create a new C# console app.

Add assemblies:

  • Microsoft.Xrm.Sdk;
  • Microsoft.Xrm.Tooling.Connector;

Now, add the Using statements:

using Microsoft.Xrm.Tooling.Connector;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Messages;
using Microsoft.Xrm.Sdk.Metadata;

We will first connect to Dynamics 365 to get the IOrganizationService:

var connectionString = @"AuthType = Office365; Url = https://yourcrm.crm.dynamics.com/;Username=yourusername;Password=yourpassword";
CrmServiceClient conn = new CrmServiceClient(connectionString);

IOrganizationService service;
service = (IOrganizationService)conn.OrganizationWebProxyClient != null ? (IOrganizationService)conn.OrganizationWebProxyClient : (IOrganizationService)conn.OrganizationServiceProxy;

Next, we will use RetrieveEntityRequest that contains the data needed to retrieve entity metadata. In this example, we will retrieve metadata for the account entity by setting the LogicalName. Note the different options available for what is returned through the EntityFilters, including:

  • All
  • Attributes
  • Default
  • Entity
  • Privileges
  • Relationships

In this example, we will retrieve some data about the account entity and then all its attributes. Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Microsoft.Xrm.Tooling.Connector;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Messages;
using Microsoft.Xrm.Sdk.Metadata;

namespace Carl.CrmMetadata
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                var connectionString = @"AuthType = Office365; 
                Url = https://yourcrm.crm.dynamics.com/;Username=yourusername;Password=yourpassword";
                CrmServiceClient conn = new CrmServiceClient(connectionString);

                IOrganizationService service;
                service = (IOrganizationService)conn.OrganizationWebProxyClient != null ? (IOrganizationService)conn.OrganizationWebProxyClient : (IOrganizationService)conn.OrganizationServiceProxy;

                RetrieveEntityRequest retrieveEntityRequest = new RetrieveEntityRequest
                {
                    EntityFilters = EntityFilters.All,
                    LogicalName = "account"
                };
                RetrieveEntityResponse retrieveAccountEntityResponse = (RetrieveEntityResponse)service.Execute(retrieveEntityRequest);
                EntityMetadata AccountEntity = retrieveAccountEntityResponse.EntityMetadata;

                Console.WriteLine("Account entity metadata:");
                Console.WriteLine(AccountEntity.SchemaName);
                Console.WriteLine(AccountEntity.DisplayName.UserLocalizedLabel.Label);
                Console.WriteLine(AccountEntity.EntityColor);

                Console.WriteLine("Account entity attributes:");
                foreach(object attribute in AccountEntity.Attributes)
                {
                    AttributeMetadata a = (AttributeMetadata)attribute;
                    Console.WriteLine(a.LogicalName);
                }
                Console.ReadLine();
            }
            catch (Exception ex)
            { }
        }
    }
}

When we run this, we get:

 

THANKS FOR READING. BEFORE YOU LEAVE, I NEED YOUR HELP.
 

I AM SPENDING MORE TIME THESE DAYS CREATING YOUTUBE VIDEOS TO HELP PEOPLE LEARN THE MICROSOFT POWER PLATFORM.

IF YOU WOULD LIKE TO SEE HOW I BUILD APPS, OR FIND SOMETHING USEFUL READING MY BLOG, I WOULD REALLY APPRECIATE YOU SUBSCRIBING TO MY YOUTUBE CHANNEL.

THANK YOU, AND LET'S KEEP LEARNING TOGETHER.

CARL

https://www.youtube.com/carldesouza

 

ABOUT CARL DE SOUZA

Carl de Souza is a developer and architect focusing on Microsoft Dynamics 365, Power BI, Azure, and AI.

carldesouza.comLinkedIn Twitter | YouTube

 

4 Responses to Dynamics 365 – Get Entity Metadata using C#

    • Koustav
      Replace this line
      Console.WriteLine(a.LogicalName);
      with this:
      Console.WriteLine(a.DisplayName);

  1. Thank you. We are here because I want to generate “create table” SQL from entity metadata. We are on GCC and want to migrate from Scribe and SSIS.

Leave a Reply

Your email address will not be published. Required fields are marked *