Sunday, December 16, 2012

Reading OptionSet Items Key/value Using MetaData Request

             Sometimes we need to get the option set key value pair to display in web resource like Silverlight etc or sometimes we have option set key but we need to display value (which is the string type display in option set).

             When creating web resource for MS CRM 2011 there are some situations when we need to display one of the option set in combo box or in dropdown html control so that user can select any of the option from the combo box or from dropdown control. In order to get option set item we need to make meta data call which can return number of item of that field of entity.You can see the screen of the sample application in Image 1; here you can see that I have continue my last post where I have display entity attribute meta data for this sample I have displayed on option set attributes and also added data grid control to display option set items.
Image 1
Let us discuss code which is used to get the option set items from by using meta data request You can see the code in List 1 you can see the function which is used to get the items "getOptionSetLabels", function will take entity schema name and also the schema name of the option set attribute and display name of the option set attribute which will be displayed while showing busy bar.
Here you can see that first property of the request is the request name is "RetreiveAttributes" and the second property which is set is logical name of the entity, and I have used entity name which is passed to the function and then is the logical name which is the schema name of the option set attribute which is also passed to the function. Both entity name and the attribute name(which is the option set ) are schema names.
List 1

Code in List 1, I have mentioned important properties of the request, now when the request return call back event handler is executed and here you can see I just get the option set meta data to loop through and get all the option set item as code is listed in List 2.
List 2

This is how you can get option set item using meta data request, if you want to get only string value of just one option set item then you can loop through the item in the option set and then use if condition to get the specific value and then break. You can use same request to get all option set item or you can get only one value of the option set item by passing the key ( integer value of the option set).

You can download the source code of the sample from here.
and working CRM 2011 solution from here.

Go to Home

All and any comments / bugs / suggestions are welcomed!

No comments: