Get folder contents of large lists using REST

If you have a large lost with more items than the list view threshold you may want to use folders to scope the contents.

It’s possible to get just the contents of a folder using REST before applying a filter and cheat the threshold. The trick is to do a POST and set the CAML query for the view in the body. Additionally set the FolderServerRelativeUrl parameter to the server relative URL of the folder.

Using fetch:

        fetch(
            _spPageContextInfo.webAbsoluteUrl + "/_api/Web/Lists/getByTitle('Big Big List')/getItems",
            {
                method: 'POST',
                headers: {
                    'Accept': 'application/json; odata=verbose',
                    'content-type': 'application/json; odata=verbose',
                    "X-RequestDigest": jQuery("#__REQUESTDIGEST").val()
                },
                credentials: 'same-origin',
                body: JSON.stringify({
                    query: {
                        "__metadata": { type: "SP.CamlQuery" },
                        ViewXml: '<View><Query><Where><Eq><FieldRef Name="BigField" LookupId="TRUE"/><Value Type="Lookup">' + id + '</Value></Eq></Where></Query></View>',
                        FolderServerRelativeUrl: "/MySite/BigBigList/SubFolder/2015"
                    }
                })
            })
            .then(response => {
                return response.json();
            })
            .then(data => {
                if (data.d &amp;&amp; data.d.results) {
                    data.d.results.map(item => {
                        console.log(item.Title);
                    });
                }
            })
            .catch(err => {
                console.log(JSON.stringify(err));
            });

Leave a Reply

Your e-mail address will not be published. Required fields are marked *