In one of my article we saw how to get SharePoint folders of root folder of document library using SharePoint REST Services. In this article we will see how to get folders within folders. This scenario is helpful to build up folder structure of library.
Following is the complete SharePoint REST URL :
"/_api/web/GetFolderByServerRelativeUrl('/testdoc/folder1')/folders"
All you need to pass here is the path of folder. Then SharePoint REST service returns the list of folders within that folder.
Complete sample code:
<html>
<head>
<script src="/Site%20Assets/js/jquery-1.11.1.min.js"></script>
<script>
function GetAllFoldersofRootFolders()
{
$.ajax(
{
url: "/_api/web/GetFolderByServerRelativeUrl('/testdoc/folder1')/folders",
method: "GET",
headers: {
"ACCEPT": "application/json;odata=verbose",
"content-type": "application/json;odata=verbose"
},
success: function (data) {
var htmlStr = "";
$.each(data.d.results, function(){
if(htmlStr === "")
{
htmlStr = "<li 'ServerRelativeUrl="+ this.ServerRelativeUrl + "'>" + this.Name + "(" + this.ItemCount + ")</li>";
}
else
{
htmlStr = htmlStr + "<li 'ServerRelativeUrl="+ this.ServerRelativeUrl + "'>" + this.Name + "(" + this.ItemCount + ")</li>";
}
});
$("#foldersDiv").html(htmlStr);
},
error: function (data) {
alert("Error occured." + data);
}
});
}
</script>
</head>
<body>
<div>
<input type="submit" onclick="GetAllFoldersofRootFolders()" value="Get All Folders of folder"> </input>
</div>
<div>
<ul id="foldersDiv">
</ul>
</div>
</body>
</html>
Following is the complete SharePoint REST URL :
"/_api/web/GetFolderByServerRelativeUrl('/testdoc/folder1')/folders"
All you need to pass here is the path of folder. Then SharePoint REST service returns the list of folders within that folder.
Complete sample code:
<html>
<head>
<script src="/Site%20Assets/js/jquery-1.11.1.min.js"></script>
<script>
function GetAllFoldersofRootFolders()
{
$.ajax(
{
url: "/_api/web/GetFolderByServerRelativeUrl('/testdoc/folder1')/folders",
method: "GET",
headers: {
"ACCEPT": "application/json;odata=verbose",
"content-type": "application/json;odata=verbose"
},
success: function (data) {
var htmlStr = "";
$.each(data.d.results, function(){
if(htmlStr === "")
{
htmlStr = "<li 'ServerRelativeUrl="+ this.ServerRelativeUrl + "'>" + this.Name + "(" + this.ItemCount + ")</li>";
}
else
{
htmlStr = htmlStr + "<li 'ServerRelativeUrl="+ this.ServerRelativeUrl + "'>" + this.Name + "(" + this.ItemCount + ")</li>";
}
});
$("#foldersDiv").html(htmlStr);
},
error: function (data) {
alert("Error occured." + data);
}
});
}
</script>
</head>
<body>
<div>
<input type="submit" onclick="GetAllFoldersofRootFolders()" value="Get All Folders of folder"> </input>
</div>
<div>
<ul id="foldersDiv">
</ul>
</div>
</body>
</html>
No comments:
Post a Comment