Skip to main content

Notifications

Microsoft Dynamics CRM (Archived)

How to get records using FetchXml Paging Cookie using Java Script in Dynamics 365?

Posted on by Microsoft Employee

Hello,

Here I'm getting appointment using fetchxml, but it gives me only 5000 records. Now I want to get records using paging cookie in javascript.
function getAppointment() {

            var appointmentQueryValue = "";
            appointmentQueryValue += '<entity name="appointment">';
            appointmentQueryValue += '<attribute name="subject" />';
            appointmentQueryValue += '</entity>';

            var finalData = [];
            geRecordsFetchXML("appointments", appointmentQueryValue, finalData);
        }

        function geRecordsFetchXML(entityName, ODataUrl, finalData) {
                
                var fetchQuery = '';
                var req = new XMLHttpRequest();

                fetchQuery = ['<fetch mapping="logical" output-format="xml-platform" version="1.0">', ODataUrl, '</fetch>'].join('');
                fetchQuery = escape(fetchQuery);

                req.open('GET', serverUrl + '/api/data/v8.0/' + entityName + '?fetchXml=' + fetchQuery, true);
                req.setRequestHeader("OData-MaxVersion", "4.0");
                req.setRequestHeader("OData-Version", "4.0");
                req.setRequestHeader("Accept", "application/json");
                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) {
                                for (var i = 0; i < data.value.length; i++) {
                                    finalData.push(data.value[i]);
                                }
                            }


                            if (data["@Microsoft.Dynamics.CRM.fetchxmlpagingcookie"] != undefined &&
                                data["@Microsoft.Dynamics.CRM.fetchxmlpagingcookie"] != null
                                && data["@Microsoft.Dynamics.CRM.fetchxmlpagingcookie"] != "") {

                               //next link
                            }
                            else {
                                callbackFetchXml(finalData);
                                //my all records
                            }
                        }
                        else {
                            var error = JSON.parse(this.response).error;
                            alert("error " + error.message);
                        }
                    }
                };
                req.send();
        }
Please suggest me how to get next records using paging cookie?

*This post is locked for comments

  • Suggested answer
    Kjeld Poulsen Profile Picture
    Kjeld Poulsen 180 on at
    RE: How to get records using FetchXml Paging Cookie using Java Script in Dynamics 365?

    I had same challange, using fetchxml with webapi, and finally found the solution:

    Assuming you have a response from your first request, with a blank paging-cookie, and if more pages are found, you can get the paging cookie from "@Microsoft.Dynamics.CRM.fetchxmlpagingcookie"

    remember to add the this http header to get that cat:

    headers = headers.append("Prefer", "odata.include-annotations=\"*\"");

    and below the type-/javascript to extract the pagingcookie and add it to the next fetchxml:

    var fragment = "";

    var pageCookie = response["@Microsoft.Dynamics.CRM.fetchxmlpagingcookie"] as string;

    if (pageCookie != null && pageCookie != '') {

    let pageCookieMatch = /(?<=pagingcookie=['"])[a-z,A-Z,0-9,%-_.~]+/g;      

           fragment = decodeURIComponent(decodeURIComponent(pageCookie.match(this.pageCookieMatch)[0]));

           fragment = fragment.replace(/&/g,"&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/\"/g, "&quot;");

    }

    let fetchxml = "<fetchxml mapping='logical' paging-cookie='"+fragment+"' page='"+ nextpage +"' count='"+pagesize+"'>...</fetchxml>

  • Suggested answer
    gdas Profile Picture
    gdas 50,085 on at
    RE: How to get records using FetchXml Paging Cookie using Java Script in Dynamics 365?

    You can have a look  below thread -

    community.dynamics.com/.../304574

  • Suggested answer
    Sreevalli Profile Picture
    Sreevalli 3,256 on at
    RE: How to get records using FetchXml Paging Cookie using Java Script in Dynamics 365?

    Hay Chhaya,

    As per the authors post in the comments the issues is occurring due to a plugin on the same entity, from the link(www.inogic.com/.../execute-fetchxml-using-web-api-in-dynamics-crm-2016) share by Goutam.

    Copy of post

    Recently, we also came across the same kind of issue, where when we tried to retrieve more than 5K records of Contact Entity, it returned the same paging cookie all the time. Due to this, it retrieved the same records again and again and the process went into a recursive loop that never ended. As we worked on this, we found that it occurred because we have created a field on the Contact Entity and the value in this field gets populated by the plug-in. We have registered the plugin on the Retrieve Multiple message which reads value from other Entity and sets into this field that is shown to the user, but we do not save the value of this field in database. This field is like a virtual field, which may be the reason that it gives the same paging cookie each time. If we remove this field from fetchxml and execute using Web API, it starts working.

    Do you have any such fields in your fetch?

  • Subhash_Mahato Profile Picture
    Subhash_Mahato 147 on at
    RE: How to get records using FetchXml Paging Cookie using Java Script in Dynamics 365?

    hi 

    I am getting the same issue with the fetchxml query, it is going in recursive loop.

    Thanks & regards

    Subhash Mahato

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: How to get records using FetchXml Paging Cookie using Java Script in Dynamics 365?

    Hi Goutam,

    I get same page cookies in FetchMore() function, So it is going in recursive loop.

    Please help me to solve this issue.

    fetchMore: function (originalFetch, entitySetName, retrieveUsingFetchSucess, errorCallback, pageCookies, pageNumber, entityObj) {
     
    
    //pageCookies
    
    }


  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: How to get records using FetchXml Paging Cookie using Java Script in Dynamics 365?

    Thanks Goutam!

    I have already tried that solution. but It gives me same page number in fetchMore() function.

    Can you please tell me that why It gives me same Page Number?

  • Suggested answer
    Ben Thompson Profile Picture
    Ben Thompson 6,350 on at
    RE: How to get records using FetchXml Paging Cookie using Java Script in Dynamics 365?

    Goutam provided a link to a blog post that answers this in your duplicate question at community.dynamics.com/.../306157

  • Suggested answer
    gdas Profile Picture
    gdas 50,085 on at
    RE: How to get records using FetchXml Paging Cookie using Java Script in Dynamics 365?

    Hi,

    Please have  a look below reference -

    www.inogic.com/.../execute-fetchxml-using-web-api-in-dynamics-crm-2016

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    How to get records using FetchXml Paging Cookie using Java Script in Dynamics 365?
    Hello,
    Here I'm getting appointment using fetchxml, but it gives me only 5000 records. Now I want to get records using paging cookie in javascript.
    
    
    function getAppointment() {
    
                var appointmentQueryValue = "";
                appointmentQueryValue += '<entity name="appointment">';
                appointmentQueryValue += '<attribute name="subject" />';
                appointmentQueryValue += '</entity>';
    
                var finalData = [];
                geRecordsFetchXML("appointments", appointmentQueryValue, finalData);
            }
    
            function geRecordsFetchXML(entityName, ODataUrl, finalData) {
                    
                    var fetchQuery = '';
                    var req = new XMLHttpRequest();
    
                    fetchQuery = ['<fetch mapping="logical" output-format="xml-platform" version="1.0">', ODataUrl, '</fetch>'].join('');
                    fetchQuery = escape(fetchQuery);
    
                    req.open('GET', serverUrl + '/api/data/v8.0/' + entityName + '?fetchXml=' + fetchQuery, true);
                    req.setRequestHeader("OData-MaxVersion", "4.0");
                    req.setRequestHeader("OData-Version", "4.0");
                    req.setRequestHeader("Accept", "application/json");
                    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) {
                                    for (var i = 0; i < data.value.length; i++) {
                                        finalData.push(data.value[i]);
                                    }
                                }
    
    
                                if (data["@Microsoft.Dynamics.CRM.fetchxmlpagingcookie"] != undefined &&
                                    data["@Microsoft.Dynamics.CRM.fetchxmlpagingcookie"] != null
                                    && data["@Microsoft.Dynamics.CRM.fetchxmlpagingcookie"] != "") {
    
                                   //next link
                                }
                                else {
                                    callbackFetchXml(finalData);
                                    //my all records
                                }
                            }
                            else {
                                var error = JSON.parse(this.response).error;
                                alert("error " + error.message);
                            }
                        }
                    };
                    req.send();
            }

    Can you please suggest me how to get next records ?

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!

Community AMA December 12th

Join us as we continue to demystify the Dynamics 365 Contact Center

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,149 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans