Home › Dynamics 365 Customer Engagement (CRM) › Dynamics 365 – Get Entity Metadata using C#
Dynamics 365 Customer Engagement (CRM) · Jan 23, 2018

Dynamics 365 – Get Entity Metadata using C#

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:

[sourcecode language=”CSharp”]
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)
{ }
}
}
}
[/sourcecode]

When we run this, we get:

 

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.

  2. Hi Carl,
    thanx, good job!
    In case of “.All” it should be enough to request only the “.Attributes” ?
    Best Robert

Leave a Reply

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

Carl de Souza
KEEP LEARNING. KEEP BUILDING.

Explore AI, agents & Microsoft technology.

I share practical ideas, tutorials, and videos about AI, AI agents, Microsoft technologies, and the Power Platform.