0

I have a Templates document library, where the piece of code below has happily forcing the download of a template document instead of the file been opened. However, it has stopped working.

The reason for needing to force download is that users actually need a copy of the template and don't need to open the actual original template. (There is an approval workflow, as part of a QMS, which manages the templates and tracks the version, users accidentally interfere with this otherwise).

If any user clicks on a file name currently they see this error message (which isn't really telling us anything, other than the code is not rerouting the 'open file' request to a 'download document' request:

file not found

Here is the code that is being used:

n.b. there is a Templates Editors group for certain users who have permission to edit the templates in order to keep them updated. For everyone else -> force download.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js" type="text/javascript"></script><script src="/uk_qhse/Style%20Library/jquery.SPServices-0.7.2.min.js" type="text/javascript">
</script><script type="text/javascript">
jQuery.noConflict();
jQuery(document).ready(function(){
  jQuery().SPServices({  
      operation: "GetGroupCollectionFromUser",  
      userLoginName: jQuery().SPServices.SPGetCurrentUser(),  
      async: false,  
      completefunc: function(xData, Status) { 
        //if current user is not a member of this group...       
        if(jQuery(xData.responseXML).find("Group[Name='Templates Editors']").length != 1)  
        {  
           //force download file on click
           jQuery("a[onclick*='DispEx']").each( function(data){
            var href = this.href;
            this.href = '/uk_qhse/_layouts/download.aspx?SourceURL=' + this.href;
            });
            jQuery("a[onclick*='DispEx']").removeAttr('onclick');  
        }  
      }  
    });  
});
</script>

Update 05/03/2019

Following Ahmad's suggestion to check the Console - there is a warning of 'deprecation' - it appears that my custom .js code is being ignored by the browser somehow. I've never seen this happen before, but I also didn't know the console existed (so what do I know...).

I did a search on the warning message and found this popular link. Possibly the code needs to load via AJAX instead of in the Style Library? There are quite a few Answers in this post, but a lot of it I don't understand, since I am a humble SP Admin who only dabbles in modifying & reusing existing code.

error message

  • 1
    Can you see any javascript error in the console? Can you also do a console.log(this.href) in the onclick to make sure the SourceURL is correct? – Ahmad Zia Feb 27 at 2:29
  • Hi Ahmad, thanks for the suggestion, I'm looking in to this. I'm an amateur with using things like console.log and I only have a basic understanding of Javascript. There is an error in the console. PLease post your comment as an answer, as you might have solved my issue I think. – Tally Mar 5 at 9:06
0

Regarding the deprecation message:

The deprecation message is caused by the async: false in your SPServices call. You should be able to perform that query asynchronously (change to async: true) without issues, as you are correctly using the completefunc callback already.

The message should then disappear - but it also shouldn't be the cause of your issue, as a deprecation typically means that something is still functional, but should not be used anymore because of performance issues or being removed in future versions of the software.

Regarding your original issue:

If the problem does not occur with every document (I was not able to determine whether that's the case), but only with those with special characters in the name, it is possible that encoding the URL parameter solves the issue. This should be done nonetheless to prevent additional problems coming from umlauts etc.

this.href = '/uk_qhse/_layouts/download.aspx?SourceURL=' + encodeURIComponent(this.href);

Am I right in thinking that the documents are also all in a library within the /uk_qhse subsite?

  • re: deprecation message - I changed async to async: true. As you expected, this didn't solve the original issue. The problem does occur with every document, there are no special characters being used. The documents are all within the same document library, the document library is within the uk_qhse subsite. – Tally Mar 5 at 12:19
  • Can you please provide an example URL for a document, i.e. the original and adjusted href? Which SharePoint major version (and CU if known) are you using? @Tally – Anon Mar 5 at 12:53

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.