1

I need to enum directories and documents in SP 2013. I need only those the user works with (no system or web files) To get initial lists omitting non-user ones I inquire

/_api/Web/Lists?$filter=BaseType+eq+1 and Hidden+eq+false and IsPrivate+eq+false and IsCatalog+eq+false

Than - what to do better? I have 2 options:

  1. Get /_api/Web/Lists(guid'GUID')/Items, chose those with FileSystemObjectType eq 0 for files and FileSystemObjectType eq 1 for folders, and then arrange folders into tree using relative paths (e.g. for folder1="/A/B" and folder2="/A/B/C" assume that folder2 lays inside folder1)

    PS: filtering with neither /_api/Web/Lists(guid'GUID')/Items?$filter=FileSystemObjectType eq 0 nor /_api/Web/Lists(guid'GUID')/Items?$filter=FileSystemObjectType+eq+0 does not work for me

  2. Get /_api/Web/Lists(guid'GUID')/RootFolder and continue navigating with

/_api/Web/GetFolderByServerRelativeUrl(FOLDER_PATH)/Folders,
/_api/Web/GetFolderByServerRelativeUrl(FOLDER_PATH)/Files and
/_api/Web/GetFileByServerRelativeUrl(FOLDER_PATH)

Any other option?

2-nd is easier but I doubt if there can be some non-user files inside the folders. If not - I would prefer 2.

Thank you

0

Based on the discussion in REST with $filter for List Items in Sharepoint 2013 question:

Option 1

Since filtering on FileSystemObjectType property does not seem to work using $filter query option in SharePoint 2013 REST,you could consider a another option, i.e to filter by FileSystemObjectType property using CAML query instead 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)); 
});

I would prefer this option since it allows to retrieve nested files/folders

Option 2

The limitation of this approach that it allows to return files/folders for one sub level only.

The following REST query returns top folders of Documents library:

/_api/web/lists/getByTitle('Documents')/rootFolder/Folders?$expand=ListItemAllFields&$filter=ListItemAllFields ne null

Filter: ListItemAllFields ne null is used to exclude system folders

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.