Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

not fetching the records in to html

Posted on by Microsoft Employee

Hi Everyone,

Greetings for the day!!!

I need a help regarding fetching records to  html using webapi

I have written java-script code and the code is successfully executing but when it comes to html including i'm getting error please help on this.. See the below code and suggest me further where i have struck regarding the output. 

<!DOCTYPE html>

<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<link rel="stylesheet" href="cdnjs.cloudflare.com/.../font-awesome.min.css">
<script src="ClientGlobalContext.js.aspx" type="text/javascript"></script>
<script language="javascript" type="text/javascript">
function searchbutton() {
debugger;
var req = new XMLHttpRequest();
req.open("GET", window.parent.Xrm.Page.context.getClientUrl() + "/api/data/v9.0/accounts?$select=name", false);-->query is not reading the req.open method 
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 results = JSON.parse(this.response);
for (var i = 0; i < results.value.length; i++) {
//var accountid = results.value[i]["accountid"];
var name = results.value[i]["name"];
// console.log(name);
$("#tb").append("<tr><td>" + name + "</td></tr>");

}
} else {
Xrm.Utility.alertDialog(this.statusText);
}
}
};
req.send();
}
</script>


<body bgcolor="blue" onload="searchbutton()" style="word-wrap:break-word" >
<center><h1>Email formate Design</h1></center><br />
From : <input type="text" name="fromaddress" value="" align="left"><br />
To :
<select style="color : #fff " align="left">
<table id="tb" style="width:100%;">
<tbody>
<tr>
<th style="text-align:left">Name</th>

</tr>
</tbody>
</table>


</select>
<button onclick="searchbutton()" type="submit"><i class="fa fa-search"></i></button>
</body>
</head>

</html>

Regards,

Narendra

*This post is locked for comments

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: not fetching the records in to html

    Thank you Chetan for ur valuable Response

  • ChetanKhanapure Profile Picture
    ChetanKhanapure 81 User Group Leader on at
    RE: not fetching the records in to html

    You need to call the webapi get request through the Ajax. Below I have demonstrate the pic of code you can try this.

    function searchbutton() {

    $.ajax({
    type: "GET",
    contentType: "application/json; charset=utf-8",
    datatype: "json",
    url: Xrm.Page.context.getClientUrl() + "/api/data/v9.0/accounts?$select=name",
    beforeSend: function (XMLHttpRequest) {
    XMLHttpRequest.setRequestHeader("OData-MaxVersion", "4.0");
    XMLHttpRequest.setRequestHeader("OData-Version", "4.0");
    XMLHttpRequest.setRequestHeader("Accept", "application/json");
    XMLHttpRequest.setRequestHeader("Prefer", "odata.include-annotations=\"OData.Community.Display.V1.FormattedValue\"");
    },
    async: false,
    success: function (data, textStatus, xhr) {

           //result you will gets the Data from Account
           var result = data;
           //Rest of the Logic goes here Ex.
           $("#HtmlField").val(result["CRMField"]);
          },
          error: function (xhr, textStatus, errorThrown) {
        }
      });
    }

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,253 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,188 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans