I need to get a list of folders and sub folders from document library. I feel that approaching this problem using REST call would be easier than the traditional CSOM query. Please suggest a relevant approach and solution.
2 Answers
You could utilize GetItems method and specify the following query:
<View Scope="RecursiveAll">
<Query>
<Where>
<Eq>
<FieldRef Name="FSObjType" /><Value Type="Integer">1</Value>
</Eq>
</Where>
</Query>
</View>
to return all folder items in a library.
Example
function getListItems(webUrl,listTitle, viewXml)
{
var url = webUrl + "/_api/web/lists/getbytitle('" + listTitle + "')/getitems";
var queryPayload = {
'query' : {
'__metadata': { 'type': 'SP.CamlQuery' },
'ViewXml' : viewXml
}
};
return $.ajax({
url: url,
method: "POST",
contentType: "application/json;odata=verbose",
data: JSON.stringify(queryPayload),
headers: {
"X-RequestDigest": $("#__REQUESTDIGEST").val(),
"Accept": "application/json; odata=verbose"
}
});
}
function getAllFolderItems(webUrl,listTitle)
{
var viewXml = '<View Scope="RecursiveAll"><Query><Where><Eq><FieldRef Name="FSObjType" /><Value Type="Integer">1</Value></Eq></Where></Query></View>';
return getListItems(webUrl,listTitle,viewXml);
}
Usage
//print folder names from Documents library
getAllFolderItems(_spPageContextInfo.webAbsoluteUrl,'Documents')
.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 am unable to get the folder and sub folder names. When I added this code it returned me object. I don't know how to go ahead now. Can you please suggest me the next steps. Apr 30, 2015 at 11:20
Recently I answered a very similar question here. If you need all of the folders and subfolders (but no documents) recursively from a document library, that is what you should use (send it via POST to the server due to GetItems
method call):
/_api/web/Lists/GetByTitle('Title of your doc. lib.')/GetItems(query=@v1)?$expand=Folder&$select=Title,Folder/ServerRelativeUrl&@v1={"ViewXml":"<View Scope='RecursiveAll'><Query><Where><Eq><FieldRef Name='FSObjType' /><Value Type='Integer'>1</Value></Eq></Where></Query></View>"}
Alternatively, you could use the following REST query as well (via HTTP GET):
/_api/web/Lists/GetByTitle('Title of your doc. lib.')/Items?$expand=Folder&$select=Title,Folder/ServerRelativeUrl
It will return all of the items (documents and folders) in you library, you should filter them on the client side based on the FileSystemObjectType
property, as you can't use this one in the $filter
expression in REST (see details here).
Last, but not least, you can use the SharePoint 2010 compatible REST Interface as shown in option 3:
/_vti_bin/listdata.svc/YourDocLib?$filter=ContentType eq 'Folder'
Note, that the syntax is language-specific in this case. The query above is OK for English, but for German it would be:
/_vti_bin/listdata.svc/YourDocLib?$filter=Inhaltstyp eq 'Ordner'