Solving the "Paging cookie is required" error

11 April 2016
Daniel Cai

When using FetchXML query to read data from Microsoft Dynamics CRM server, you might be following the SDK document to page through the query results in order to get all records that satisfy the query. You might run into a situation that even you have supplied the paging cookie by following the documentation, you are still receiving an error complaining "Paging cookie is required when trying to retrieve a set of records on any high pages".

You might be pulling your hair when this happens, since you would think you had done everything right, and you have followed the exact code sample provided in the SDK sample.

If you are wondering why, you are actually not alone. I have run into this error more than a couple of times while supporting our clients who use our CRM source component. With our CRM Source Component, we do page through the entire recordset based on the query provided, and we always use page-cookies in subsequent queries, so there should be no reason for this error to happen. I was pulling my hair too when I first saw this error. But I did spot something while looking at our client's query, which leads to a solution for the problem, so I thought it worth writing a blog post which could save your some research.

If you ever run into this, the first thing that you should check is to make sure that you have included the primary key field of the primary entity in the query, second thing that you want to check whether you have used a distinct operator in your query. Say that you have a query like the following, you would want to ask yourself if you really intend to do a distinct on a large number of CRM records (say anything more than the page size that you are using - we call it the Batch Size in our CRM Source Component).

<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="true">
	<entity name="account">
		<attribute name="name" />
		<attribute name="address1_line1" />
		<attribute name="address1_line2" />
		<attribute name="address1_line3" />
		<attribute name="address1_city" />
		<attribute name="address1_stateorprovince" />
		<attribute name="address1_postalcode" />
		<attribute name="telephone1" />
		<order attribute="name" descending="false" />
	</entity>
</fetch>

When the distinct operator is not used in a FetchXML query, CRM server would typically automatically add the primary key field to the query and return back the IDs, which will be used as part of the paging cookie for the next service call. But if the query contains a distinct operator, this will not happen, which is the reason that we are getting this error message.

With that in mind, the solution for the error is rather simple. We have a couple of options to fix the issue.

  • You can add the primary key field to the query. As your original intention is to do a distinct, you would want to ask yourself if adding the primary key field invalidates your original intention.
  • You can remove the distinct operator from your FetchXML query. Again, this is a different change compared to the above, you would want to ask yourself whether the new query would satisfy your requirements.

If it happens that you don't like either of the above options, you will have to program your paging code, so you check if MoreRecords is true and also the returned PagingCookie is not empty as the combined conditions to decide whether to navigate to the next page.

Hope this helps.

Archive

November 2024 3 October 2024 1 September 2024 1 August 2024 2 July 2024 1 June 2024 1 May 2024 1 April 2024 2 March 2024 2 February 2024 2 January 2024 2 December 2023 1 November 2023 1 October 2023 2 August 2023 1 July 2023 2 June 2023 1 May 2023 2 April 2023 1 March 2023 1 February 2023 1 January 2023 2 December 2022 1 November 2022 2 October 2022 2 September 2022 2 August 2022 2 July 2022 3 June 2022 2 May 2022 2 April 2022 3 March 2022 2 February 2022 1 January 2022 2 December 2021 1 October 2021 1 September 2021 2 August 2021 2 July 2021 2 June 2021 1 May 2021 1 April 2021 2 March 2021 2 February 2021 2 January 2021 2 December 2020 2 November 2020 4 October 2020 1 September 2020 3 August 2020 2 July 2020 1 June 2020 2 May 2020 1 April 2020 1 March 2020 1 February 2020 1 January 2020 1 December 2019 1 November 2019 1 October 2019 1 May 2019 1 February 2019 1 December 2018 2 November 2018 1 October 2018 4 September 2018 1 August 2018 1 July 2018 1 June 2018 3 April 2018 3 March 2018 3 February 2018 3 January 2018 2 December 2017 1 April 2017 1 March 2017 7 December 2016 1 November 2016 2 October 2016 1 September 2016 4 August 2016 1 June 2016 1 May 2016 3 April 2016 1 August 2015 1 April 2015 10 August 2014 1 July 2014 1 June 2014 2 May 2014 2 February 2014 1 January 2014 2 October 2013 1 September 2013 2 August 2013 2 June 2013 5 May 2013 2 March 2013 1 February 2013 1 January 2013 1 December 2012 2 November 2012 2 September 2012 2 July 2012 1 May 2012 3 April 2012 2 March 2012 2 January 2012 1

Tags