none
Multiple ClientContext Calls for data across Site Collections RRS feed

  • Question

  • Hi all,

    I need to create a consolidated list of sub sites that span Site Collections. I was wondering if I could set the ClientContext to the entire Web Application and enumerate from there, or selectively state which Site Collections to pull from for the Web enumeration. The code below (pasted into a Script Editor Web Part for testing), works for retrieving all the sub sites within a single Site Collection, but I need to cross over.

    <script type="text/javascript" src="https://ajax.aspnetcdn.com/ajax/jquery/jquery-1.9.0.min.js"></script>
    <script type="text/javascript">
        $(function() {
            // Make sure the SharePoint script file 'sp.js' is loaded before your
            // code runs.
            SP.SOD.executeFunc('sp.js', 'SP.ClientContext', sharePointReady);
        });
    
    // Create an instance of the current context.
    function sharePointReady() {
        var url = "https://yourcompany.com/sites/projects";
        var clientContext = new SP.ClientContext(url);
        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 webEnumerator = this.webCollection.getEnumerator();
        while (webEnumerator.moveNext()) {
            var web = webEnumerator.get_current();
            if(web.get_description() == "Special Site") {
                $("#SpecialProjects ul").append("<li><a href=" + web.get_url() + ">" + web.get_title() + "</a></li>");
            }
        }
    }
    
    function onQueryFailed(sender, args) {
        alert('Request failed. ' + args.get_message() + args.get_stackTrace());
    }
    </script>
    
    <div id="SpecialProjects"><h2>Special Projects List</h2><ul></ul></div>

    Thanks,

    Kelly


    Personal Blog: http://thebitsthatbyte.com


    • Edited by Kelly Rusk Wednesday, August 14, 2013 8:23 PM
    Wednesday, August 14, 2013 4:06 PM

Answers

  • Hi Kelly

    Please find below the REST query which returns the trimmed list of sub webs.

    http://site/_api/web/webs/?$select=title,effectivebasepermissions&$filter=effectivebasepermissions/high%20gt%2032

    Narahari

    ******If a post answers your question, please click "Mark As Answer" on that post and "Vote as Helpful*******


    • Marked as answer by Kelly Rusk Sunday, August 18, 2013 9:00 PM
    Sunday, August 18, 2013 8:57 PM

All replies

  • It is acceptable or is it poor practice to have separate Script Editor Web Parts on a page that each display the sub sites? My concern is repeated calls to the CSOM and setting of the ClientContext will impact performance or run over each other. It would be great if I could comma delimitate the desired URLs. If not, does anyone have examples of how to set multiple ClientContexts?

    I am also working in SharePoint Online if that makes a difference.


    Personal Blog: http://thebitsthatbyte.com




    • Edited by Kelly Rusk Wednesday, August 14, 2013 5:21 PM
    Wednesday, August 14, 2013 4:17 PM
  • Update: If I can't roll it all into one, I may just upload my individual JS files to SharePoint and reference them from the Script Editor. If I didn't have the requirement to use the Script Editor Web Part, I would put the references in a new Master Page.

    Personal Blog: http://thebitsthatbyte.com

    Wednesday, August 14, 2013 5:51 PM
  • Update: I created separate JS files for each Site Collection and tried to reference them together in one HTML file, but it gives me the following error:

    The collection has not been initialized. It has not been requested or the request has not been executed. It may need to be explicitly requested.


    Personal Blog: http://thebitsthatbyte.com

    Wednesday, August 14, 2013 8:12 PM
  • Hi Kelly, 

    Using JS CSOM, you can fetch the data for current site collection only. It will not span across site collections. To fetch data across site collections using JS, I would suggest REST.

    Narahari

    ******If a post answers/helps your question, please click "Mark As Answer" on that post and/or "Vote as Helpful*******

    Thursday, August 15, 2013 2:42 AM
  • Thanks Narahari,

    I would like to use REST but I can't get it to security trim the sites. Do you know the correct context for the following:

    http://<sitecollection>/<site>/_api/web/getSubwebsForCurrentUser(query)

    I had tried your recommendation here, but can't locate documentation for "GetSubwebsFilteredForCurrentUser" which is different than "getSubwebsForCurrentUser".

    http://social.msdn.microsoft.com/Forums/sharepoint/en-US/6c6e13e2-3d7e-4182-8257-3a38687baae7/easy-rest-endpoint-question

    My end goal is to retrieve the OData for all the sub sites including the title, description, URL, etc.

    Thanks for all your help,

    Kelly


    Personal Blog: http://thebitsthatbyte.com


    • Edited by Kelly Rusk Friday, August 16, 2013 12:00 AM
    Thursday, August 15, 2013 11:42 PM
  • Can anyone confirm is the REST API for getSubwebsForCurrentUser is not functioning/has bugs or if I just need the correct syntax?

    Personal Blog: http://thebitsthatbyte.com

    Saturday, August 17, 2013 3:21 PM
  • Hi Kelly

    Please find below the REST query which returns the trimmed list of sub webs.

    http://site/_api/web/webs/?$select=title,effectivebasepermissions&$filter=effectivebasepermissions/high%20gt%2032

    Narahari

    ******If a post answers your question, please click "Mark As Answer" on that post and "Vote as Helpful*******


    • Marked as answer by Kelly Rusk Sunday, August 18, 2013 9:00 PM
    Sunday, August 18, 2013 8:57 PM