SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
up vote 7 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.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++) {


share|improve this answer
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
Here is the MSDN link for the classes: also check out the following MSDN article series:… – 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
I have edited the original code. see the ExecuteOrDelayUntilScriptLoaded function. – Vardhaman Deshpande Sep 4 '11 at 5:56
Thanks, Vardhaman. It worked like a charm once I added the ExecuteOrDelayUntilScriptLoaded() function. Also, I placed it inside the jQuery $(document).ready() function. By the way, if we run this from a site, it seems to only return that site's title; does it need to be run from a master page for the entire site collection to return all sites? – Alex C Sep 4 '11 at 6:02

Your Answer


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.