locked
Get the Value of a OptionSet From its Text. CRM 2011. RRS feed

  • Question

  • Hello people.

    Please a hand.  I need to get a value from an OptionSet giving the text.  Example:

    - My client has a table with a field with the text:  "JOB". 

    - In CRM, we have an OptionSet with text: "JOB" with a value "1000001"

     - I am developing  an app. for a mass import, in which I need to set the OptionSet field with the value: 1000001, that corresponds to the text that the client is passing me.

    Thanks for your help in advanced.

     


    Roxana Cevallos Q. Microsoft CRM 4.0
    Tuesday, September 20, 2011 2:07 PM

Answers

  • Hi,

    Another way could be, you may also use RetrieveAttributeRequest to get the OptionSet metadata and then can triverse the attribute and the get the value by text. You may also refer the sample code below: 

     

     

    private int GetOptionsSetTextOnValue(IOrganizationService prmCrmService, string prmEntityName, string prmAttributeName, string prmOptionSetText)

            {

                int jj_OptionSetValue = 0;

                RetrieveAttributeRequest retrieveAttributeRequest = new RetrieveAttributeRequest

                {

                    EntityLogicalName = prmEntityName,

                    LogicalName = prmAttributeName,

                    RetrieveAsIfPublished = true

                };

                RetrieveAttributeResponse retrieveAttributeResponse = (RetrieveAttributeResponse)prmCrmService.Execute(retrieveAttributeRequest);

                Microsoft.Xrm.Sdk.Metadata.PicklistAttributeMetadata retrievedPicklistAttributeMetadata = (Microsoft.Xrm.Sdk.Metadata.PicklistAttributeMetadata)

                retrieveAttributeResponse.AttributeMetadata;

                OptionMetadata[] jj_OptionList = retrievedPicklistAttributeMetadata.OptionSet.Options.ToArray();

                foreach (OptionMetadata jj_OptionMetaData in jj_OptionList)

                {

                    if (jj_OptionMetaData.Label.UserLocalizedLabel.Label == prmOptionSetText)

                    {

                        jj_OptionSetValue = jj_OptionMetaData.Value.Value;

                        break;

                    }

                }

                return jj_OptionSetValue;

            }

     


     


    Jehanzeb Javeed

    http://worldofdynamics.blogspot.com
    Linked-In Profile |CodePlex Profile

    If you find this post helpful then please "Vote as Helpful" and "Mark As Answer".
    Tuesday, September 20, 2011 3:57 PM

All replies

  • In an app you can use RetrieveEntityRequest with the CRM service to get the values for an option set.  Just set entityfilters to attributes so you don't pull back everything.

    http://mileyja.blogspot.com/2011/05/how-to-retrieve-metadata-for-entity.html

    In Jscript you can use this syntax: to get the values for an option set from inside the form:

    http://mileyja.blogspot.com/2011/05/how-to-get-and-set-lookup-value-in.html

     

     

     


    Jamie Miley
    Check out my about.me profile!
    http://mileyja.blogspot.com
    Linked-In Profile
    Follow Me on Twitter!
    Tuesday, September 20, 2011 3:31 PM
    Moderator
  • Hi,

    Another way could be, you may also use RetrieveAttributeRequest to get the OptionSet metadata and then can triverse the attribute and the get the value by text. You may also refer the sample code below: 

     

     

    private int GetOptionsSetTextOnValue(IOrganizationService prmCrmService, string prmEntityName, string prmAttributeName, string prmOptionSetText)

            {

                int jj_OptionSetValue = 0;

                RetrieveAttributeRequest retrieveAttributeRequest = new RetrieveAttributeRequest

                {

                    EntityLogicalName = prmEntityName,

                    LogicalName = prmAttributeName,

                    RetrieveAsIfPublished = true

                };

                RetrieveAttributeResponse retrieveAttributeResponse = (RetrieveAttributeResponse)prmCrmService.Execute(retrieveAttributeRequest);

                Microsoft.Xrm.Sdk.Metadata.PicklistAttributeMetadata retrievedPicklistAttributeMetadata = (Microsoft.Xrm.Sdk.Metadata.PicklistAttributeMetadata)

                retrieveAttributeResponse.AttributeMetadata;

                OptionMetadata[] jj_OptionList = retrievedPicklistAttributeMetadata.OptionSet.Options.ToArray();

                foreach (OptionMetadata jj_OptionMetaData in jj_OptionList)

                {

                    if (jj_OptionMetaData.Label.UserLocalizedLabel.Label == prmOptionSetText)

                    {

                        jj_OptionSetValue = jj_OptionMetaData.Value.Value;

                        break;

                    }

                }

                return jj_OptionSetValue;

            }

     


     


    Jehanzeb Javeed

    http://worldofdynamics.blogspot.com
    Linked-In Profile |CodePlex Profile

    If you find this post helpful then please "Vote as Helpful" and "Mark As Answer".
    Tuesday, September 20, 2011 3:57 PM
  • Thanks a lot to both answers!.

    I only needed to check that the entity logical name and the attribute name were in 'lower case', to have the correct result.

    Best regards.

     


    Roxana Cevallos Q. Microsoft CRM 4.0
    Tuesday, September 20, 2011 5:29 PM
  • Tuesday, September 20, 2011 5:40 PM
    Moderator
  • This looks like the solution I need also. However how do I  convert from 'Import.CrmSdk.OrganizationServiceClient' to 'Microsoft.Xrm.Sdk.IOrganizationService' ?

    or is there an alternative way to use OrganizationServiceClient?

    Wednesday, November 30, 2011 9:32 PM
  • Hi,

    You need to add microsoft.crm.sdk.proxy dll file reference into you visual studio project, the dll library can be found into the <CRM SDK Dir>\bin directory


    Jehanzeb Javeed

    http://worldofdynamics.blogspot.com
    Linked-In Profile |CodePlex Profile

    If you find this post helpful then please "Vote as Helpful" and "Mark As Answer".
    Wednesday, November 30, 2011 10:02 PM

  • I do not understand.
    I am makeing the method call like this:

    GetOptionsSetTextOnValue(CRMService, "contact", "new_contacttype", "Regular");

    where CRMService is of type OrganizationServiceClient. How do I convert to to 'Microsoft.Xrm.Sdk.IOrganizationService' ?

    Wednesday, November 30, 2011 10:09 PM
  • Hi,

    You can add a reference to the dll library mentioned above and can use the OrganizaitonServiceClient as

    Microsoft.Xrm.Sdk.IOrganizationService  CrmService

    Are you using this code for Crm 4.0?


    Jehanzeb Javeed

    http://worldofdynamics.blogspot.com
    Linked-In Profile |CodePlex Profile

    If you find this post helpful then please "Vote as Helpful" and "Mark As Answer".
    Wednesday, November 30, 2011 10:19 PM
  • i am using it in CRM 2011

    are you saying i can cast CrmService to IOrganizationService?

    Wednesday, November 30, 2011 10:25 PM
  • i tried it and of course i can't
    Thursday, December 1, 2011 6:55 AM
  • Hi, Try to use this:

    serviceProxyCRM = new OrganizationServiceProxy( configServer.OrganizationUri,
                                                                        configServer.HomeRealmUri,
                                                                        configServer.Credentials,
                                                                        configServer.DeviceCredentials);
                        // This statement is required to enable early-bound type support.
                        serviceProxyCRM.ServiceConfiguration.CurrentServiceEndpoint.Behaviors.Add(new ProxyTypesBehavior());
                        //--test
                        IOrganizationService servicioCRM = (IOrganizationService)serviceProxyCRM;

    Note: ConfigServer is a classs created by me, to get all the necessary parameters.

    Dlls to import:

    using

     

    System;

    using

     

    System.ServiceModel;

    using

     

    System.ServiceModel.Description;

    // These namespaces are found in the Microsoft.Xrm.Sdk.dll assembly

    // located in the SDK\bin folder of the SDK download.

    using

     

    Microsoft.Xrm.Sdk;

    using

     

    Microsoft.Xrm.Sdk.Query;

    using

     

    Microsoft.Xrm.Sdk.Client;

    Import in the Project the reference:

    microsoft.crm.sdk.proxy  AND   microsoft.xrm.sdk

    I hope this works for you.
    Roxana Cevallos Q. Microsoft CRM 4.0
    • Proposed as answer by biconix Friday, December 2, 2011 10:04 AM
    Thursday, December 1, 2011 2:41 PM
  • works great

    thanks

     

     

    Friday, December 2, 2011 10:04 AM