2

I have a list that has folders, e.g.

/sites/mysite/Lists/Useful Links/My Subfolder/

I want to read all list items that belong to the My Subfolder, but I didn't find a way to do it.

I can read the My Subfolder itself (e.g. obtain its ID and Title) using:

/sites/mysite/_api/web/lists/getbytitle('Useful Links')/items?$filter=Title eq 'My Subfolder'

However, I can't figure out how to use this information to get the items that belong to this folder.

The returned data contains properties like File, Folder, but they don't contain anything like Items.

Alternatively, I was thinking whether a list item has any property to indicate which folder it belongs to, but I couldn't find it either. Something like ParentFolderId - if I had that, I could select all items from the list and $filter them by that ParentFolderId.

5

Found a hacky way, using substring of FileRef:

/sites/mysite/_api/web/lists/getbytitle('Useful Links')/items?$filter=substringof('Useful Links/My Folder/', FileRef)

Or, with sp-rest-api.js:

var api = new SpRestApi({
    listTitle: 'Useful Links',
    onsuccess: console.log,
});

api.getAllItemsFromListSubfolder('My Folder');
3

I cover this in my blog post SharePoint REST API and Lists with Folders

There are a couple ways you can get the items in a folder. You can get the Files collection for the folder and expand ListItemAllFields or you can use a CAML query. I'm an old school SharePoint developer so I prefer to use a CAML query. The key is setting the FolderServerRelativeUrl property in the query to restrict the results to the target folder.

In the sample below I'm assuming that there are only items in the folder. If the folder had child folders I would need to filter them out if I didn't want to process them.

function getItemsInFolder() {
    var appUrl = GetUrlKeyValue("SPAppWebUrl");
    var hostUrl = GetUrlKeyValue("SPHostUrl");

    var message = jQuery("#message");
    message.text("Working on it...");

    var url = appUrl + "/_api/SP.AppContextSite(@target)" +
        "/Web/Lists/getByTitle('FolderTest')/getItems?$select=*,FileDirRef&" +
        "@target='" + hostUrl + "'";

    var call = jQuery.ajax({
        url: url,
        type: "POST",
        data: JSON.stringify({
            query: {
                "__metadata": { type: "SP.CamlQuery" },
                ViewXml: "<View></View>",
                FolderServerRelativeUrl: hostUrl + "/Lists/FolderTest/Folder 1"
            }
        }),
        headers: {
            Accept: "application/json;odata=verbose",
            "Content-Type": "application/json;odata=verbose",
            "X-RequestDigest": jQuery("#__REQUESTDIGEST").val()
        }
    });
    call.done(function (data, textStatus, jqXHR) {
        message.text("All files in Folder 1:");
        message.append("<br/>");
        jQuery.each(data.d.results, function (index, item) {
            message.append(String.format(
                "Title: {0};  Number: {1};  Id: {2}; Path: {3}",
                item["Title"], item["Number"], item["Id"], item["FileDirRef"]));
            message.append("<br/>");
        });
    });
    call.fail(failHandler);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.