Monday, July 11, 2011

Retrieving optionset Lable data using Metadata service in CRM 2011

Hi All, Using OData service we are not able to get the option set selected text of an entity. Its providing only value field, but not the text. so I have used Metadata service to retrieve Option set text for the selected value.
 Here I am retrieving State option set text from country entity and assigning that option text to text field.

function RetrieveOptionsetLabel()
{
     // Entity schema name 
     var entityLogicalName = "new_country";
     // option set schema name
     var RetrieveAttributeName = "new_state";
     // Target Field schema name to which optionset text needs to be assigned
     var AssignAttributeName = "new_state";

// Option set value for which label needs to be retrieved
        var stateValue = optionValue;
     
        // Calling Metadata service to get Optionset Label
        SDK.MetaData.RetrieveEntityAsync(SDK.MetaData.EntityFilters.Attributes, entityLogicalName, null, false, function (entityMetadata) { successRetrieveEntity(entityLogicalName, entityMetadata, RetrieveAttributeName, stateValue, AssignAttributeName); }, errorDisplay);

}

// Called upon successful metadata retrieval of the entity
function successRetrieveEntity(logicalName, entityMetadata, RetrieveAttributeName, OptionValue, AssignAttributeName) {
    ///<summary>
    /// Retrieves attributes for the entity 
    ///</summary>

    var success = false;
    for (var i = 0; i < entityMetadata.Attributes.length; i++) {
        var AttributeMetadata = entityMetadata.Attributes[i];
        if (success) break;
        if (AttributeMetadata.SchemaName.toLowerCase() == RetrieveAttributeName.toLowerCase()) {
            for (var o = 0; o < AttributeMetadata.OptionSet.Options.length; o++) {
                var option = AttributeMetadata.OptionSet.Options[o];
                if (option.OptionMetadata.Value == OptionValue) {
                    Xrm.Page.getAttribute(AssignAttributeName).setValue(option.OptionMetadata.Label.UserLocalizedLabel.Label);
                    success = true;
                    break;
                }
            }
        }

    }


}


 function errorDisplay(XmlHttpRequest, textStatus, errorThrown) {

     alert(errorThrown);
 }

Note: Dont forget to add "sdk.metadata.js" resource to form before calling these methods. you can find this library in sdk "sdk\samplecode\js\soapforjscript\soapforjscript\scripts".



11 comments:

  1. what security role privilege needs to be enabled in order to execute this call? I have the code working when run as sys admin, but it fails for regular users.

    ReplyDelete
  2. Hi Andrew,

    User needs to have at least Read privileges on Entity, Attributes, Relationships on Customization Tab of the security Role.

    make sure you have configured those settings..

    ReplyDelete
  3. Thank you very much but it raised error in case of Status column
    I added this code
    var option = AttributeMetadata.OptionSet.Options[o];
    if (option.OptionMetadata!=null&& option.OptionMetadata.Value == OptionValue) {
    AssignAttributeName.innerText = option.OptionMetadata.Label.UserLocalizedLabel.Label;
    success = true;
    break;
    }
    if (option.StatusOptionMetadata != null && option.StatusOptionMetadata.Value == OptionValue) {
    AssignAttributeName.innerText = option.StatusOptionMetadata.Label.UserLocalizedLabel.Label;
    success = true;
    break;
    }

    ReplyDelete
    Replies
    1. Hi Ahmed,

      For Status(schema name : statecode) use StateOptionMetadata
      For StatusReason (schema name: statuscode) use StatusOptionMetadata

      Delete
  4. Hi Ahmed,

    What kind of error you are getting?

    ReplyDelete
  5. Guru, is this still the only way to retrieve option set text?

    ReplyDelete
    Replies
    1. This is one of the way to retrieve option set text. i'm not sure about the other possible options.

      Delete
  6. Just starting with 2011, I've added code to accces the SDK.metadata methids, and added the script to a form. However, I get this error:
    "Unable to get value of the property 'RetrieveEntityAsync': object is null or undefined"
    It sounds to me lie I'm not loading the resource properly. Any clues?

    ReplyDelete
    Replies
    1. check 'RetrieveEntityAsync' object available or not

      Delete
  7. I tried using the same code but its not calling my sdk.metadata.js library.
    And it is giving an error called "sdk is undefined".
    please share your experience on the same if you have also faced it.
    Thanks

    ReplyDelete
  8. Hi All,

    Its pretty simple to get the optionset label from the particular entity and attribute

    Just add the SDK.Metadata.js to your CRM form (you can find this under sdk\samplecode\js\soapforjscript\soapforjscript\scripts)

    And then Create one more webresource and add the below javascript function to it.

    function GetOptionSetLabel(EntityLogicalName, AttributeLogicalName)
    {

    SDK.Metadata.RetrieveAttribute(EntityLogicalName, AttributeLogicalName, "00000000-0000-0000-0000-000000000000", true,
    function (result) {

    for (var i = 0; i < result.OptionSet.Options.length; i++) {

    var text = result.OptionSet.Options[i].Label.LocalizedLabels[0].Label;

    var value = result.OptionSet.Options[i].Value;
    }
    },
    function (error) { }
    );
    }

    And its done.

    ReplyDelete