Hey readers, I wanted to share with you a solution that I came up with on how to show all sub sites from your top level site using SharePoint's ECMAScript Client Object Model. Let’s get started!!!
First, add a Content Query Web Part to your home page.
Home page view after adding the content query web part.
Next, edit the web part by clicking on the down arrow next to the checkbox and select the ‘Edit Web Part’ menu item.
Now we need to do some HTML editing within the Content Query Web Part. To do this we must double click on the content ‘Click here to add new content’, then select the HTML button towards the far right of the ribbon and select ‘Edit HTML Source’ menu item.
Now you should see the HTML Source dialog window appear. Within this window I want you to add the following code below.
ExecuteOrDelayUntilScriptLoaded method?
This method accepts two parameters, your function name and one of SharePoint's scripting files which is required inorder for you to interact with SharePoint. In my solution, the sp.js file provides you with the ability to retrieve site information, list information, even allows you to perform CURD operations against a single list and more. Before we can do this we first allow the sp.js to load, which is the purpose of the ExecuteOrDelayUntilScriptLoaded method, once the file is loaded then your method is executed. There’s a bunch more scripting files which allows you to interaction with other areas of SharePoint check out ECMAScript Class Library for more information.
ShowAllSubSites first establishes a connection to SharePoint by creating an instance of the SP.ClientContext object, similar to SPContext object. Now that I have a connection establish I can begin with retrieving all my child sites by using the ctx.get_web().get_webs() method. In short, this retrieves all sub sites for the current web application. Finally, we call the executeQueryAsync method which does async request back to SharePoint to retrieve all child sites from your top level site. If the call was successful then the onUpdate method is executed, else the onFail method is executed.
After a successful call, now we need to loop though the this.webSubSites collections object to print all child sites to the screen.
Here is your end result:
So here's a simple way to retreive all subsites using the ECMAScript client object model provided to you by SharePoint. You could a data view web part or a content query web part, but then you're working with xslt and I haven't master xslt.....yet!! Well I hope you’ve learned something from this post, let me know if you like it or not. Other than that, thanks for stopping by. Below is a list of articles that I used to achieve this solution.
References: