Sharepoint - REST with $filter for List Items in Sharepoint 2013

It does not seem possible to filter list items using FileSystemObjectType property in SharePoint 2013 REST.

Option 1

But you could utilize the following REST query to return folders and associated list items:

/_api/web/lists/Lists(guid'GUID')/rootFolder/Folders?$expand=ListItemAllFields

JavaScript example

var listTitle = "Documents"; 
var url = "/_api/web/lists/getByTitle('" + listTitle + "')/rootFolder/Folders?$expand=ListItemAllFields";
$.getJSON(url, function(data) {
    //print folders
    $(data.value).each(function(i,folder){
         var folderName = folder.Name;
         var folderItem = folder.ListItemAllFields; //get asssociated List Item with a Folder
    });
});

Update

In order to exclude "system" folders like Attachments you could apply an additional filter like demonstrated below:

/_api/web/lists/getByTitle('list title')/rootFolder/Folders?$expand=ListItemAllFields&$filter=Name ne 'Attachments' and Name ne 'Item' and Name ne 'Forms'

Option 2

Another approach would be to apply the filtering by Folder via CAML query as demonstrated below:

function getListItems(listTitle, queryViewXml)
{
   var queryPayload = { 
       'query':{ 
           '__metadata': { 'type': 'SP.CamlQuery' },
           'ViewXml': queryViewXml
        } 
   }; 
   var endpointUrl = _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('" + listTitle + "')/getitems";

   return $.ajax({
      type: "POST", 
      headers: { 
            "accept": "application/json;odata=verbose",
            "content-type": "application/json;odata=verbose",
            "X-RequestDigest": $("#__REQUESTDIGEST").val()
      }, 
      data: JSON.stringify(queryPayload),
      url: endpointUrl
   });
}

function getFolderItems(listTitle)
{
    var query = '<View><Query><Where><Eq><FieldRef Name="FSObjType" /><Value Type="Integer">1</Value></Eq></Where></Query></View>';
    return getListItems(listTitle,query);
}

Usage

//retrieve and print folder item names
getFolderItems('Requests')
.done(function(data){
    var items = data.d.results;
    for(var i = 0; i < items.length; i++) {
          console.log(items[i].Title);   
    }
})
.fail(function(error){
    console.log(JSON.stringify(error)); 
});

Option 3

Utilize SharePoint 2010 REST Interface, in particular the following query returns folder items:

/_vti_bin/listdata.svc/Requests?$filter=ContentType eq 'Folder'

where Requests is the name of a List


To return files use:

_api/web/lists/getbytitle('Documents')/items?$filter=startswith(ContentTypeId, '0x0101')

To return folders use:

_api/web/lists/getbytitle('Documents')/items?$filter=startswith(ContentTypeId, '0x0120')

I have tested this against SP2013 Online (O365), it should work against SP2010 as well. This works against all child content types as the content type id should inherit from the parent as explained in the MSDN article 'Content Type IDs'


I'm working with DocSets and they are a "folder" type. I'm using

url: webURL + "/_api/web/lists/getbytitle('" + listName + "')/items?$filter=FSObjType eq 1"

and that is working just fine for me. Before I added that on it was giving me both the docsets and their contents (the contents have FSObjType of 0) and after I added the filter I only have the DocSets and not the children.

Tags:

Rest

Filter