In one of our routine tasks, we received a requirement to retrieve TimeZone display text from the selected option of a whole number field in C# code. So, here I will give detail on how the code is used to retrieve that.

Note that this field is not like common option set field as we usually use in dynamics CRM/365.

The field schema looks like this

1.png

The field displays as shown here

2

We have to pass the selected value of “Time Zone” field which a numeric code value like “1,2,3…”. The following code will return the display text of that time zone as you can see in the above screenshot. You can skip the call to “GetCRMConnectionOnline” method if you are gonna use this code in the plugin then use the organization service object from plugin context instead of creating a new object for connection.

Here is the code which we used to retrieve that

public static void GetTimeZone(int crmTimeZoneCode)
        {
            IOrganizationService service = GetCRMConnectionOnline();
            var qe = new QueryExpression("timezonedefinition"); //-- "timezonedefinition" is the entity name
            qe.ColumnSet = new ColumnSet("userinterfacename"); //--"userinterfacename" is the field name you need to retrieve
            qe.Criteria.AddCondition("timezonecode", ConditionOperator.Equal, crmTimeZoneCode);

            Entity ent = service.RetrieveMultiple(qe).Entities.First();
            string timeZoneText = ent.GetAttributeValue<string>("userinterfacename");
            Console.WriteLine("Time Zone Code = " + crmTimeZoneCode.ToString() + "  **********  Time Zone Option Display Text : " + timeZoneText);
        }

        public static IOrganizationService GetCRMConnectionOnline()
        {
            ClientCredentials clientCredentials = new ClientCredentials();
            clientCredentials.UserName.UserName = "MyUser@XYZ.onmicrosoft.com";
            clientCredentials.UserName.Password = "Passw@rd";

            // For Dynamics 365 Customer Engagement V9.X, set Security Protocol as TLS12
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
            // Get the URL from CRM, Navigate to Settings -> Customizations -> Developer Resources
            // Copy and Paste Organization Service Endpoint Address URL 
            string organizationServiceURL = "https://orgName.api.crm4.dynamics.com/XRMServices/2011/Organization.svc";
            IOrganizationService organizationService = (IOrganizationService)new OrganizationServiceProxy(new Uri(organizationServiceURL), null, clientCredentials, null);
            return organizationService;
        }