Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for fetchxmlpagingcookie when executing a fetchXML query #49

Open
KhashRastin opened this issue Oct 17, 2017 · 7 comments
Open

Comments

@KhashRastin
Copy link

Request support for the annotation Microsoft.Dynamics.CRM.fetchxmlpagingcookie to enable paging for fetchxml queries.

@davidyack
Copy link
Owner

Thanks for adding this to the list

@SolzIT
Copy link

SolzIT commented Aug 27, 2018

Hi David,
Any updates on this?

@EkoZuluDev
Copy link

EkoZuluDev commented Mar 27, 2019

I really am in need of the paging cookie support :-(
Is there a way to manually insert the pagingcookie annotation in fetchxml string?

@SolzIT
Copy link

SolzIT commented Mar 28, 2019 via email

@davidyack
Copy link
Owner

@SolzIT can you post the key changes here at least until it gets integrated others can take advantage of it

@SolzIT
Copy link

SolzIT commented Mar 30, 2019

Sure thing. Here you go. We had made the below changes to the CRMWebAPI.cs file and CRMGetListResult.cs file namely:

  1. Added a new method GetLargeList which is almost similar to GetList function but supports fetchxml based pagining cookie.
  2. Updated function FillPreferHeader to accommodate annotations in case of paging cookie.
  3. Updated CRMGetListResult.cs file to include paging cookie.

For the sake of clarity we have marked the changes by tag SolzIT.

To invoke the GetLargeList function
`
///


/// Get more than 5000 records
///

/// Fetch xml
/// Entity name
/// List of records
public async Task GetMoreThen5000Records(string querry, string entityName)
{
try
{
//Connect to crm
var api = await _crm.GetAPI();
dynamic finalResult = null;

            // Define the fetch attributes.
            // Set the number of records per page to retrieve.
            int fetchCount = 5000;
            // Initialize the page number.
            int pageNumber = 1;
            // Specify the current paging cookie. For retrieving the first page, 
            // pagingCookie should be null.
            string pagingCookie = null;

            while (true)
            {
                string xml = Helpers.CreateXml(querry, pagingCookie, pageNumber, fetchCount);

                dynamic result = await api.GetLargeList(entityName, QueryOptions: new CRMGetListOptions() { FetchXml = xml, FormattedValues = true });

                string count = result.List.Count.ToString();

                if (finalResult == null)
                    finalResult = result;
                else
                    finalResult.List.AddRange(result.List);

                if (result.PagingCookie != null)
                {
                    //Increment the page number to retrieve the next page. if count is equal to 5000
                    pageNumber++;
                    pagingCookie = result.PagingCookie;
                }
                else
                    //If count is less than result nodes, exit the loop.
                    break;
            }
            return finalResult;
        }
       
        catch (Exception Ex)
        {
           //Do exception handling               
        }
    }

`

Reference to the changed cs files is in attached zip. Please note this might not be the latest files as per Xrm.Tools.CRMWebAPI build
CRMWebAPI.zip

@mohsinonxrm
Copy link

@KhashRastin , I'll be opening a pull request in a couple of days (already coded, going to test a bit more) and submitting the changes for @davidyack to review and merge.

@SolzIT , I've looked at your sample and also a sample which Microsoft always provided since 4.0 days, I'm going to slightly modify that and not have the users do the looping, it'll just be a parameter and if you want to get all records, it'll return all records.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants