{"id":988,"date":"2018-09-25T15:44:22","date_gmt":"2018-09-25T10:14:22","guid":{"rendered":"https:\/\/blog.binarybits.net\/?p=988"},"modified":"2019-12-20T13:03:48","modified_gmt":"2019-12-20T07:33:48","slug":"get-folder-and-files-recursively-in-sharepoint-using-rest-call","status":"publish","type":"post","link":"https:\/\/blog.binarybits.net\/get-folder-and-files-recursively-in-sharepoint-using-rest-call\/","title":{"rendered":"Get folder and files recursively in SharePoint using REST call"},"content":{"rendered":"\n
There was a simple requirement I came across for which a page should list all the files and folders in a SharePoint document library.<\/p>\n\n\n\n
I came up with a solution using ODATA call. Following is the REST call I made to get the list of all folder and files.<\/p>\n\n\n\n
\/_api\/web\/Lists\/GetByTitle(‘Documents’)\/Items?$select=FileLeafRef,FileRef&$orderby=FileRef asc<\/p><\/blockquote>\n\n\n\n
Following is the code which quickly prints them out in a table. Just add this code to a script editor web-part.
\nPlease make sure the URL is updated based on your site URL.<\/p>\n\n\n\n<script src="\/\/ajax.googleapis.com\/ajax\/libs\/jquery\/1\/jquery.min.js"><\/script>\n<script type="text\/javascript">\n $.ajax({\n url: "<site url>\/_api\/web\/Lists\/GetByTitle('Documents')\/Items?$select=FileLeafRef,FileRef&$orderby=FileRef asc",\n type: "GET",\n headers: {\n "Accept": "application\/json;odata=verbose"\n },\n success: function (data, textStatus, xhr)\n {\n $.each(data.d.results, function (index, item)\n {\n \/\/ alert("The items in list are : " + item.FileRef);\n $("#table1 tbody").append("<tr><td>" + item.FileRef + "<\/td><\/tr>");\n })\n },\n error: function r(xhr, textStatus, errorThrown)\n {\n alert("error:" + JSON.stringify(xhr));\n }\n });\n<\/script>\n\n<table id="table1">\n <tbody><\/tbody>\n<\/table><\/pre><\/div>\n","protected":false},"excerpt":{"rendered":"There was a simple requirement I came across for which a page should list all the files and folders in a SharePoint document library. I came up with a solution using ODATA call. Following is the REST call I made to get the list of all folder and files. \/_api\/web\/Lists\/GetByTitle(‘Documents’)\/Items?$select=FileLeafRef,FileRef&$orderby=FileRef asc Following is the code […]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[62],"tags":[273,272,274,150,208],"yoast_head":"\n
Get folder and files recursively in SharePoint using REST call : Binary Bits<\/title>\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\t\n\t\n\t\n