13

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.

26

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

  • Works like a charm, thanks a lot! :) Just for completeness, could you add an "?" behind the default.aspx? – curmudgeon Oct 11 '12 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 '16 at 14:31
5

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.
  • 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 '18 at 14:49
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 });

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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