none
Get all sharepoint sites in a site collection to which user has access using Javascript client object model RRS feed

  • Question

  • 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.

    Dhaval Raval

    Monday, February 9, 2015 10:04 PM

Answers

  • Hi,

    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. For example, you have site collection at address http://site. Under it a two sub sites http://site/subsite1 and http://site/subsite2. Now you have sub sites under subsite1 and subsite2 like http://site/subsite1/subsite11 and http://site/subsite2/subsite22. Now if you run the following JavaScript code in a page in root site collection, you will get only http://site/subsite1 and http://site/subsite2. In order to get all sub webs you will need to modify the code to support recursive calls:

    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);

    Thanks,

    Nadeem

     


    Please remember to up-vote or mark the reply as answer if you find it helpful.

    Tuesday, February 10, 2015 5:59 AM

All replies

  • Hi,

    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. For example, you have site collection at address http://site. Under it a two sub sites http://site/subsite1 and http://site/subsite2. Now you have sub sites under subsite1 and subsite2 like http://site/subsite1/subsite11 and http://site/subsite2/subsite22. Now if you run the following JavaScript code in a page in root site collection, you will get only http://site/subsite1 and http://site/subsite2. In order to get all sub webs you will need to modify the code to support recursive calls:

    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);

    Thanks,

    Nadeem

     


    Please remember to up-vote or mark the reply as answer if you find it helpful.

    Tuesday, February 10, 2015 5:59 AM
  • Hi,

    Please go through the below url

    Thanks,
    Vivek
    Please vote or mark your question answered, if my reply helps you

    Tuesday, February 10, 2015 8:13 AM