Wednesday, January 1, 2014

Get all sub sites inside a particular SharePoint Site Collection using built-in web services

Recently I came across a scenario where I had to create a console application for getting all sub sites  inside a particular SharePoint site collections using SharePoint web services. I used GetAllSubWebCollection() method in Webs.asmx web service. Well following are the steps:-

Steps:
  1. Add "{your site coll url}/_vti_bin/Webs.asmx" as web reference.
  2. Use the GetAllSubWebCollection() method in Webs.asmx webservice to get all sub sites and site collections.
  3. Point to remember - User should have atleast "Read" permission to all the sites.
Code Snippet
  1. class Program
  2. {
  3.     static void Main(string[] args)
  4.     {
  5.         //webs object from the service reference named "WebRef"
  6.         WebRef.Webs myweb = new WebRef.Webs();
  7.         
  8.         //Enter you network credentials
  9.         myweb.Credentials = new System.Net.NetworkCredential("username", "password", "domain");
  10.  
  11.         //Replace {your site coll url} with your site collection url
  12.         myweb.Url = "{your site coll url}/_vti_bin/Webs.asmx";
  13.         
  14.         //Get a collection of all the subsites inside the site collection
  15.         XmlNode objXmlNode = myweb.GetAllSubWebCollection();
  16.         XmlNodeList objNodelist = objXmlNode.ChildNodes;
  17.         foreach (XmlNode objNode in objNodelist)
  18.         {
  19.             Console.WriteLine(objNode.Attributes["Url"].Value.ToString());
  20.         }
  21.         Console.ReadLine();
  22.     }
  23. }

No comments:

Post a Comment