Development Desk

Just another WordPress.com site

Category Archives: Micorsoft Dynamics CRM

Get Option Set Value by Option Set Text in CRM on-prem /Online

To read crm option set information from the CRM, CRM matadata call need to made and it will bring the information on the based of following input attributes. below input attributes are showing test data.
string entityName = "contact";
string fieldName = "title"; // could be any optionset entity attribute like agegroug etc
string optionSetText = "Mr"
pass these values to given method and it will return optionset value of the selected option set.
GetOptionSetValueByText(entityName , attributeName, attributeValue);

public static int? GetOptionSetValueByText(string entityName, string fieldName, string optionSetText)
 {
 var retrieveAttributeRequest = new RetrieveAttributeRequest
 {
 EntityLogicalName = entityName,
 LogicalName = fieldName,
 RetrieveAsIfPublished = true
 };

 var retrieveAttributeResponse = (RetrieveAttributeResponse)_orgService.Execute(retrieveAttributeRequest);
 var attributeMetadataList = (EnumAttributeMetadata)retrieveAttributeResponse.AttributeMetadata;

 var optionSetInfo = (from optionSetDetail in attributeMetadataList.OptionSet.Options
 from labelInfo in optionSetDetail.Label.LocalizedLabels.Where(label => label.Label == optionSetText).Where(l => optionSetDetail.Value != null)
 select optionSetDetail).ToList();

 return optionSetInfo.Any() && optionSetInfo.Count > 0 && optionSetInfo[0].Value.HasValue ? optionSetInfo[0].Value : null;
 }

Connecting to CRM Online (2013/2015)

Whenever i am going to extend CRM system or developing 3rd party party for CRM or a webservice. CRM Authentication is required to perform Create, Update, Delete or retrieve operations in CRM organisation therefore connection must be established and authenticated correctly to get hands on the CRM IOrganisation Service. Below is simple code to connect to CRM. i wrote this example in C# using windows console application and reading credentials from app.config.

</pre>
<pre class="lang-cs prettyprint prettyprinted"><code><span class="pln">public static class CrmConnector
 {
 public static IOrganizationService CreateCrmService()
 {
 ConnectToMscrm();

 var userid = ((WhoAmIResponse)_orgService.Execute(new WhoAmIRequest())).UserId;
 if (userid != Guid.Empty)
 {
 Console.WriteLine("Connection Established Successfully");
 Console.ReadKey();
 }
 else
 {
 Console.WriteLine("Connection couldn't established Successfully");
 Console.ReadKey();
 }
 
 return _orgService;
 }

 static IOrganizationService _orgService;

 public static void ConnectToMscrm()
 {
 try
 {
 var connectorUser = ConfigurationManager.AppSettings["connectorUser"]; //test.user@crmorg.onmicrosoft.com
 var connectorPassword = ConfigurationManager.AppSettings["connectorPassword"]; //password123
 var uri = ConfigurationManager.AppSettings["crmOrgServiceUrl"]; //https://crmorg.api.crm4.dynamics.com/XRMServices/2011/Organization.svc
 //var homeRelamUrl = new Uri(ConfigurationManager.AppSettings["HomeRelamURL"]);


 var credentials = new ClientCredentials();
 credentials.UserName.UserName = connectorUser;
 credentials.UserName.Password = connectorPassword;

 var serviceUri = new Uri(uri);

 var orgProxy = new OrganizationServiceProxy(serviceUri, null, credentials, null);
 orgProxy.EnableProxyTypes();
 _orgService = (IOrganizationService)orgProxy;
 }
 catch (Exception ex)
 {
 //throw new Exception("Error Has Occurred while connecting to CRM Server " + ex.Message);
 Console.WriteLine("Error Has Occurred while connecting to CRM Server " + ex.Message);
 }
 }
 } 
</span></code></pre>
<pre>