Saturday, February 21, 2015

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 :


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:

<script src="/Site%20Assets/js/jquery.min.js"></script>
function GetAllFoldersofRootFolders()
                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>";
                                                                htmlStr = htmlStr + "<li 'ServerRelativeUrl="+ this.ServerRelativeUrl + "'>"  + this.Name + "(" + this.ItemCount + ")</li>";
                error: function (data) {
                                alert("Error occured." + data);


                <input type="submit" onclick="GetAllFoldersofRootFolders()" value="Get All Folders of Root folder"> </input>

                <ul id="foldersDiv">

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

Happy SharePoint coding.


Post a Comment