Michael Philip's Blog

Writing about MS Tech with strong focus on Azure & CRM

How To : Get Metadata for Status Code / State Code Mapping In Microsoft Dynamics CRM 2011

Leave a comment

Code:

//Create the request and lets say you wanna retrieve  it from the “incident” entity

RetrieveAttributeRequest attributeRequest = new RetrieveAttributeRequest
{
EntityLogicalName = “incident”,
LogicalName = “statuscode”,
RetrieveAsIfPublished = true
};

//Get the response
RetrieveAttributeResponse attributeResponse = (RetrieveAttributeResponse)_serviceproxy.Execute(attributeRequest);

// Cast the response to attribute meta data
AttributeMetadata attrMetadata = (AttributeMetadata)attributeResponse.AttributeMetadata;

// Cast AttributeMetadata to StatusAttributeMetadata
StatusAttributeMetadata statusMetadata = (StatusAttributeMetadata)attrMetadata;

//Create Dictionnary
var dict = new Dictionary<int?, string>();

//Loop through each option and get value & label
foreach (OptionMetadata optionMeta in statusMetadata.OptionSet.Options)
{
dict.Add(optionMeta.Value, optionMeta.Label.UserLocalizedLabel.Label);

//To get  the mapping  state code

int stateOptionValue = (int)((StatusOptionMetadata)optionMeta).State;

}

Note: you can check how to create Service Proxy at the following link
https://crmprof.wordpress.com/2013/03/13/creating-organization-service-proxy-in-microsoft-dynamics-crm-2011/

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s