Friday 15 September 2017

Get files from SharePoint document library using REST API

Sometimes we need to read files in particular folder in SharePoint, we can achieve this using REST.

My Business Case : I got the requirement in angularjs form to attach documents, so rather than attaching in list item itself, i used document library to attach files and then read it in edit and display form using unique id.

Usage : var filenames = getFolderFiles("foldername");

function getFolderFiles(foldername) {
var attachments = [];
$.ajax({
url: SiteURL + "/_api/web/getfolderbyserverrelativeurl('/SiteCol1/Site1/Library1/" + foldername + "\')/files",
method: "GET",
async: false,
headers: {
"Accept": "application/json; odata=verbose"
},
success: function(data) {
var files = data.d.results;
for (i = 0; i < files.length; i++) {
//debugger;
attachments.push(files[i].ServerRelativeUrl);
}
//window.open(AddAttachmentURL + foldername, "_blank")
},
error: function(jqXHR, textStatus, errorThrown) {
//handle error...
alert(jqXHR.responseText);
}
});
return attachments;
}




No comments:

Post a Comment