Thursday, 18 February 2016

Query Metadata of Global OptionSet in Microsoft Dynamics CRM 2016 using Web API


To retrieve meta data of global optionSet in Web API, first you will need to retrieve  meta data id of option Set.
So i create two functions , 

RetrieveGlobalOptionSetMetaDataId("OptionSetSchemaName") function 

will retrieve meta data id of optionSet by passing schema name of option , then pass meta data id to


RetrieveGlobalOptionSetOptionsMetaData(optionSetMetaDataId) 

it will retrieve meta data of optionSet.

I am binding options of global optionSet to Select element in Html Page , 




var globalOptionSetMetaDataId = RetrieveGlobalOptionSetMetaDataId("OptionSetSchemaName");

RetrieveGlobalOptionSetOptionsMetaData(globalOptionSetMetaDataId );


function RetrieveGlobalOptionSetMetaDataId(OptionSetSchemaName) {

    var globalOptionSetMetaDataId = null;
    var context = parent.Xrm.Page.context;
    var webapiQuery = context.getClientUrl() + "/api/data/v8.0/GlobalOptionSetDefinitions?$select=Name";
    var service = new XMLHttpRequest();
    service.open("GET", webapiQuery, false);
    service.setRequestHeader("Accept", "application/json");
    service.setRequestHeader("Content-Type", "application/json; charset=utf-8");
    service.setRequestHeader("OData-MaxVersion", "4.0");
    service.setRequestHeader("OData-Version", "4.0");
    service.send();
    if (service.readyState == 4 /* complete */) {
        if (service.status == 201 || service.status == 200) {
            var RetrieveService = eval('(' + service.responseText + ')');
            if (RetrieveService.value.length > 0) {
                for (var i = 0; i < RetrieveService.value.length; i++) {
                    if (RetrieveService.value[i].Name == OptionSetSchemaName) {
                        globalOptionSetMetaDataId = RetrieveService.value[i].MetadataId;
                        break;
                    }
                }
            }
        }
    }
    return globalOptionSetMetaDataId ;
}


function RetrieveGlobalOptionSetOptionsMetaData(optionSetMetaDataId) {

    var context = parent.Xrm.Page.context;
    var webapiQuery = context.getClientUrl() + "/api/data/v8.0/GlobalOptionSetDefinitions(" + optionSetMetaDataId + ")";
    var service = new XMLHttpRequest();
    service.open("GET", webapiQuery, false);
    service.setRequestHeader("Accept", "application/json");
    service.setRequestHeader("Content-Type", "application/json; charset=utf-8");
    service.setRequestHeader("OData-MaxVersion", "4.0");
    service.setRequestHeader("OData-Version", "4.0");
    service.send();
    if (service.readyState == 4 /* complete */) {
        if (service.status == 201 || service.status == 200) {
            var RetrieveService = eval('(' + service.responseText + ')');
            if (RetrieveService.Options.length > 0) {
                var options = "";
                for (var i = 0; i < RetrieveService.Options.length; i++) {
                    var optionLabel = RetrieveService.Options[i].Label.UserLocalizedLabel.Label;
                    var optionValue = RetrieveService.Options[i].Value;
                    options += "<option value='" + optionValue + "'>" + optionLabel + "</option>";
                }
                $("#dropdownReason").html(options);
            }
        }
    }
}

No comments:

Post a Comment

Read only field , get latest value from read only field in in power app portal

Below line is used to make field readonly in power app portal through js $( "#bdo_relationshiprole_name" ).parent().css( "po...