Fetch Image and document URLs for SharePoint Document library

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 Amin is a Microsoft MVP for Microsoft 365 (since 2015) and a Senior Microsoft Consultant. He writes practical guides and PowerShell scripts for SharePoint, OneDrive, Teams and Copilot admins.

Leave a Reply

Your email address will not be published. Required fields are marked *