Recently, I need to fetch the optionset values of one of the custom entities named as ‘Candidate’. It has one a custom attribute like job type. The job type is having values like Permanent, Temporary, Part time, on contract. I developed a Silverlight 4 data grid control to show the all candidates with some search criteria. By design, using OData service reference you can’t fetch the metadata of CRM entity. So here only get the related values of the optionset like 1 for Permanent, 2 for Temporary and so on. So, if you would like to show the label you have to choose the SOAP endpoint service reference.
Now, to start with SOAP endpoint development I referred a very useful link of MSDN. I am sharing the same here,
http://msdn.microsoft.com/en-us/library/gg594452.aspx
With the help of this link you can setup the required service reference and the utility classes to use messages or methods of CRM SDK.
Below is the code snippet which I have used to fetch the labels using SOAP endpoints in my candidates.xaml.cs file. You can use the same code by just passing the entity and attribute names. I hope this will be helpful.
1: /// <summary>
2: /// Get options set labels for specified entity's optionset type attribute
3: /// </summary>
4: /// <param name="entityName"></param>
5: /// <param name="attributeName"></param>
6: private void GetOptionSetLabels(string entityName, string attributeName)
7: {
8: OrganizationRequest request = new OrganizationRequest();
9: request.RequestName = "RetrieveAttribute";
10: request["EntityLogicalName"] = entityName;
11: request["LogicalName"] = attributeName;
12: request["MetadataId"] = Guid.Empty;
13: request["RetrieveAsIfPublished"] = true;
14:
15: IOrganizationService service = SilverlightUtility.GetSoapService();
16: service.BeginExecute(request, new AsyncCallback(OnGetOptionSetLabelsComplete), service);
17: }
18:
19:
20:
21: /// <summary>
22: /// Retrieve the resutls
23: /// </summary>
24: /// <param name="result"></param>
25: private void OnGetOptionSetLabelsComplete(IAsyncResult result)
26: {
27: //Get the original query back from the result.
28: OrganizationResponse response = ((IOrganizationService)result.AsyncState).EndExecute(result);
29:
30: if (response != null && response.Results.Count > 0)
31: {
32:
33: //Get the actual optionset meta data
34: OptionSetMetadata optionSetMetadata = ((EnumAttributeMetadata)(response.Results[0].Value)).OptionSet;
35:
36: //Set the metadata object in required field
37: Common.JobTypeOptionSetMetadata = optionSetMetadata;
38: }
39: }
Hi Ankit,
ReplyDeleteI am trying to get lable of optionset value using above code but i'm getting error "System.InvalidCastException: Unable to cast object of type 'CRMOrganisationService.OptionSetValue' to type 'CRMOrganisationService.EnumAttributeMetadata'"
I'm following the same way as you mention in code.Can u tell me how i cast this.
Regards,
Mahesh
Hi Mahesh,
DeletePlease share your code.
Hi Ankit,
ReplyDeleteSee the following code
//Call Back Function
public void ContactIntrestedStock(IAsyncResult result)
{
OrganizationResponse responce = ((IOrganizationService)result.AsyncState).EndExecute(result);
EntityCollection results = (EntityCollection)responce["EntityCollection"];
foreach (Entity oEntity in results.Entities)
{
OptionSetMetadata optionsetmetadata = (EnumAttributeMetadata)(oEntity.Attributes[0].Value)).OptionSet;
}
}
The function 'ContactIntrestedStock' is call back function in which i'm trying to get optionset lable.
'OptionSetMetadata optionsetmetadata = (EnumAttributeMetadata)(oEntity.Attributes[0].Value)).OptionSet'
at this line i'm getting error.
Ragards,
Mahesh
Hi Mahesh,
DeleteInstead of getting out the EntityCollection from the response,
OptionSetMetadata optionsetmetadata = (EnumAttributeMetadata)(oEntity.Attributes[0].Value)).OptionSet;
use
((EnumAttributeMetadata)(response.Results[0].Value)).OptionSet;
Because it won't convert the value of entity.attribute[0] in EnumAttributeMetadata. It will convert the value response.result[0] in EnumAttributeMetadata
I hope it is clear now.
Thanks Ankit,...it works.
DeleteThanks a lot.
Regards,
Mahesh
Hi Ankit,
ReplyDeleteI have gone through your posts in your blogs and it is very helpful to me.
Hope you will write more posts which helps other developers.
Actually I have simple HelloWorld Webservice which I want to call from CRM 2011.
I tried a lot, even gone through Mahender Pal’s post, but nothing is works.
Do you have any resource or any step by step article which helps me successfully calling the webservice from CRM.
Thanks
Hello,
ReplyDeleteSorry for the late response.
Thank you for your kind of words. Could you please share your code and explain how did you implement it?