Search This Blog

Tuesday, March 1, 2016

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

In last article we saw how to get top level folders. Now lets see how to get all SharePoint library files from the root folder or top level folders using SharePoint REST API. Using REST services you can get files of library. in this article we will see how to get files at top level or from Root folder. We will see how to fetch files within folders in coming article.

Following is the URL to get files :

"/_api/web/lists/getbytitle('testdoc')/RootFolder/files"

The only difference with folder URL is that it replaces 'folders' by 'files'.

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/lists/getbytitle('test1')/RootFolder/files",     
                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 Folders of Root folder"> </input>
</div>

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


No comments:

Post a Comment