15

I have a Document Library with some exported webparts (.dwp). When I embed a hyperlink somewhere to the document, it opens in the browser (IE8). I cannot change any settings on the site collection neither can I change the defaults for anything regarding the farm or filesystem. The solution has to apply for this library only.

Basically I need a link which engages the same behavior like the "Download a Copy" Button in the library.

5 Answers 5

32

The link can be reproduced using the following link, where "Documents" is the name of the library:

    http://spsite/_layouts/download.aspx?SourceUrl=http://spsite/Documents/sample.docx

Effectively the download.aspx will read the content of the file you send as the SourceUrl, and render it back to you on the HTTP response.

Hope that helps

4
  • Works like a charm, thanks a lot! :) Just for completeness, could you add an "?" behind the default.aspx?
    – curmudgeon
    Oct 11, 2012 at 15:40
  • James would there be a way to tell SharePoint to add the link just like that on a publishing page for documents, or do we have to manually re-type our anchor links to look like this?
    – klewis
    Jan 14, 2016 at 14:31
  • This doesn't seem to work in the Edge browser unfortunately. We have tried several approaches, eg 1) &download=1 2) &web=0 3) https://<tenant-name>.sharepoint.com/_layouts/download.aspx?SourceUrl=<document-url>. We have been googling and testing for hours but can't find a solution to the problem "How to force download document via link in SharePoint Online". Jan 13, 2023 at 5:58
  • update and fyi, solution is here: sharepoint.stackexchange.com/a/121124 Jan 13, 2023 at 6:08
10

I was trying James' solution but was having problems building the correct URL of the document. To get the URL you can:

  1. Open Dev Tools (F12)
  2. Open the Network tab
  3. Click 'Start capturing' button
  4. Click the download a copy button for the document you want the link for
  5. The HTTP GET request URL will appear in the network tab.
2
  • This was particularly useful since the syntax appears to be different for SharePoint Online, which is what I needed. Thanks for posting.
    – Jim Parker
    Apr 6, 2018 at 14:49
  • Very helpful. helped me a lot.
    – FLICKER
    Sep 19, 2019 at 21:59
1

Here a some code I put together to automatically parse URLs and dynamically reinsert the href values with the download.aspx fix. It also checks for relative paths. sub1, sub2, and sub3 are in case you have multiple subdomains that you link from/to each other. This function assumes that the excel doc will reside in a /Documents/ library only. This could be simplified with location.hostname or similar javascript interfaces:

    $(document).ready(function(){ $('a[href$=".xlsx"], a[href$=".xls"]').each(function() {
var excelURL = $(this).attr('href');
var finalModifiedExcelURL;
var testHTTP = 0;
var testSub1 = 0;
var testSub2 = 0;
var testSub3 = 0;

/* Default value for URL base */
var siteDomain = "https://sub1.yourdomain.com";

var pathToSite;
var startOfPathToSite;
var endOfPathToSite; 

/* Testing if it's a relative URL (its on our Sharepoint server) vs. URLs with domain (important later) */
if (excelURL.indexOf("https://") === 0) {
    testHTTP = 1;
    }
    else if (excelURL.indexOf("http://") === 0) {
    testHTTP = 1;
    }
else {
    /* This means that its a relative path -- It's on our site. */
    testHTTP = 2;   
}

/* More tests...Is the file on our site? Which site? (siteDomain is set and used for the final URL stitching) */
if (excelURL.indexOf("sub1.yourdomain.com") > 0) {
    testHSUCSD = 1;
    siteDomain = "https://sub1.yourdomain.com";
}

if (excelURL.indexOf("sub2.yourdomain.com") > 0) {
    testHealth = 1;
    siteDomain = "https://sub2.yourdomain.com";
    }

if (excelURL.indexOf("sub3.yourdomain.com") > 0) {
    testWellness = 1;
    siteDomain = "https://sub3.yourdomain.com";
    }

/* Modify all excel doc links referring to health, hs, wellness, relative URLs. */
if (testSub1 === 1 || testSub2 === 1 || testSub3 === 1 || testHTTP === 2) {
    /* This determines the start of the subsite path. If it has an http, then the start of the path is not 0, since it is not a relative URL. Therefore, it finds ucsd.edu and sets the starting point of the slice there. */
    if (testHTTP === 1) {
    startOfPathToSite = excelURL.indexOf(".yourdomain.com") + 15;
    }
    else {
    startOfPathToSite = 0;
    }

    /* Determines the end of slicing the subsite path by finding the point before the Documents library */
    endOfPathToSite = excelURL.indexOf("/Documents/");

    /* Slices the start and end point of the subsite within the href. */
    pathToSite = excelURL.slice(startOfPathToSite, endOfPathToSite);

    /* Puts it all together in finalModifiedExcelURL. (downloadAspx, fieldURL, "/Documents/Forms/AllItems.aspx" are the static portions of the excel URL fix) */
    var downloadAspx = "/_layouts/15/download.aspx?SourceUrl=";
    var midDocPath = excelURL.slice(startOfPathToSite);
    var fieldURL = "&FldUrl=&Source=";
    var lastPart = siteDomain + pathToSite + "/Documents/Forms/AllItems.aspx";
    finalModifiedExcelURL = siteDomain + pathToSite + downloadAspx + midDocPath + fieldURL + lastPart;
}

/* Replaces the href value that $.each() iterated on. */
$(this).attr('href', finalModifiedExcelURL); }); });

Hope that helps.

0
var downloadFile = _spPageContextInfo.webAbsoluteUrl+
               "/_layouts/download.aspx?SourceUrl=" + item.EncodedAbsUrl;

item is from $.each(data.d.results, function(index, item){ // the above code here });

0

I cannot comment yet(because of some points), but important when inspecting the page is that you have to inspect the page on the file folder of sharepoint, and then press download.

The you'll see an download URL like this, (ID is fake) https://sharepoint-url.com/_layouts/15/download.aspx?UniqueId=990-909dwad9828e298a6c889b

This will download the document directly. The version with &download=1 gives some errors in my version of Microsoft 365

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.