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:
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
Can U Tell me How to get all field Display name like this?
Koustav
Replace this line
Console.WriteLine(a.LogicalName);
with this:
Console.WriteLine(a.DisplayName);
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.
Hi Carl,
thanx, good job!
In case of “.All” it should be enough to request only the “.Attributes” ?
Best Robert