Posts Tagged ‘IOrganization Service’

Reading Optionset using CRM 2011 IOrganizationService

February 3, 2012 1 comment


Below is the code to read “Optionset” using CRM 2011 IOrganizationService.

Below logic reads the Optionset and returns “KeyValue” list (Key = Option Label; Value = Option Value)

using Microsoft.Xrm.Sdk;

using Microsoft.Xrm.Sdk.Client;

using Microsoft.Xrm.Sdk.Query;

using Microsoft.Xrm.Sdk.Messages;

using Microsoft.Xrm.Sdk.Metadata;

  public List<KeyValuePair<string, int>> GetOptionSet(string entityName, string attributeName, IOrganizationService service)


var optionSet = new List<KeyValuePair<string, int>>();

            RetrieveAttributeRequest request = new RetrieveAttributeRequest();

request.EntityLogicalName = entityName;

request.MetadataId = Guid.Empty;

request.LogicalName = attributeName;

request.RetrieveAsIfPublished = true;

 OrganizationResponse response = service.Execute(request); 

if (response != null && response.Results != null)


foreach (KeyValuePair param in response.Results)


foreach (OptionMetadata option in ((EnumAttributeMetadata)param.Value).OptionSet.Options)


string name = option.Label.LocalizedLabels[0].Label.ToString().Trim();

int value = option.Value.Value;

optionSet.Add(new KeyValuePair(name, value));




return optionSet;


        // Returns option label by value

public string GetOptionLabelByValue(List<KeyValuePair<string, int>> optionSet, int optValue)


foreach (KeyValuePair< string, int> optn in optionSet)


if (optn.Value == optValue)


return optn.Key;



return string.Empty;


        // Returns option Value by Label

public int GetOptionValueByLabel(List<KeyValuePair<string, int>> optionSet, string optLabel) {        {

foreach (KeyValuePairoptn in optionSet)


if (optn.Key) == optLabel)


return optn.Value;



return 0;


How Do I call these methods:-

  • To read ‘Gender (i.e., gendercode)’ optionset of entity ‘Contact (i.e., contact)’
  • Get Optionset dictionary by calling “GetOptionSet()” method
  • Call “GetOptionLabelName()” to get Label by Value
  • Call “GetOptionValueByLabel()” to get Value by Label

// Get OptionSet First

var  optGender  = GetOptionSet(“contact”, “gendercode”, orgService);

// Get Option label by value

string optLblGander = GetOptionLabelName(optGender  , 1); /* 1 is Value of Male */

// To get Option Value by Label

int optValueGander = GetOptionValueByLabel(optGender  , “Male”);

Hope it helps 🙂

Types of services in CRM 2011

November 29, 2011 Leave a comment


Below are 3 main services that developers can use to extend the Dynamics CRM 2011 platform

  1. Deployment Web Service

Using this service we can

  • Create or import organizations
  • Enable and Disable organizations
  • Add deployment administrators
  • Configure IFD and Claims-based authentication

2.Discovery Web service

Using this service we can

  • Identify the organization information available in a deployment

3. IOrganization Service

  • In CRM 4.0 we had the CrmService & CrmMetadataService to access data & metadata.
  • In CRM 2011, These are combined in single IOrganization service endpoint that includes both data and metadata

This is basic information about CRM 2011 services.  In my next article i will come up with different ways to access these service API’s.

Hope this helps 🙂