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...

usingMicrosoft.SharePoint.Client;
usingNewtonsoft.Json;
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Net.Http;
usingSystem.Security;
usingSystem.Threading.Tasks;

namespacezPagingRestExample
{
    class Program
    {
        //Constants
        private const intPAGESIZE = 5000;
        private const stringUSERNAME = "[email protected]";
        private const string PWD = "wlamu$$a.3";
        private const stringSPSITE = "https://zsis.sharepoint.com/sites/TestFlow";

        //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;
            try
            {
                //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);
                result.Wait();

                //Display retrieved data
                Console.WriteLine(result.Result.Items.Count.ToString());
                Console.ReadLine();

                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
                    Console.WriteLine((string)item.Title);
                    Console.WriteLine((string)item.MyDate);
                }

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                Console.ReadLine();
            }
            finally
            {
                //Disposing Handler
                if(handler == null)
                {
                    handler.Dispose();
                    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
                client.DefaultRequestHeaders.Accept.Clear();

                //Use nometadata in order to reduce the message size
                //Take a look at this...
                //http://zsvipullo.blogspot.it/2016/10/sharepoint-rest-api-drastically-reduce.html
                client.DefaultRequestHeaders.Add("Accept", "application/json;odata=nometadata");

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

                //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);
                    res.Items.AddRange(nestedRes.Result.Items);
                }

                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));

            returnhandler;
        }
    }

    //Classes
    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

×