18

Sharepoint 2013, REST API, native C++ application: I want to get all Folder items from the list. Try

/_api/Web/Lists(guid'GUID')/Items?$filter=FileSystemObjectType+eq+1

and get HTTP 400 Bed request. Same for $filter=AuthorId+eq+1 and some other fields.
But e.g. $filter=Id+eq+1 or $filter=Modified+eq+'2014-11-01T16:13:49Z' works fine.
Any ideas?

1
  • Explore and Learn SharePoint REST Odata query ($select, $filter, $expand, $top with example): bit.ly/39Niz2i
    – anomepani
    Commented Apr 5, 2020 at 11:54

7 Answers 7

26

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

9
  • Thank you for answer, but it getting /Folders from the list root folder returns site-related items (e.g. Forms) while I need only those visible to user.
    – andrii
    Commented Dec 11, 2014 at 13:30
  • The answer has been updated, see the update section, in addition an alternative option has been added (option 2). Commented Dec 11, 2014 at 14:35
  • Well... Option 1 looks hacky Are 'Attachments', 'Item' and 'Forms' the only "system" folders which can appear? Can any other ones appear? What about "system" files to skip? Regarding Option 2 - can I do the same with C++? It looks like I do but anyway - what is assumed to be faster: posting Caml query or loading all the list items and filter them manually in C++?
    – andrii
    Commented Dec 11, 2014 at 15:08
  • Option 2 definitely is more clean solution since it returns only user-defined folders. I believe it is doable, see for example here: casablanca.codeplex.com/discussions/447077 Commented Dec 11, 2014 at 15:18
  • Regarding option 1, i'm afraid this is not the complete list of system folders, for example in Assets library contains also _t and _w folders.. Commented Dec 11, 2014 at 15:22
8

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'

2
  • 4
    By the way, I discovered that $filter can be very confusing because it is applied BEFORE conversion to JSON. If the field is boolean (such as EnableVersioning) you must use $filter=EnableVersioning eq true, but if your field an enum, such as Yes/No, you must use a number, such as $filter=CreditApplied eq 0. EVEN though the enum is mapped to boolean in JSON, you cannot treat it as boolean in the filter. Commented Aug 17, 2016 at 14:55
  • this solution return back thumbnail for every video object. Commented Aug 8, 2017 at 7:30
4

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.

4

this is what worked for me:

_api/web/lists/getbytitle('LISTNAME')/Items?$orderby=Created desc&$filter=FSObjType+eq+1

FileSystemObjectType seems not to work

2

If you're still trying to figure out the problem with AuthorID i believe it is referenced as Author/ID

here's a snippet of the parameters I use on my code for pulling up the tasks:

      var params = {
        URL:"/_api/Web/Lists/GetByTitle('tasks')/Items"
        , params:{
          1:1
          , $select:[
            'Id'
            ,'Title'
            ,'Author/Name'
            ,'Author/FirstName'
            ,'Author/LastName'
            ,'Author/ID'
            ,'Created'
            ,'Modified'
            ,'Status'
            ,'WorkflowOutcome'
            ,'Editor/Name'
            ,'Editor/FirstName'
            ,'Editor/LastName'
            ,'Editor/ID'
            ,'WorkflowItemId'
            ,'WorkflowListId'
            ,'WorkflowLink'
            ].join(",")
          , $expand:'Author/ID,Editor/ID'
          , $filter:"((Created ge datetime'"+$scope.autoApprove.date.start.value.toISOString()+"') and (Created le datetime'"+$scope.autoApprove.date.end.value.toISOString()+"'))"
//          , $filter:"((WorkflowOutcome eq 'Rejected'))"
        }};
0

best and easiest and fastest: here : by @john-m his profile

/_api/web/GetFolderByServerRelativeUrl('/Production_Library/Communication Management')/files

filter only the files exist in specific folder

1
  • 1
    depends on what is needed. this way will give even auxiliary files no interesting (and not visible) for user on SharePoint web interface
    – andrii
    Commented Oct 6, 2016 at 21:22
0

Use this approach:

(token, domain, "/_api/web/lists?" + URLEncoder.encode("$select=ID,Title&$filter=basetype ne 1&$orderby=title", "utf-8"));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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