Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Retrieve CRM Entity Metadata and Fields information in HTML Page .

(0) ShareShare
ReportReport
Posted on by 233

Hello,

<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.12.4.min.js" type="text/javascript"></script>

<script type="text/javascript">

function retrieveEntity() {
debugger;
var serverURL = parent.Xrm.Page.context.getClientUrl();
var Query = "EntityDefinitions?$select=DisplayName,EntitySetName,ObjectTypeCode";
var req = new XMLHttpRequest();
req.open("GET", serverURL + "/api/data/v9.1/" + 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() {
debugger;
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.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($("#ddlattribute").length!=0){
$("#ddlattribute").empty()
}

});
}

}
}
} else {
var error = JSON.parse(this.response).error;
alert(error.message);
}
}
};
req.send();
}

function setLogicalNameofEntity(){
debugger;
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];
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'
});

}
$('#dynamicForm').empty();

retrieveAttributes(attributesRetrieved,entityMetadataId);
}


function retrieveAttributes(attributesRetrieved,entityMetadataId) {
debugger;
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/v9.1/" + 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() {
debugger;
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) {
debugger;
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 < attributesCollection.length; i++) {

var attribute = attributesCollection[i];
if($("#ddlattribute").length!=0){
if(attribute.DisplayName.UserLocalizedLabel!=null){
$("#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" control="[object Object]" columns="2" label="General" isviewportsection="0">
<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; font-size: 11px; color: #3b3b3b;"><label for="ddlallentity">Entity</label></td>
<td width="300" id="ddlallentity_d" isautoexpanding="FALSE" formxmlcolspan="1"><select name="ddlallentity" tabindex="1790" class="ms-crm-SelectBox " id="ddlallentity" style="float: left; -ms-ime-mode: auto; min-width: 100px;" onchange="setLogicalNameofEntity()" attrpriv="7" attrname="ddlallentity" req="0" defaultselected="-1"></select></td>
<td title="Field Name" class="ms-crm-FieldLabel-LeftAlign ms-crm-Field-Normal" id="ddlattribute_c" style="padding-left: 20px; font-size: 11px; color: #3b3b3b;"><label for="ddlattribute">Field Name</label></td>
<td id="ddlattribute_d" isautoexpanding="FALSE" formxmlcolspan="1"><select name="ddlattribute" tabindex="1880" class="ms-crm-SelectBox " id="ddlattribute" style="-ms-ime-mode: auto; min-width: 100px;" attrpriv="7" attrname="ddlattribute" req="0" defaultselected="-1"></select></td></tr></tbody></table>
<div id="dynamicForm"></div>
<div id="iframediv" style="display: none;"><iframe id="MyFormIframe">
</iframe></div></body></html>

I tried the above code,i am getting only the entity name in the dropdown  based on the entity name not able to filter the field name,through this code how to filter the Attribute based on the entity,please suggest me.

*This post is locked for comments

  • Suggested answer
    Dynamics365 Rocker Profile Picture
    7,755 on at
    RE: Retrieve CRM Entity Metadata and Fields information in HTML Page .

    Below post may help you:

    community.dynamics.com/.../224191

  • Amrutha B K Profile Picture
    233 on at
    RE: Retrieve CRM Entity Metadata and Fields information in HTML Page .

    I have a requirement ,to show CRM Entity Metadata to show all Entities (Display Names) in a drop down and based on entity selection, it should show the list of fields (Display Names) in HTML form.

    Example: Entities (Dropdown) - Contact

    -------------------------------------------------

    First Name

    Last Name

    etc.,

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

🌸 Community Spring Festival 2025 Challenge Winners! 🌸

Congratulations to all our community participants!

Adis Hodzic – Community Spotlight

We are honored to recognize Adis Hodzic as our May 2025 Community…

Kudos to the April Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard > Microsoft Dynamics CRM (Archived)

#1
Mohamed Amine Mahmoudi Profile Picture

Mohamed Amine Mahmoudi 83 Super User 2025 Season 1

#2
Community Member Profile Picture

Community Member 52

#3
dkrishna Profile Picture

dkrishna 6

Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans