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)

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

(0) ShareShare
ReportReport
Posted on by

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

I have the same question (0)
  • Community Member Profile Picture
    on at
    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 ?
  • Suggested answer
    gdas Profile Picture
    50,091 Moderator on at

    Hi,

    Please have  a look below reference -

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

  • Suggested answer
    Ben Thompson Profile Picture
    6,350 on at

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

  • Community Member Profile Picture
    on at

    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?

  • Community Member Profile Picture
    on at

    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
    
    }


  • Subhash_Mahato Profile Picture
    147 on at

    hi 

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

    Thanks & regards

    Subhash Mahato

  • Suggested answer
    Sreevalli Profile Picture
    3,256 on at

    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?

  • Suggested answer
    gdas Profile Picture
    50,091 Moderator on at

    You can have a look  below thread -

    community.dynamics.com/.../304574

  • Suggested answer
    Kjeld Poulsen Profile Picture
    180 on at

    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>

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