Monday, December 10, 2007

How to get picklist values for a specific entity

Have you ever needed to get Entity information from CRM 3.0.
I needed to import data into CRM 3.0 some of the data needed to be imported into picklist fields.

Essentially what you need to do is:
1) connect to the metadata service.
2) request the entity metadata.

The following block of code calls the RetrieveEntityMetadata method.
passing the entity schema name and the EntityFlags enum.
The code than iterates through all the attributes and inserts them into a HashTable.

EntityFlags.EntityOnly; (gets only entity info)
EntityFlags.IncludeAttributes; (includes the attributes)
EntityFlags.IncludePrivileges; (includes security)
EntityFlags.IncludeRelationships; (includes relationship)

Hashtable _h = new Hashtable();
MetadataService s = new MetadataService();
s.Credentials = System.Net.CredentialCache.DefaultCredentials;

EntityMetadata em = s.RetrieveEntityMetadata("contact", EntityFlags.IncludeAttributes);

foreach (AttributeMetadata am in em.Attributes)
    _h.Add(am.Name, am);

Each attribute has it's own data type class that is inherits the AttributeMetaData base class.

To retrieve the picklist I needed to convert the attribute property of the entity to a PicklistAttributeMetadata.

The PickListAttributeMetaData class has an array property called options.

for (int i = 0; i < picklistData.Options.Length; i++)
     if (picklistData.Options[i].Description == displayValue)
          // Value has been found!!!
          // optionValue = picklistData.Options[i].OptionValue;

Oshri Cohen


Post a Comment

Subscribe to Post Comments [Atom]

Links to this post:

Create a Link

<< Home