title: How can I retrieve all records (more than 5000 and/or more than 1 page) with the Dynamics 365 Web API in a Dynamics CRM organization with a C# plugin?
body: <p>I need to count all of the <strong>Task</strong> records with a <strong>regardingobjectid</strong> of a specific <strong>Account</strong> and a <strong>statecode</strong> of <strong>0</strong>. This needs to be done with the new 365 Web API and REST that came with Microsoft Dynamics CRM 2016. Here is how the basic query (without paging) is done with Javascript:</p>
  
  <pre><code>var req = new XMLHttpRequest();
  req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/tasks?$filter=_regardingobjectid_value eq 00000000-0000-0000-0000-000000000000 and  statecode eq 0", 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=\"*\",odata.maxpagesize=5000");
  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 &lt; results.value.length; i++) {
                  var activityid = results.value[i]["activityid"];
              }
          } else {
              Xrm.Utility.alertDialog(this.statusText);
          }
      }
  };
  req.send();
  </code></pre>
  
  <p>Furthermore, this must be done in a fashion that allows it to retrieve more than 5000 records. Information on how to do this can be found here: <a href="https://msdn.microsoft.com/en-us/library/gg334767.aspx" rel="nofollow noreferrer">https://msdn.microsoft.com/en-us/library/gg334767.aspx</a></p>
  
  <p>Here is how I could query up to 5000 records with a C# plugin and FETCH XML:</p>
  
  <pre><code>int count = 0;
              string fetchTemplate =
                  @"&lt;fetch version='1.0' output-format='xml - platform' mapping='logical' distinct='false'&gt;
                      &lt;entity name='activitypointer'&gt;
                          &lt;attribute name='activitytypecode' /&gt;
                          &lt;attribute name='subject' /&gt;
                          &lt;attribute name='prioritycode' /&gt;
                          &lt;attribute name='regardingobjectid' /&gt;
                          &lt;attribute name='statecode' /&gt;
                          &lt;attribute name='instancetypecode' /&gt;
                          &lt;attribute name='community' /&gt;
                          &lt;attribute name='scheduledend' /&gt;
                          &lt;attribute name='activityid' /&gt;
                          &lt;filter type='and'&gt;
                              &lt;condition attribute='isregularactivity' operator='eq' value='1' /&gt;
                              &lt;condition attribute='statecode' operator='eq' value='0' /&gt;
                          &lt;/filter&gt;    
                          &lt;link-entity name='account' from='accountid' to='regardingobjectid' alias='ab'&gt;
                              &lt;filter type='and'&gt;
                                  &lt;condition attribute='accountid' operator='eq' uitype='account' value='{0}' /&gt;
                              &lt;/filter&gt;
                          &lt;/link-entity&gt;
                      &lt;/entity&gt;
                  &lt;/fetch&gt;";
              fetchTemplate = String.Format(fetchTemplate, entityId.ToString());
              List&lt;Entity&gt; records = context.RetrieveAll(fetchTemplate);
              if (records != null &amp;&amp; records.Count &gt; 0)
              {
                  count = records.Count;
              }
  </code></pre>
  
  <p>Any help on this would be greatly appreciated. I am using Microsoft Dynamics CRM Online 2016, Visual Studio Professional 2015, relevant Nuget packages, and the Plugin Registration Tool.</p>
  
  <p>I have come to StackOverflow for help with this because I cannot find a simple example of a REST query online for Microsoft Dynamics CRM that can retrieve more than 5000 records in a C# plugin.</p>
  
  <p>I am particularly interested in the @odata.nextlink property and how this enables the restful retrieval of multiple pages (and potentially more than 5000 records).</p>
  
  <p>If you would like to help me enhance my current C# plugin code so that it can retrieve more than 5000 records, that would also be appreciated.</p>