In one of my article we saw how to fetch files from SharePoint Root folders using SharePoint RESTservices. Now In this article we will see how to fetch the files from a particular folder using SharePoint REST services.
Following is the SharePoint REST API:
"/_api/web/GetFolderByServerRelativeUrl('/LIBRARY_NAME/FOLDER_NAME')/folders"
Following is the 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/testfolder')/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 + “</li>";
}
else
{
htmlStr = htmlStr + "<li 'ServerRelativeUrl="+ this.ServerRelativeUrl + "'>" + this.Name + “</li>";
}
});
$("#foldersDiv").html(htmlStr);
},
error: function (data) {
alert("Error occured." + data);
}
});
}
</script>
</head>
<body>
<div>
<input type="submit" onclick="GetAllFoldersofRootFolders()" value="Get All Files of folder"> </input>
</div>
<div>
<ul id="foldersDiv">
</ul>
</div>
</body>
</html>
Following is the SharePoint REST API:
"/_api/web/GetFolderByServerRelativeUrl('/LIBRARY_NAME/FOLDER_NAME')/folders"
Following is the 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/testfolder')/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 + “</li>";
}
else
{
htmlStr = htmlStr + "<li 'ServerRelativeUrl="+ this.ServerRelativeUrl + "'>" + this.Name + “</li>";
}
});
$("#foldersDiv").html(htmlStr);
},
error: function (data) {
alert("Error occured." + data);
}
});
}
</script>
</head>
<body>
<div>
<input type="submit" onclick="GetAllFoldersofRootFolders()" value="Get All Files of folder"> </input>
</div>
<div>
<ul id="foldersDiv">
</ul>
</div>
</body>
</html>
No comments:
Post a Comment