Pages

Wednesday 30 January 2013

How to list all the Web Applications, all the site collections within the web app and all the Level 1 sub sites within that site collection using c# SharePoint 2010

I recently had  a requirement for a SharePoint Hosting provider, they wanted an inventory list of all the web applications, all the site collections and the first level sub sites with the site collection.

I used a console application, you can do this easily with Powershell as well.


try
            {
                SPServiceCollection services = SPFarm.Local.Services;

                foreach (SPService curService in services)
                {
                    if (curService is SPWebService)
                    {
                        SPWebService webService = (SPWebService)curService;
                        SPSecurity.RunWithElevatedPrivileges(delegate()
                            {
                                foreach (SPWebApplication webApp in webService.WebApplications)
                                {
                                    // here you can do something with webApp

                                    foreach (SPSite siteCollection in webApp.Sites)
                                    {
                                        using (SPSite site = new SPSite(siteCollection.Url.ToString()))
                                        //(ConfigurationManager.AppSettings["SiteCollUrl"]))
                                        {
                                            SPWebCollection webCollection = site.AllWebs;
                                            if (webCollection.Count > 0)
                                            {
                                                for (int i = 0; i < webCollection.Count; i++)
                                                {
                                                    if (!webCollection.Names[i].Contains("/"))
                                                    {
                                                        Console.WriteLine(webCollection[i].Url + " : " + webCollection[i].Title + " : " +
                                                                            webCollection[i].Description + " : " + webCollection[i].IsRootWeb);
                                                    }
                                                }
                                            }
                                        }
                                        //}
                                        siteCollection.Dispose();
                                    }
                                }
                            });
                    }
                }
            }
            catch (Exception ex)
            {
                writetoCSV("", "", ex.Message, false);
                throw ex;
            }


You can write this to a CSV file or txt file.

If you want a list of all the site collections and sub sites for a given web application use the powershell script listed here:



No comments:

Post a Comment