Entity & attribute metadata using c#
Below example to get Attribute Metadata using C# for a specific entity.
/*Connection establishing using ClientID & Client Secret*/
public static void GetConnection()
{
try
{
string connectionString = "AuthType=ClientSecret; url=REPLACE WITH ORANIZATION URL; ClientId=REPLACE WITH YOUR CLIENT ID; ClientSecret=REPLACE WITH YOUR CLIENT SECRET";
crmServiceClient = new CrmServiceClient(connectionString); //Connecting to the D-365 CDS instance
if (crmServiceClient != null && crmServiceClient.IsReady)
{
Console.WriteLine("\nConnection successful.");
}
else
{
Console.WriteLine("\nPlease make sure the Connection String is correct.");
}
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
private static void GetEntityMetadata(string entityName)
{
RetrieveEntityRequest req = new RetrieveEntityRequest()
{
EntityFilters = EntityFilters.Attributes,
LogicalName = entityName,
RetrieveAsIfPublished = true
};
RetrieveEntityResponse res = (RetrieveEntityResponse)organizationService.Execute(req);
foreach (var item in res.Results)
{
var attributeCollection = (item.Value as EntityMetadata).Attributes;
foreach (var attribute in attributeCollection)
{
var displayName = (attribute.DisplayName != null && attribute.DisplayName.UserLocalizedLabel != null) ? attribute.DisplayName.UserLocalizedLabel.Label : string.Empty;
string lookupAttributeInfo = string.Empty;
if (displayName == string.Empty)
{
displayName = "*********";
}
if (attribute.AttributeType.ToString() == "Lookup")
{
lookupAttributeInfo = (attribute as LookupAttributeMetadata).Targets[0];
}
if (lookupAttributeInfo != string.Empty)
{
Console.WriteLine($"Display Name:{displayName} -->LogicalName:{attribute.LogicalName} -->Target Entity Name:{lookupAttributeInfo}-->IsPrimaryName:{attribute.IsPrimaryName}");
}
else
{
Console.WriteLine($"Display Name:{displayName} -->LogicalName:{attribute.LogicalName} -->IsPrimaryName:{attribute.IsPrimaryName}");
}
}
}
}
static void Main(string[] args)
{
GetConnection();
GetEntityMetadata("account");
}
Comments
Post a Comment