Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

Is there a way to get the list of sites in the current site collection into jQuery as an array or other data structure? Thanks.

share|improve this question

1 Answer 1

up vote 6 down vote accepted

You can use the ECMA Script Client Object Model:

ExecuteOrDelayUntilScriptLoaded(loadWebs, "sp.js");

    function loadWebs() {

       var clientContext = new SP.ClientContext.get_current();

       this.webs = clientContext.get_web().get_webs();

       clientContext.load(this.webs);

       clientContext.executeQueryAsync(Function.createDelegate(this, this.onWebsLoaded), Function.createDelegate(this, this.onQueryFailed));

    }

    function onQueryFailed(sender, args) {

       alert('request failed ' + args.get_message() + '\n' + args.get_stackTrace());

    }

    function onWebsLoaded(sender, args) {

       for (var i = 0; i < this.webs.get_count(); i++) {

       alert(this.webs.itemAt(i).get_title());

       }    
    }
share|improve this answer
1  
Thanks, Vardhaman. Where did you get the syntax for the Client Object Model? Is there documentation on SharePoint/JavaScript? Thanks. –  Alex C Sep 4 '11 at 5:47
3  
Here is the MSDN link for the classes: msdn.microsoft.com/en-us/library/ee557057.aspx also check out the following MSDN article series: blogs.msdn.com/b/sharepointdev/archive/2011/07/13/… –  Vardhaman Deshpande Sep 4 '11 at 5:50
    
It's giving me a JavaScript error: SP.ClientContext is undefined. I just noticed that the JS file in my page layout loads before the required SP.js files are loaded; any way to fix this? –  Alex C Sep 4 '11 at 5:53
1  
I have edited the original code. see the ExecuteOrDelayUntilScriptLoaded function. –  Vardhaman Deshpande Sep 4 '11 at 5:56
1  
Check out the class library msdn link. We can do many awesome things with the Client Object Model. –  Vardhaman Deshpande Sep 4 '11 at 6:10

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.