1

I want to check the permissions of a page, but not be on that page. I can already check permissions of the page you're currently on, but I just want to check a list of links and check each page in that list, and then check the permissions.

So basically I want to loop through a list of links (like the quick launch menu) and check the permissions of each page behind each list item, without having to be on each page.

I know how to check permissions of the page you're currently on, by the help of this page and this page, but I just really need to find out how to fetch each list item in the menu and check each links permissions.

Any help is GREATLY appreciated, since I've been searching for this for some time now.

Here's the JavaScript code I use to check the permissions of the page the user is currently on:

SP.SOD.executeOrDelayUntilScriptLoaded(sharePointReady, 'SP.UserProfiles.js', "~sitecollection/Style%20Library/Scripts/jquery.SPServices-2013.01.min.js");
SP.SOD.executeFunc('SP.js', 'SP.ClientContext', sharePointReady);

sharePointReady();

function sharePointReady() {
    var clientContext = new  SP.ClientContext.get_current();    
    this.website = clientContext.get_web();
    this.currentUser = website.get_currentUser();

    this.usergrps = currentUser.get_groups(); 
    clientContext.load(this.usergrps); 

    clientContext.load(website,'EffectiveBasePermissions');

    clientContext.load(currentUser);
    clientContext.load(website);
    clientContext.executeQueryAsync(Function.createDelegate(this, this.onRequestSucceeded), Function.createDelegate(this, this.onRequestFailed));
}

function onRequestSucceeded() {    
    if (website.get_effectiveBasePermissions().has(SP.PermissionKind.editListItems)) {
        alert('Nice, edit list permissions!');
    }   
    else { 
        alert('Boo, no edit list permissions!'); 
    }   
}

function onRequestFailed(sender, args) {   
    console.log('Error: ' + args.get_message());
    alert('Request failed: ' + args.get_message() + '\n'+ args.get_stackTrace());
}



EDIT:
The following piece of code will fetch all links in the side navigation, and return them in another function (for testing purposes). The thing is, it returns all urls twice. So the first one is shown twice, then the second one is shown twice, and so on.

accordionMe(selector);

function accordionMe(selector) {
    var $this = selector;

    if($this != null) {
        $this.find('li').each(function(i){
            siteUrl = $this.find("a.static")[i].href;
            console.log('siteUrl: ' + siteUrl);
            // The following function is executed twice?
            returnThisThing(siteUrl); // sharePointReady(siteUrl);  
        });
    }
}

function returnThisThing(siteUrl){
    var siteUrl = siteUrl;
    console.log('siteUrl: ' + siteUrl); // Strange behaviour: this will return each link in the list TWICE. 
 }

When I try to pass the siteUrl to my original function (sharePointReady), it just does one loop and stops after the first link instead of going through all links. Any ideas? :s

4

looking here:

http://msdn.microsoft.com/en-us/library/ee557057(v=office.14).aspx

youll notice that you have clientcontext:

Represents the context for SharePoint objects and operations. Use the SP.ClientContext Class to return context information about such objects as the current Web application, site, site collection, or server version.

so from the above we can then use the url to get the context to load with the specific url

   var siteUrl = '/mysites/MySiteCollections';

   function sharePointReady()
   {

      var clientContext = new SP.ClientContext(siteUrl);
      var oWebsite = clientContext.get_web();

      this._currentUser = oWebsite.get_currentUser();

      context.load(this._currentUser);

      context.load(web,'EffectiveBasePermissions');

      context.executeQueryAsync(Function.createDelegate(this,this.onSuccessMethod),                                            
      Function.createDelegate(this, this.onFailureMethod));
    }

    function onSuccessMethod(sender, args) 
    {
       if (website.get_effectiveBasePermissions().has(SP.PermissionKind.editListItems)) {
          alert('Nice, edit list permissions!');
       }   
       else { 
          alert('Boo, no edit list permissions!'); 
       }   
    }   

    function onRequestFailed(sender, args) 
    {   
       console.log('Error: ' + args.get_message());
       alert('Request failed: ' + args.get_message() + '\n'+ args.get_stackTrace());
    }

wrap the above within a for loop, if your getting the url from a list in sharepoint than you need to get the list items first using this method, http://msdn.microsoft.com/en-us/library/hh185009(v=office.14).aspx once you have the list items you can loop each one and get the url as siteUrl instead of it being static! let me know if you need help! it should be quite straight forward but the consept is the same.

so it goes like:

1) get access to list

2) loop through each item within the list returning the correct row that the url is on,

3) instead of siteUrl use the list item row that the url is on,

4) wrap the above code that Iv given within a foreach loop or a for loop.

how you use the data from the list is upto you (list of urls from a sharepoint list), if you decide to put it in an array before you use it or if you use it directly is up to you!

within the array you populate during the list retrival within onSuccessMethod or directly you can put in a for loop within onSuccessMethod but passing a string url val to sharePointReady!

EDIT

based on my code and yours it should look somthing like this:

$(document).ready(function () { ExecuteOrDelayUntilScriptLoaded(loadConstants, "sp.js"); }); 

accordionMe(selector);

function accordionMe(selector) {
    var $this = selector;
    var siteurls;

    if($this != null) {
        $this.find('li').each(function(i){
            siteurls = $this.find("a.static")[i].href;
            sharePointReady(siteurls ); 
        });
    }
}

   function sharePointReady(siteUrl)
   {

      var clientContext = new SP.ClientContext(siteUrl);
      var oWebsite = clientContext.get_web();

      this._currentUser = oWebsite.get_currentUser();

      context.load(this._currentUser);

      context.load(web,'EffectiveBasePermissions');

      context.executeQueryAsync(Function.createDelegate(this,this.onSuccessMethod),                                            
      Function.createDelegate(this, this.onFailureMethod));
    }

    function onSuccessMethod(sender, args) 
    {
       if (website.get_effectiveBasePermissions().has(SP.PermissionKind.editListItems)) {
          alert('Nice, edit list permissions!');
       }   
       else { 
          alert('Boo, no edit list permissions!'); 
       }   
    }   

    function onRequestFailed(sender, args) 
    {   
       console.log('Error: ' + args.get_message());
       alert('Request failed: ' + args.get_message() + '\n'+ args.get_stackTrace());
    }
  • Well the list is actually the navigation in the quick launch, so I'm trying to fetch all links in the list items and for each link in the list, set them as the var siteUrl. I'm following your answer right now and trying to implement it, I'll let you know when I've made progress! :) – Magali Oct 28 '13 at 9:35
  • OK so the following code will loop through all the links in the left side navigation (quick launch), and give the link in an alert: function accordionMe(selector) { var $this = selector; if($this != null) { $this.find('li').each(function(i){ var linkValue = $this.find("a.static"); alert(linkValue[i].href); }); } } So far so good. Now I'll put it in a loop that for each link, it'll do the code to check permissions. – Magali Oct 28 '13 at 9:53
  • thats good, looks like your looping through returning on the alert, instead of alert call your metho sharePointReady(linkValue[i].href); sharePointReady should contain a varible string that will hold the url! – Ali Jafer Oct 28 '13 at 10:02
  • I keep getting the error Uncaught TypeError: undefined is not a function on this line: var clientContext = new SP.ClientContext(siteUrl); I don't understand why... :( – Magali Oct 28 '13 at 12:24
  • thats becasue SP.ClientContext doesnt exist: put this code first to run befor anything: $(document).ready(function () { ExecuteOrDelayUntilScriptLoaded(loadConstants, "sp.js"); }); your code should work properly then – Ali Jafer Oct 28 '13 at 13:03

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.