Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Retrieve all SharePoint data using Rest API and Paging

Here an example of how retrieve SharePoint list data using Rest API and pagination.

It is a particularly useful when the list has a number of record greater than the SharePoint threshold limit (5.000 items): be careful when you use $filter statement because if the result of the filtered record is greater than the threshold, you get an error even if you are using the pagination.

This code can be used on every SharePoint list, thanks to the dynamic type of the de-serialized class, using the internal field name in order to access to the property value.
Between the lines the comments...


    class Program
        private const intPAGESIZE = 5000;
        private const stringUSERNAME = "[email protected]";
        private const string PWD = "wlamu$$a.3";
        private const stringSPSITE = "";

        //skiptoken=Paged=TRUE means that you need paging
        //$top means the number of record for each page
        private const stringRESTURL = "{0}/_api/web/lists/getbytitle('BigList')/items?&$skiptoken=Paged=TRUE&$top={1}&$select=Title, MyDate, Id";

        static voidMain(string[] args)
            HttpClientHandlerhandler = null;
                //Creating the REST url setting the right filter parameter
                //and the size of each page
                stringrestUrl = string.Format(RESTURL, SPSITE, PAGESIZE);

                //Getting authenticated handler using cookies
                //I ask to autenticate to SP only once
                //and use the same handler again
                handler = getClientHandler(SPSITE, USERNAME, PWD);

                //Getting data
                TaskSPData.Rootobject> result = getSPData(restUrl, handler);

                //Display retrieved data

                foreach (var item in result.Result.Items)
                    //You can use the internal name of the field!!!
                    //each item of the Items collection is a dynamic value
                    //then you can use this code for any SharePoint list

            catch (Exception ex)
                //Disposing Handler
                if(handler == null)
                    handler = null;

        //Private funcionts

        /// Returns all data specified in the restUrl parameter
        /// restUrl">
        /// handler">
        private static async TaskSPData.Rootobject> getSPData(string restUrl, HttpClientHandlerhandler)
            using (varclient = new HttpClient(handler))
                //Adding Header

                //Use nometadata in order to reduce the message size
                //Take a look at this...
                client.DefaultRequestHeaders.Add("Accept", "application/json;odata=nometadata");

                //Calling SP REST Service
                HttpResponseMessageresponse = await client.GetAsync(restUrl).ConfigureAwait(false);

                //Getting string JSON Content and parse it into a dynamic object
                stringjsonData = await response.Content.ReadAsStringAsync();
                //dynamic d = JObject.Parse(jsonData);

                //Deserialising json string into SPData.Rootobject object
                //Thanks to Newtonsoft.Json work
                SPData.Rootobject res = JsonConvert.DeserializeObjectSPData.Rootobject>(jsonData);

                //If odatanextLink is null, I reach the last page
                //otherwise i recursively call the getSPData function passing the new page rest url
                if (!string.IsNullOrWhiteSpace(res.OdatanextLink))
                    varnestedRes = getSPData(res.OdatanextLink, handler);

                return res;

        /// Returns an authenticated HttpClientHandler
        /// spSiteUrl">
        /// username">
        /// pwd">
        private static HttpClientHandlergetClientHandler(string spSiteUrl, stringusername, string pwd)
            //Creating Credentials
            var passWord = new SecureString();
            foreach (var c in pwd) passWord.AppendChar(c);
            var credential = new SharePointOnlineCredentials(username, passWord);

            HttpClientHandlerhandler = new HttpClientHandler() { Credentials = credential };

            //Getting authentication cookies
            Uri uri = new Uri(spSiteUrl);
            handler.CookieContainer.SetCookies(uri, credential.GetAuthenticationCookie(uri));


    public class SPData
        public class Rootobject
            [JsonProperty(PropertyName = "odata.nextLink")]
            public stringOdatanextLink { get; set; }

            [JsonProperty(PropertyName = "value")]
            public Listdynamic> Items { get; set; }

This post first appeared on ZSvipullo, please read the originial post: here

Share the post

Retrieve all SharePoint data using Rest API and Paging


Subscribe to Zsvipullo

Get updates delivered right to your inbox!

Thank you for your subscription