Question

How is it possible to download file from document library by the help of item ID? In my data I have only item ID of a file which was uploaded to document library now I want to download the file i.e somehow get ServerRelativeUrl of the file uploaded,how is it possible in pnp js?

Was it helpful?

Solution

You should get it by looking for the EncodedAbsUrl

var requestUri = _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getByTitle('Shared Documents')/items?$select=EncodedAbsUrl&$filter=Id eq 123";        

var requestHeaders = {
    "accept": "application/json;odata=verbose"}

$.ajax({
url: requestUri,
type: 'GET',
dataType: 'json',
headers: requestHeaders,
success: function (data) 
{        
   $.each(data.d.results, function(i,result) {
        var path = result.EncodedAbsUrl;
     });

  },
  error: function ajaxError(response) {
    alert(response.status + ' ' + response.statusText);
  }
});

Set your ID variable for the 123 in first line. Be sure to change the title of your document library in the first line as well. The variable "path" will hold your full file path for downloading. You just need to download it then.

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top