If you have ever managed an Anonymous SharePoint site you know that working with Office documents is a pain.  SharePoint and Office try to be smart.  SharePoint passes a path to the to the Office application and then the Office application goes out to the web application to get the file.  This is great for collaboration sites and allows you to automatically save the file back to SharePoint without having to manually upload it again.  But in anonymous internet sites, this is a terrible user experience.  The user gets a log-in prompt or two.  If they cancel, the document will likely open, but that experience is not good.  This post is going to provide you with a way to override the default link for Office documents within SharePoint so you can avoid all the log-in prompts.

The worst kick in the pants on this is that FireFox and Chrome handle these files perfectly in this scenario.  If you look at the links in those browsers, they are different.  They download them directly without passing the path to the Office application.

So, the jQuery below will look through site and update and link to an Office document and change the URL to the direct download link.  There are also some events you have to remove from the link.

   1: <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.js" type="text/javascript"></script>
   2: <script type="text/javascript">
   4:     if (window.location.toString().toLowerCase().indexOf("/allitems.aspx") !=-1){ 
   5:         var updateFileExt = new Array("docx", "doc", "xls", "xlsx", "ppt", "pptx", "pdf", "txt");
   6:         var checkString = "";
   7:         for(var i = 0; i < updateFileExt.length; i++)
   8:         {
   9:             checkString += 'ahref$="' + updateFileExt[i] + '", ahref$="' + updateFileExt[i].toUpperCase + '", ';
  10:         }
  11:         checkString = checkString.substring(0, checkString.length - 2)
  12:         alert(checkString);
  14:         $m(checkString).each(function(){
  15:             var newDocUrl = location.protocol + "//" + window.location.host + "/_layouts/download.aspx?SourceUrl=" + this.href; 
  16:             this.href = newDocUrl;
  17:             $m(this).removeAttr("OnClick");
  18:             $m(this).removeAttr("OnFocus");
  19:             $m(this).removeAttr("OnMouseDown");
  20:         });
  21:     }
  22: </script>

I’m basically building a string of file extensions that I want to override the behavior for, then looping through that string .  It is checking against the uppercase and lowercase file extension in case someone uploads it that way.  Line 4 does a check and does not run this code against the default All Items views of a document library.  When you click a document link, you will just get the standard IE download toolbar. 


This is best added to your master page so that it affects all pages in your site.  There may be a more efficient way to have the jQuery search through the page, feel free to put it in the comments.

For more information on C5 Insight or this blog entry, please Contact Us.