Dynamics 365 – Get Entity Metadata using C#

Leave a comment

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:

 

Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedIn

Leave a Reply

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

10 − 6 =