Sunday, March 1, 2009

Get all the sub sites under the current site

Scenario:
I wanted to get all the sub sites directly beneath the current site

Solution:
You can do this using Web Service call using Web.asmx

Code:

proxy.Webs ws = new proxy.Webs();
ws.Url = "http://localhost/_vti_bin/Webs.asmx";
ws.Credentials =System.Net.CredentialCache.DefaultCredentials;
System.Xml.XmlNode nd = ws.GetWebCollection();
XmlNodeList ndlist = nd.ChildNodes;
foreach (XmlNode xGet in ndlist){
string sWebTitle = xGet.Attributes["Title"].Value;
string sOneSubSite =xGet.Attributes["Url"].Value;
}

3 comments:

Anonymous,  July 29, 2010 at 9:06 AM  

Where do i put that code??

Sandeep K Nahta July 29, 2010 at 1:33 PM  

Good question , but the reason I didn't specify exactly where to use it was , it can be used at multiple places
1. in a webpart
2. in a console application
3. in a timer job
4. in a window service and many more.. depending upon how you want to consume this information

Sri,  December 15, 2010 at 2:35 AM  

How can I use this web service on a InfoPath form to display all the subsites Dynamically in a drop down list.