0

I want to display all sites in a site collection using JSOM to which user has access to. In other words I only need to find collection of sites to which user has access in a site collection. I am able to get all webs but it doesnt work if user doesnt have permissions to some of web sites.

Learner
  • 79
  • 2
  • 11

3 Answers3

3

SP.Web.getSubwebsForCurrentUser Method returns a security trimmed (user has access) collection of sub sites (only one level beneath)

Example

var ctx = SP.ClientContext.get_current();
var webs = ctx.get_web().getSubwebsForCurrentUser(null);
ctx.load(webs);
ctx.executeQueryAsync(
    function() {
        for(var i=0;i< webs.get_count();i++) {
            var web = webs.getItemAtIndex(i);
            console.log(web.get_title());   
        }
    },
    function(sender,args){
       console.log(args.get_message());
    }
);

If you are interested in all sub webs within site collection, you could consider the following approach.

function getAllSubwebsForCurrentUser(success,error)
{
   var ctx = SP.ClientContext.get_current();
   var web = ctx.get_site().get_rootWeb();
   var result = [];
   var level = 0;
   var getAllSubwebsForCurrentUserInner = function(web,result,success,error) 
   {
      level++;
      var ctx = web.get_context();
      var webs = web.getSubwebsForCurrentUser(null);
      ctx.load(webs,'Include(Title,Webs)');
      ctx.executeQueryAsync(
        function(){
            for(var i = 0; i < webs.get_count();i++){
                var web = webs.getItemAtIndex(i);
                result.push(web);
                if(web.get_webs().get_count() > 0) {
                   getAllSubwebsForCurrentUserInner(web,result,success,error);
                }   
            }
            level--;
            if (level == 0 && success)
              success(result);  
        },
        error);
   };

   getAllSubwebsForCurrentUserInner(web,result,success,error);    
}

Usage

getAllSubwebsForCurrentUser(
function(allwebs){
    for(var i = 0; i < allwebs.length;i++){
        console.log(allwebs[i].get_title());   
    }
},
function(sendera,args){
    console.log(args.get_message());
});
Vadim Gremyachev
  • 57,952
  • 20
  • 129
  • 193
  • 1
    It's not working for the users with 'Visitors' access,.. It's goes to failure() method 'error',.. when I remove Webs from Include: ctx.load(webs,'Include(Title,Webs)'); , It's working & goes to Success function.. Any help ?? – Ronak Patel Aug 13 '15 at 07:18
0

Hi the following code snippet may help you.

var ctx = SP.ClientContext.get_current();
var web = ctx.get_web();
ctx.load(web);
var webCollection = web.getSubwebsForCurrentUser(null);
ctx.load(webCollection);
ctx.executeQueryAsync(
  Function.createDelegate(this,this.onSuccess),
  Function.createDelegate(this,this.onError)
);

getSubwebsForCurrentUser - uses a parameter of type SP.SubwebQuery which you may leave as null. The web collection you get using this code is just of one level. You will not get the subsites of the subsites. For that you need to execute the same statements on every SP.Web object you get - recursively - starting from the root web.

Saratchandra
  • 166
  • 5
-3

If you can use the API instead then I would suggest you do the following to return all the sub webs for the current user.

using(SPSite site = new SPSite("http://example/site/"))
{
using (SPWeb web = site.OpenWeb())
{
    SPWebCollection webCollection = web.GetSubwebsForCurrentUser();
}
}

Note: As pointed out by Helm Sterk in comment below, GetSubwebsForCurrentUser() would not return result which the user is seeking. So above code would not work.

404
  • 249
  • 6
  • 16
  • 1
    getSubwebsForCurrentUser() returns only one level of sites under the current site. It does not fatch the sub, subsites. – Imir Hoxha Oct 28 '15 at 09:51