Feature Post


MS CRM: Enumerate or loop through Picklist

Following is one way of enumerating the Picklist items to find match on Picklist.Name public property.

The picklist values are in the retrievedAttributeMetadata.Options collection. To do this we will need to get the attribute metadata from the meta dataservice. This is a four step process.

STEP 1: Authenticate
STEP 2: Connect to CRM webservice
STEP 3: Retrieve
STEP 4: Iterate

Following is a snippet that would help get started.
// Create an authentication token.
CrmAuthenticationToken token = new CrmAuthenticationToken();
token.OrganizationName = "AdventureWorksCycle";

// You can use enums.cs from the SDK\Helpers folder to get the enumeration for AD Authentication.
token.AuthenticationType = 0;

//Create the metadata Web service;
MetadataService service = new MetadataService();
service.Url = "http://:/MSCRMServices/2007/MetadataService.asmx";
service.CrmAuthenticationTokenValue = token;
service.Credentials = System.Net.CredentialCache.DefaultCredentials;
service.PreAuthenticate = true;

// Create the request
RetrieveAttributeRequest attributeRequest = new RetrieveAttributeRequest();
attributeRequest.EntityLogicalName = EntityName.contact.ToString();
attributeRequest.LogicalName = "prefferedmethodcontactcode";
attributeRequest.RetrieveAsIfPublished = true;

RetrieveAttributeResponse attributeResponse = (RetrieveAttributeResponse)metadataService.Execute(attributeRequest);

// Access the retrieved attribute
PicklistAttributeMetadata retrievedAttributeMetadata = (PicklistAttributeMetadata)attributeResponse.AttributeMetadata;

foreach (var item in retrievedAttributeMetadata .Options)

if (contact.PreferredContactMethod.ContactMethod.ToString() == item.Label.UserLocLabel.Label.ToString())
picklist.Value = item.Value.Value;

if (!picklist.IsNull)
crmContactEntity.preferredcontactmethodcode = picklist;

Happy CRMing.