Retrieving metadata using the Web API

Microsoft Dynamics 365 is a metadata-driven application. Sometimes, you need to query the metadata while working with requirements such as those mentioned in this scenario. For this purpose, you can easily query the metadata using the Web API. We will use the EntityDefinitions entity to retrieve the metadata of the Contact entity. The URL will be as follows:

clientURL + "api/data/v8.2/EntityDefinitions(LogicalName='contact')/Attributes"

We will create an HTML web resource and the following is the code to retrieve the metadata using the Web API. You will need to create a HTML control for selection in the web resource, as you cannot append items into the option set. So, we'll use the $(document).ready , function of JQuery, which will be executed on the page load. The Web API call is made inside this function, which will retrieve all the fields of the Contact entity. After retrieving the fields, the option set is appended with the retrieved fields:

<html>
<head>
<meta charset="utf-8" />
<title></title>
<script type="text/javascript" src="ClientGlobalContext.js.aspx">
</script>
<script type="text/jscript" src="
https://dynamistydemo.crm.dynamics.com//
WebResources/mcs_jquerymin"></script>
<script type="text/jscript">
//Function called when the web resource is loaded
$(document).ready(function () {
//Initializing requet
var req = new XMLHttpRequest();

//Opens request
req.open("GET", window.parent.Xrm.Page.context.getClientUrl() +
"/api/data/v8.2/EntityDefinitions(LogicalName='contact')
/Attributes", true);

//Sets request Headers
req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Content-Type", "application/json;
charset=utf-8");
req.setRequestHeader("Prefer",
"odata.include-annotations="*"");

//Function to detect ready state change
req.onreadystatechange = function () {
if (this.readyState === 4) {
req.onreadystatechange = null;

//Check if request completed successfully
if (this.status === 200) {
var result = JSON.parse(this.response);
var index = 1;
for (var i = 0; i < result.value.length; i++) {
//Gets schemaname from response.
var schemaName = result.value[i].SchemaName;
$("#Entities").append("<option value='" + index +
"'>" + schemaName + "</option>");
index = index + 1;
}

}
else {
Xrm.Utility.alertDialog(this.statusText);
}
}
};
req.send();

});
</script>

<style type="text/css">
.select {
font-size: 12px;
font-family: Segoe00020UI;
color: #444444;
margin: -8px;
}

.control {
margin-left: 108px;
font-size: 12px;
height: 23px;
}
</style>
</head>
<body>
<div class="select">
<label for="Entities">Fields</label>
<select name="Entities" id="Entities" class="control">
<option value="0">--<options>
</select>
</div>
</body>
</html>

Here is the output of the preceding code:

As you can see in the preceding screenshot, the option set is loaded with the field names of the entity.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset