1

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.

2

You can use SP.Web.getSubwebsForCurrentUser(query_) to get the list of sub sites a user has permission on. However, please note that this will return the sub site immediately underneath a site. That is, if you have nested sites several level deep you will need to write a recursion. Here is the sample code:

var webCollection = null;
    function runCode() {

        var clientContext = new SP.ClientContext.get_current();
        if (clientContext != undefined && clientContext != null) {
            var web = clientContext.get_web();

            this.webCollection = web.getSubwebsForCurrentUser(null);

            clientContext.load(this.webCollection);
            clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
        }
    }

    function onQuerySucceeded() {
        var webInfo = 'Child sites of the current site: \n\n';
        var webEnumerator = this.webCollection.getEnumerator();
        while (webEnumerator.moveNext()) {
            var web = webEnumerator.get_current();
            webInfo += web.get_title() + '\n';
        }
        console.log(webInfo);
    }

    function onQueryFailed(sender, args) {
        alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
    }

SP.SOD.executeFunc('sp.js', 'SP.ClientContext', runCode);
0

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();
    }
}
0

Use SP.Web.getSubwebsForCurrentUser Method since it returns a security trimmed collection of sub sites (where user has access)

But it only returns sites for one level beneath current site.

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 a 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.get_webs(); 
      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());
});

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.