Here's the background and solution of 'SP.JS file is not Loading for Read Only (read/ View) user error in SharePoint online.
Background: We have a document library and two level folders so the hierarchy is like this:
Document Library > Folder, then Sub Folder1, Sub Folder 2, Sub Folder 3
Our requirement is that on the publishing page we need to fetch all sub folders and we want to show them with links, so that the user will click on the respective sub folder link and go directly to the respective sub folder, either to view the documents or to upload based on the permissions.
So we wrote a JavaScript file, used JSOM (Java Script Object Model),and we queried the folder directly and listed the sub folders. We added the Content Editor web part on the page and referred to our JS file. All is fine and it works.
The problems occur when the user has only “read” / “view” permission and visits the page he/she can not see the result. But site collection administrators or users having full control/contributes rights can see the results
Problem/Issue:
Since it is JS file, we started debugging using the Developer Tool bar in IE and now we have the code as follows:
- $(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 this:
- $(document).ready(function()
- {
- loadSubFolders();
- });
Then, while getting the object of ClientContext (as shown in the following line) in the function we got the exception:
- var context = new SP.ClientContext.get_current();
So we verified all the .JS files loaded, and “SP.JS” and “SP.Runtime.JS” files were not there as in the following figure 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 that 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 the “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 the 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 did not work. Our other JSOM web parts also failed.
Finally, we tried one workaround: We have added one Script Editor web part, and added the following snippet into it, published the page, and it worked like a charm.
- <script type="text/javascript">
- SP.SOD.executeFunc('sp.js', 'SP.ClientContext', sharePointReady);
-
-
-
- function sharePointReady() {}
- </script>
Read more articles on SharePoint: