0

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?

1 Answer 1

2

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.