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

  • Verified answer
    Aric Levin - MVP Profile Picture
    30,188 Moderator on at
    RE: Retrieving records from CRM entities to an external html page using JavaScript Jquery

    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.

  • Community Member Profile Picture
    on at
    RE: Retrieving records from CRM entities to an external html page using JavaScript Jquery

    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,188 Moderator on at
    RE: Retrieving records from CRM entities to an external html page using JavaScript Jquery

    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
    RE: Retrieving records from CRM entities to an external html page using JavaScript Jquery

    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!
  • Community Member Profile Picture
    on at
    RE: Retrieving records from CRM entities to an external html page using JavaScript Jquery

    I'm using javascript to connect from my html page

  • Suggested answer
    Aric Levin - MVP Profile Picture
    30,188 Moderator on at
    RE: Retrieving records from CRM entities to an external html page using JavaScript Jquery

    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.

  • Suggested answer
    Rawish Kumar Profile Picture
    13,758 on at
    RE: Retrieving records from CRM entities to an external html page using JavaScript Jquery

    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#?

  • Community Member Profile Picture
    on at
    RE: Retrieving records from CRM entities to an external html page using JavaScript Jquery

    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
    RE: Retrieving records from CRM entities to an external html page using JavaScript Jquery

    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.

  • Suggested answer
    Rawish Kumar Profile Picture
    13,758 on at
    RE: Retrieving records from CRM entities to an external html page using JavaScript Jquery

    it would be :

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

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
Victor Onyebuchi Profile Picture

Victor Onyebuchi 6

Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans