Accessing MetaData with the Dynamics Web Api

In the previous Dynamics365 SDK, accessing metadata required that you go through a completely different service to get at the structure of your data.  With the WebApi, this has changed to simply be another querystring string call to get at your information.

Ignoring how to setup a connection, the query can be as simple as this;

string dataResponse = DynamicsHelper.Instance.GetFromDynamics("/api/data/v9.0/EntityDefinitions?$select=MetadataId,SchemaName");

Where my request for the name and id of all entities in the system is sent via EntityDefinitions.

At this stage in your query, you cannot filter out which entities you want to query for due to the fact that functions such as “contains” are not supported in this interface, thereby forcing you to do this client-side instead of during your trip to the server.

In my scenario I only wanted to return entity schema that was related to the Unified Service Desk, when executed I was able to output as follows.

metadata.PNG

The code to filter and iterate through being pretty simple.

if (results["value"].HasValues)
{
foreach (var res in results["value"])
{
if (res["SchemaName"].ToString().Contains("UII_") || res["SchemaName"].ToString().Contains("msdyusd_"))
{
MetaEntity entity = res.ToObject<MetaEntity>();
//Console.WriteLine(res["MetadataId"].ToString());
//Console.WriteLine(res["SchemaName"].ToString());

QueryForData("Data: " + entity.SchemaName);
Console.WriteLine(">>> " + entity.SchemaName.ToString());
entities.Add(entity);
}
}
}

Where my MetaEntity class looks like this;

class MetaEntity
{
public Guid MetaDataId { get; set; }
public string SchemaName { get; set; }

}

If you want to dig deeper into the entities themselves (to retrieve attributes), this can be done relatively easily by using the attributes extension to retrieve that data.

string dataResponse = DynamicsHelper.Instance.GetFromDynamics("/api/data/v9.0/EntityDefinitions(LogicalName='account')/Attributes?$select=LogicalName");

And then once again, iterating through those records to decide what you want to do with them.

Post A Reply

%d bloggers like this: