web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Retrieving records from CRM entities to an external html page using JavaScript Jquery

(0) ShareShare
ReportReport
Posted on by

Hello,

Suppose I need to retrieve records from a CRM entity named Contact to an external page. I have been reading bunch of reference material but they're very confusing as I'm not very experienced at this. Can anyone show me how please?

Can I do something like this?

$.ajax({
        type: "GET",
        dataType: "json",
	crossDomain: true,
        url: 'what do I put here?'
        success: function(data) {
	    console.log(JSON.stringify(data))		
        },
        error: function(e){
            console.log(e);
        }
    });


Thank you!

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Rawish Kumar Profile Picture
    13,758 on at

    it would be :

    http://hosturl/OrgName/api/data/v8.1/entityname

  • Suggested answer
    Rawish Kumar Profile Picture
    13,758 on at

    it would be good if you start with CRM webapi - it should be quick and easy.

    Basically if you need retrieve contacts and select some field from it you would do it like below :

    var req = new XMLHttpRequest();

    req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.1/contacts?$select=fullname,jobtitle", 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 results = JSON.parse(this.response);

               for (var i = 0; i < results.value.length; i++) {

                   var fullname = results.value[i]["fullname"];

                   var jobtitle = results.value[i]["jobtitle"];

               }

           } else {

               Xrm.Utility.alertDialog(this.statusText);

           }

       }

    };

    req.send();

    you can tell me what exactly the data you need from CRM , the exact criteria , i will help you with the script.

    mark my answer a verified if helpful.

  • Community Member Profile Picture
    on at

    I just want the first name, last name and address of the records in Contact entity.

    From your above example, how do I pass authentication? since i'm accessing from an external html page.

  • Suggested answer
    Rawish Kumar Profile Picture
    13,758 on at

    than it will be just : /api/data/v8.1/contacts?$select=firstname,lastname,address1,address2 etc.

    and for authentication - please tell me how are you connecting to CRM from your webpage? javascirpt or C#?

  • Suggested answer
    Aric Levin - MVP Profile Picture
    30,190 Moderator on at

    You need to get an authentication token, and pass the token as part of the request header:

    req.setRequestHeader("Authorization", "Bearer: " + token);

    To get the token you need to authenticate against CRM.

    Not sure where you app is hosted, but you will need to redirect to CRM to authentication, or create an Azure app to handle the authentication and use delegation with CRM.

    I would try checking out the CRMWebAPI Tools by David Yack:

    github.com/.../Xrm.Tools.CRMWebAPI

    Tools contain an Authentication library that you can use to Authenticate against CRM using External Sources.

    Hope this helps.

  • Community Member Profile Picture
    on at

    I'm using javascript to connect from my html page

  • Community Member Profile Picture
    on at

    Right now my external html page is just on my local drive. I tried looking at David Yack's CRMWebAPI Tools, but I can't figure out how to use them.

    In David's usage example, 

    var apiconfig = { APIUrl: 'https://orgname.crm.dynamics.com/api/data/v8.0/', AccessToken: "<accesstoken>" };
    
    var crmAPI = new CRMWebAPI(apiconfig);


    Where do I get the access token? Can you elaborate on how I can "authenticate against CRM"?

    Thank you!
  • Verified answer
    Aric Levin - MVP Profile Picture
    30,190 Moderator on at

    I think you would have to familiarize yourself with Azure on how to get the Authentication Token.

    See the following post as a starting point.

    docs.microsoft.com/.../active-directory-protocols-oauth-code

    I will look and see if I have something that I can give you.

    Is your external page ASP.NET or MVC by any chance?

  • Community Member Profile Picture
    on at

    Nope. One more question, there is a CRMWebAPIMetadata.js in David's CRMWebAPI Tools. Since I can see all the data in each field by directly using this link on a browser h.ttp://hosturl/OrgName/api/data/v8.1/entityname Can I some how extract the data from that link with David's CRMWebAPIMetadata.js without authentication?

    Thank you!

  • Verified answer
    Aric Levin - MVP Profile Picture
    30,190 Moderator on at

    You can probably see it because you are already authenticated against CRM (whether in another browser window, cached or a token/refresh token). You should not be able to see anything if you are externally accessing CRM, and none of your browser windows are authenticated.

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans