Retrieve Global OptionSet meta data and value

I was working on a Silverlight web resource tool in Dynamics Crm 2011 that can display all global optionset meta data and all optionset values/label for specific global optionset by calling  OrganizationRequest using message name RetrieveOptionSet. Looks like currently there isn’t that much details available for this, even in msdn reference for Micosoft.Xrm.Sdk, it just simply says the RequestName property supporting ‘RetrieveOptionSet’ message name that can be used both  online and offline. I managed to complete this successfully, would like to share in more detail.

1. Retrive all Global OptionSets through OrganizationRequest in Silverlight page

//start...
  try
  {
      OrganizationRequest request = new OrganizationRequest();
      request.RequestName = "RetrieveAllOptionSets";
      request.Parameters = new ParameterCollection();
      request.Parameters.Add(new KeyValuePair<string, object>
             ("RetrieveAsIfPublished", false));
      client.ExecuteCompleted += new EventHandler<ExecuteCompletedEventArgs>
             (ExecuteRetrieveAllOptionSets_ExecuteCompleted);
      client.ExecuteAsync(request);
   }
   catch (FaultException<OrganizationServiceFault> ex)
   {
      ExceptionHandling(ex);
   }
   //continue...
private void ExecuteRetrieveAllOptionSets_ExecuteCompleted
        (object sender, ExecuteCompletedEventArgs response)
{
   try
   {
     ObservableCollection<OptionSetMetadataBase> data = 
      (ObservableCollection<OptionSetMetadataBase>)response.Result.Results[0].Value;
     var values = from d in data
                    orderby d.DisplayName
                    select d;
     // Bind to datagrid
     globalOptionsetData.ItemsSource = values;
   }
   catch (Exception ex)
   {
       ExceptionHandling(ex);
   }
}

2. Retrieve one specific Global OptionSet value/label in Silverlight page

    //start...
    try
    {
        OrganizationRequest request = new OrganizationRequest();
        request.RequestName = "RetrieveOptionSet";        
        request.Parameters = new ParameterCollection();
        request.Parameters.Add(new KeyValuePair<string, object>
                ("Name", globalOptionSetName));
        request.Parameters.Add(new KeyValuePair<string, object>
                ("MetadataId", new Guid("00000000-0000-0000-0000-000000000000")));
        request.Parameters.Add(new KeyValuePair<string, object>
                (RetrieveAsIfPublished", false));
        client.ExecuteCompleted += new EventHandler<ExecuteCompletedEventArgs>
                (ExecuteRetrieveOptionSet_ExecuteCompleted);
        client.ExecuteAsync(request);
    }
    catch (FaultException<OrganizationServiceFault> ex)
    {
        ExceptionHandling(ex);
    }
    //continue...

    private void ExecuteRetrieveOptionSet_ExecuteCompleted(object sender, 
            ExecuteCompletedEventArgs response)
    {
        try
        {
          OptionSetMetadata data=
              (OptionSetMetadata)response.Result.Results[0].Value;
          var values = from d in data.Options
                       orderby d.Value
                       select d;
          //binding to DataGrid
          dataGrid.ItemsSource = values;
        }
        catch (Exception ex)
        {
            ExceptionHandling(ex);
        }
    }

In both cases, available member properties of OptionSetMetadataBase (item 1) and Options (item 2)
can be displayed using e.g. DataGrid.

Leave a comment