We can retrieve attributes of an entity on a form using SystemForm metadata. SystemForm has information of all the forms present on the Entity and also formxml which can be used to parse the field names present on the form. Refer below code:
<html><head>
<meta charset="utf-8">
<style type="text/css">
body
{
background-color : #FFFFFF;
font-family:Segoe UI, Tahoma, Arial;
font-size:11px;
}
dynamicForm{
overflow-y:scroll;
}
td{
color: #3b3b3b;
font size=11px;
text-align: center;
}
select{
font-family:Segoe UI, Tahoma, Arial;
font-size:11px;
valign:top;
}
.tablabel{
font-size:15px;
font-weight:bold;
color:#1261e1;
font-family:Segoe UI, Tahoma, Arial
}
.sectionlabel{
font-size:13px;
font-weight:bold;
color:#3b3b3b;
font-family:Segoe UI, Tahoma, Arial
}
.section{
margin-left: 15px;
}
.attributeTable{
margin-left: 30px;
}
</style>
<script language="javascript" src="new_jquery_1.7.1.min.js" type="text/javascript"></script>
<script type="text/javascript">
function retrieveEntity() {
var serverURL = parent.Xrm.Page.context.getClientUrl();
var Query = "EntityDefinitions?$select=DisplayName,EntitySetName,ObjectTypeCode,LogicalName";
var req = new XMLHttpRequest();
req.open("GET", serverURL + "/api/data/v8.2/" + Query, true);
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");
req.setRequestHeader("Prefer", "odata.include-annotations=*");
req.onreadystatechange = function() {
if (this.readyState == 4 /* complete */ ) {
req.onreadystatechange = null;
if (this.status == 200) {
var data = JSON.parse(this.response);
if(data!=null && data.value !=null){
var AllentityMetadataCollection = data.value;
var entityMetadataCollection = [];
for(var i = 0; i <AllentityMetadataCollection.length; i++ ){
if(AllentityMetadataCollection[i].DisplayName && AllentityMetadataCollection[i].DisplayName.UserLocalizedLabel){
entityMetadataCollection[entityMetadataCollection.length] = AllentityMetadataCollection[i];
}
}
entityMetadataCollection.sort(function (a, b) {
if (a.DisplayName.LocalizedLabels[0].Label < b.DisplayName.LocalizedLabels[0].Label)
{ return -1 }
if (a.DisplayName.LocalizedLabels[0].Label> b.DisplayName.LocalizedLabels[0].Label)
{ return 1 }
return 0;
});
for (var i = 0; i < entityMetadataCollection.length; i++) {
var entity = entityMetadataCollection[i];
if($("#ddlallentity").length!=0){
if(entity.DisplayName.UserLocalizedLabel!=null){
$("#ddlallentity").append("<option value='"+entity.EntitySetName+"|"+entity.ObjectTypeCode+"|"+entity.MetadataId+"|"+entity.LogicalName+"'>"+entity.DisplayName.LocalizedLabels[0].Label+"</option>");
}
}
else{
$("#ddlallentity").change(function (){
var entityName = $("#ddlallentity option:selected").text();
var entityLogicalName = $("#ddlallentity option:selected").val();
parent.Xrm.Page.getAttribute("new_entityname").setValue(entityLogicalName);
if($("#ddlallForms").length!=0){
$("#ddlallForms").empty()
}
if($("#ddlattribute").length!=0){
$("#ddlattribute").empty()
}
});
}
}
selectValueonLoad();
}
} else {
var error = JSON.parse(this.response).error;
alert(error.message);
}
}
};
req.send();
}
function setLogicalNameofEntity(){
var entityName = $("#ddlallentity option:selected").text();
var entLogicalName = $("#ddlallentity option:selected").val();
var entityLogicalName = $("#ddlallentity option:selected").val().split("|")[0];
var entityTypeCode = $("#ddlallentity option:selected").val().split("|")[1];
var entityMetadataId = $("#ddlallentity option:selected").val().split("|")[2];
var entityLogicalName1 = $("#ddlallentity option:selected").val().split("|")[3];
parent.Xrm.Page.getAttribute("new_entityname").setValue(entityLogicalName);
parent.Xrm.Page.getAttribute("new_entitytypecode").setValue(entityTypeCode);
parent.Xrm.Page.getAttribute("new_entitymetadataid").setValue(entityMetadataId);
if($("#ddlattribute > option").length!=0){
$("#ddlattribute").empty();
$("#ddlattribute").css({
'min-width': '100px'
});
}
if($("#ddlallForms > option").length!=0){
$("#ddlallForms").empty();
$("#ddlallForms").css({
'min-width': '100px'
});
}
$('#dynamicForm').empty();
var attributesRetrieved = false;
retrieveForms(entityLogicalName1,entityMetadataId);
// retrieveAttributes(attributesRetrieved,entityMetadataId);
}
function setFormNameofEntity(){
var FormName = $("#ddlallForms option:selected").text();
var entityMetadataId = $("#ddlallForms option:selected").val().split("|")[0];
var formid = $("#ddlallForms option:selected").val().split("|")[1];
retrieveFormsXml(formid);
var FormXml = parent.Xrm.Page.getAttribute("new_formxml");
if($("#ddlattribute > option").length!=0){
$("#ddlattribute").empty();
$("#ddlattribute").css({
'min-width': '100px'
});
}
$('#dynamicForm').empty();
var attributesRetrieved = false;
retrieveAttributes(attributesRetrieved,entityMetadataId);
}
function retrieveForms(ObjectTypeCode,entityMetadataId){
var req = new XMLHttpRequest();
req.open("GET", parent.Xrm.Page.context.getClientUrl() + "/api/data/v8.2/systemforms()?$select=formid,name,objecttypecode,formxml,type&$filter=objecttypecode eq '"+ObjectTypeCode+"' and formactivationstate eq 1 and (type eq 2 or type eq 5 or type eq 6 or type eq 7 or type eq 11 or type eq 12)", true);
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=\"*\"");
req.onreadystatechange = function() {
if (this.readyState === 4) {
req.onreadystatechange = null;
if (this.status === 200) {
var data = JSON.parse(this.response);
if(data!=null && data.value !=null){
var AllentityFormCollection = data.value;
var entityFormCollection = [];
for(var i = 0; i <AllentityFormCollection.length; i++ ){
if(AllentityFormCollection[i].name){
entityFormCollection[entityFormCollection.length] = AllentityFormCollection[i];
}
}
entityFormCollection.sort(function (a, b) {
if (a.name < b.name)
{ return -1 }
if (a.name> b.name)
{ return 1 }
return 0;
});
for (var i = 0; i < entityFormCollection.length; i++) {
var form = entityFormCollection[i];
if($("#ddlallForms").length!=0){
if(form.name!=null){
$("#ddlallForms").append("<option value='"+entityMetadataId+"|"+form.formid+"'>"+form.name+"|"+form['type@OData.Community.Display.V1.FormattedValue']+"</option>");
}
}
}
}
} else {
Xrm.Utility.alertDialog(this.statusText);
}
}
};
req.send();
}
function retrieveFormsXml(formid){
var req = new XMLHttpRequest();
req.open("GET", parent.Xrm.Page.context.getClientUrl() + "/api/data/v8.2/systemforms()?$select=formxml&$filter=formid eq "+formid.toUpperCase() , true);
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=\"*\"");
req.onreadystatechange = function() {
if (this.readyState === 4) {
req.onreadystatechange = null;
if (this.status === 200) {
var data = JSON.parse(this.response);
if(data!=null && data.value[0] !=null){
var form = data.value[0];
if(form.formxml!=null){
parent.Xrm.Page.getAttribute("new_formxml").setValue(form.formxml);
}
}
} else {
Xrm.Utility.alertDialog(this.statusText);
}
}
};
req.send();
}
function retrieveAttributes(attributesRetrieved,entityMetadataId) {
var serverURL = parent.Xrm.Page.context.getClientUrl();
var Query = "EntityDefinitions("+ entityMetadataId.toUpperCase() +")?$select=LogicalName&$expand=Attributes($select=LogicalName,DisplayName)";
var req = new XMLHttpRequest();
req.open("GET", serverURL + "/api/data/v8.2/" + Query, true);
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");
req.setRequestHeader("Prefer", "odata.include-annotations=*");
req.onreadystatechange = function() {
if (this.readyState == 4 /* complete */ ) {
req.onreadystatechange = null;
if (this.status == 200) {
var data = JSON.parse(this.response);
if(data!=null && data.Attributes!=null){
var AllAttributesCollection = data.Attributes;
var attributesCollection = [];
for(var i = 0; i <AllAttributesCollection.length; i++ ){
if(AllAttributesCollection[i].DisplayName && AllAttributesCollection[i].DisplayName.UserLocalizedLabel){
attributesCollection[attributesCollection.length] = AllAttributesCollection[i];
}
}
attributesCollection.sort(function (a, b) {
if (a.DisplayName.LocalizedLabels[0].Label < b.DisplayName.LocalizedLabels[0].Label)
{ return -1 }
if (a.DisplayName.LocalizedLabels[0].Label> b.DisplayName.LocalizedLabels[0].Label)
{ return 1 }
return 0;
});
var formxml = parent.Xrm.Page.getAttribute("new_formxml");
for (var i = 0; i < attributesCollection.length; i++) {
var attribute = attributesCollection[i];
if(formxml && formxml.getValue() != null)
if($("#ddlattribute").length!=0){
attribute.LogicalName
if(attribute.DisplayName.UserLocalizedLabel!=null && attribute.LogicalName!=null && formxml.getValue().search(attribute.LogicalName) !== -1){
$("#ddlattribute").append("<option value='"+attribute.LogicalName+"'>"+attribute.DisplayName.LocalizedLabels[0].Label+"</option>");
}
}
}
}
}
else {
var error = JSON.parse(this.response).error;
alert(error.message);
}
}
};
req.send();
}
window.onload = retrieveEntity;
</script>
<meta></head>
<body style='margin: 0px;'>
<table height='1%' class='ms-crm-FormSection' id='{91951bf5-186c-4bb4-8d0b-80c807212b46}' style='table-layout: fixed;' cellspacing='0' cellpadding='3' valign='top' isviewportsection='0' label='General' columns='2' control='[object Object]'>
<colgroup>
<col width='120'>
<col>
<col width='140' class='FormSection_WriteColumnHeaders_col'>
<col>
</colgroup><tbody>
<tr valign='top'>
<td title='Entity' class='ms-crm-FieldLabel-LeftAlign ms-crm-Field-Normal' id='ddlallentity_c' style='text-align: left; color: rgb(59, 59, 59); font-size: 11px;'><label for='ddlallentity'>Entity</label></td>
<td width='300' id='ddlallentity_d' formxmlcolspan='1' isautoexpanding='FALSE'><select name='ddlallentity' tabindex='1790' class='ms-crm-SelectBox ' id='ddlallentity' style='float: left; -ms-ime-mode: auto; min-width: 100px;' onchange='setLogicalNameofEntity()' defaultselected='-1' req='0' attrname='ddlallentity' attrpriv='7'></select></td>
<td title='Form Name' class='ms-crm-FieldLabel-LeftAlign ms-crm-Field-Normal' id='ddlallForms_c' style='text-align: left; color: rgb(59, 59, 59); font-size: 11px;'><label for='ddlallForms'>Form Name</label></td>
<td width='300' id='ddlallForms_d' formxmlcolspan='1' isautoexpanding='FALSE'><select name='ddlallForms' tabindex='1790' class='ms-crm-SelectBox ' id='ddlallForms' style='float: left; -ms-ime-mode: auto; min-width: 100px;' onchange='setFormNameofEntity()' defaultselected='-1' req='0' attrname='ddlallForms' attrpriv='7'></select></td>
<td title='Field Name' class='ms-crm-FieldLabel-LeftAlign ms-crm-Field-Normal' id='ddlattribute_c' style='color: rgb(59, 59, 59); padding-left: 20px; font-size: 11px;'><label for='ddlattribute'>Field Name</label></td>
<td id='ddlattribute_d' formxmlcolspan='1' isautoexpanding='FALSE'><select name='ddlattribute' tabindex='1880' class='ms-crm-SelectBox ' id='ddlattribute' style='-ms-ime-mode: auto; min-width: 100px;' onchange='setIdOfAttribute()' defaultselected='-1' req='0' attrname='ddlattribute' attrpriv='7'></select></td></tr></tbody></table>
<div id='dynamicForm' style='font-family: undefined;'></div>
<div id='iframediv' style='font-family: undefined; display: none;'><iframe id='MyFormIframe'>
</iframe></div></body></html>