
Document or list items can be easily fetched using REST calls, recently went into an while fetch the details from a document library using REST call, all documents are opening properly except the image files due to parameter issue. I am a small code snippet for fetching all type file URLs from a SharePoint Document library.
function GetDocumentsByTitle(docTitle) {
$.ajax({
url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle(docTitle)/Items?$select= Created,Author/FirstName,Author/LastName, FileLeafRef,FileRef/FileRef&$expand=Author/Id",
type: "GET",
headers: {"accept": "application/json;odata=verbose"},
success: function (data) {
if (data.d.results) {
var listItems = " ";
for (var i = 0; i < data.d.results.length; i++) {
listItems += "<div ><a target='_blank' href='"+data.d.results[i].FileRef+"'>"+data.d.results[i].FileLeafRef+"</a><p>"+String.format("{0:d}",new Date(data.d.results[i].Created))+", "+data.d.results[i].Author.FirstName+" "+data.d.results[i].Author.LastName+"</p></div>";
}
console.log(listItems);
$("#divResults").html(listItems);
}
},
error: function (errMsg) {
alert(errMsg.status + ': ' + errMsg.statusText);
}
});
}
In above code snippet, output is shown a div with id divResults.
No Comments