Search This Blog

Tuesday, March 1, 2016

SharePoint 2013 working with file and folders: How to get folders of SharePoint library using REST services

SharePoint Libraries are the basic feature of SharePoint from its first release. Over the year there is so much change in SharePoint programming but the basic entities like lists and libraries remains unchanged. In SharePoint 2013 REST services are new way of programming. So lets see how to work with folders and files using SharePoint REST services. There will  be several articles I will publish regarding files and folders. In this article we will see how to get top folders of library.

Following is the REST URL you need to use :

"/_api/web/lists/getbytitle(LIBRARY_NAME')/RootFolder/folders"


In above REST you need to take care of certain things like appending 'folders' at the end of URL. The html method is 'GET' .

Complete Sample Code:

<html>
<head>
<script src="/Site%20Assets/js/jquery.min.js"></script>
<script> 
function GetAllFoldersofRootFolders()
{
                $.ajax(
                {
                url: "/_api/web/lists/getbytitle('test1')/RootFolder/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 Root folder"> </input>
</div>

<div>
                <ul id="foldersDiv">
                </ul>
               
</div>
</body>
</html>

To execute above code provide the correct name of existing library. This code returns folders of Root folder of SharePoint Library.

No comments:

Post a Comment