Wednesday 24 June 2020

How to get Entity Metadata Using WEB API call in Dynamic CRM 365

As of now SDK.Metadata.js using the SOAP calls which are deprecated.
to get the Entities Metadata we should create and call a JavaScript function on the Onload event.

Below Function retrieve the all entities metadata of IsCustomizable eq to true.

function GetEntityMetadata() {
    var result;
    var apiQuery = Xrm.Utility.getGlobalContext().getClientUrl() + "/api/data/v9.1/" + "EntityDefinitions?$select=LogicalName,DisplayName,IsCustomizable&$filter=IsCustomizable/Value eq true";
    var entityList = ExecuteQuery(apiQuery);
    var localmetadataID = "";
    if (entityList != null) {
        for (var i = 0; i < entityList.length; i++)
        {
            var localName = entityList[i].LogicalName;
            if (localName == "contact") {
                localmetadataID = entityList[i].MetadataId;
            }
         
        }
    }
    //var entityAttributes=GetEntityAttributes(localmetadataID);
    alert(localmetadataID);
    return entityList;
}

function ExecuteQuery(apiQuery) {
    var results;
    $.ajax({
        type: "GET",
        contentType: "application/json; charset=utf-8",
        datatype: "json",
        url: apiQuery,
        async: false,
        beforeSend: function (xhr) {
            xhr.setRequestHeader("Accept", "application/json");
        },
        success: function (data, textStatus, xhr) {
            if (data.value != null && data.value.length) {
                results = data.value;
            }
        },
        error: function (xhr, textStatus, errorThrown) {
            if (xhr && xhr.responseText) {
                console.error('OData Query Failed: \r\n' + apiQuery + '\r\n' + xhr.responseText);
            }
            throw new Error(errorThrown);
        }
    });
    return results;
}


Get the All Attributes metadata

function GetEntityAttributes(entityMetadataId) {
    debugger;
   // var entitynamec = "contact";
    var apiQuery = Xrm.Utility.getGlobalContext().getClientUrl() + "/api/data/v9.1/" + "EntityDefinitions(" + entityMetadataId + ")?$select=LogicalName";

    apiQuery += "&$expand=Attributes($select=LogicalName,AttributeType,DisplayName;$filter=AttributeType ne Microsoft.Dynamics.CRM.AttributeTypeCode'Lookup' and AttributeType ne Microsoft.Dynamics.CRM.AttributeTypeCode'Customer')";

    var results;
    $.ajax({
        type: "GET",
        contentType: "application/json; charset=utf-8",
        datatype: "json",
        url: apiQuery,
        async: false,
        beforeSend: function (xhr) {
            xhr.setRequestHeader("Accept", "application/json");
        },
        success: function (data, textStatus, xhr) {
            if (data != null) {
                results = data;
            }
        },
        error: function (xhr, textStatus, errorThrown) {
            if (xhr && xhr.responseText) {
                console.error('API Query Failed: \r\n' + apiQuery + '\r\n' + xhr.responseText);
            }
            throw new Error(errorThrown);
        }
    });
    return results;
}