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))+",&nbsp;"+data.d.results[i].Author.FirstName+"&nbsp;"+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.

Adnan is six time Microsoft MVP (Since 2015) with over 16 years of extensive experience with major expertise on SharePoint, SharePoint based development, Microsoft 365, Microsoft Teams, .Net Platform and Microsoft BI. He is currently working Sr Microsoft Consultant at Olive + Goose. He is MCT Regional Lead for Pakistan Chapter since 2012. He is working on SharePoint for past 12 years and worked on different intranet/intranet solutions for private & govt. sector majorly in United states and Gulf region and have experience of working with multiple Fortune 500 companies. He is a trainer, technology evangelist and also speaks in community forums.

Leave a Reply