Wednesday 15 February 2017

SharePoint Online / Office 365 - SP.JS file is not getting not loaded for read only users (users having “read” / “View” permission)

n this article I’ll explain the issue which we faced for users who have read /view rights on the site for SP.JS file and workaround we did for it.
 
Background : We have document library and two level folders so hierarchy is like
 
                         Document Library => Folder => Sub Folder1, Sub Folder 2, Sub Folder 3
 
Our requirement is on one publishing page we need to fetch all sub folders and want to show them with links. So that user will click on respective sub folder link and directly go to the respective sub folder either to view the documents or to upload based on the permissions.
 
So we have written JavaScript file, used JSOM (Java Script Object Model), we queried to the folder directly and listed the sub folders. Added Content Editor web part on the page and refer the our JS file. All is fine and working.
 
Problems occur when user having only “read” / “view” permission visited the page he/she cannot see the result. But Site Collection Administrators or user having Full Control / Contributes rights can see the result.
 
Problem / Issue :
Since it is JS file, we started debugging using Developer Tool bar in IE and since we have code like as follow
$(document).ready(function () {
          ExecuteOrDelayUntilScriptLoaded(loadSubFolders,"sp.js");
                });
 
our function “loadSubFolders” never get called since “sp.js” file is not loaded and if we directly call function like
 
               $(document).ready(function () {
                        loadSubFolders();
                });
 
then while getting object of ClientContext(as shown in following line) in function we got the exception.
 
                      var context = new SP.ClientContext.get_current();
 
So verified all the .JS files loaded and “SP.JS” and “SP.Runtime.JS” files were not there as shown in Fig 1. “sp.core.js”, “sp.init.js” files are loaded but not “sp.js” and “sp.runtime.js” loaded.
 
Figure 1 : Only these .js files are loaded
We went to our master page, verified there we have explicitly loaded the “sp.js” file and “sp.runtime.js” files as
 
Figure 2 : sp.js and sp.runtime.js referenced in master page
We were wondering why these two files are not getting loaded.
 
Solution / Work Around : After googling a lot we found that “sp.js” file is loaded only for specific permissions and it doesn't get loaded for the users who have View / Read permissions (in some cases). Sometime it works only for Edit / Approve permissions and suggestion like use of explicitly calling “sp.js” using SP.SOD.executeFunc .
 
So we tried this in our js file but then it started throwing some other exceptions but not working. Our other JSOM web parts also failed.
 
Finally we tried one workaround, we have added one Script Editor web part, and added following snippet into it, published the page and it worked like charm :)
<script type="text/javascript">
   SP.SOD.executeFunc('sp.js''SP.ClientContext', sharePointReady);
   //Empty function just to ensure "sp.js" file loaded
  function sharePointReady(){
}

        </script>


Happy SharePoint.....:)

No comments:

Post a Comment