Friday, July 9, 2010

Retrieve CRM Entity's Picklist using MetadataService

This method returns a NameValueCollection of picklist items. CrmHelper is similar to the SDK utility sample.




public NameValueCollection GetAgencyRelationshipTypePicklist()
{
CrmHelper helper = new CrmHelper();

NameValueCollection picklist = new NameValueCollection();

RetrieveAttributeRequest attribReq = new RetrieveAttributeRequest();
attribReq.EntityLogicalName = "new_agencycontact";
attribReq.LogicalName = "new_agencycontacttype";
attribReq.RetrieveAsIfPublished = true;

MetadataService service = helper.GetMetadataService();

RetrieveAttributeResponse attribResp = service.Execute(attribReq) as RetrieveAttributeResponse;

PicklistAttributeMetadata listData = attribResp.AttributeMetadata as PicklistAttributeMetadata;

foreach (Option item in listData.Options)
{
picklist.Add(item.Value.formattedvalue, item.Label.UserLocLabel.Label);
}

return picklist;
}

No comments: