Tuesday, June 12, 2012

Iterate through All SharePoint Web Applications, Site Collections, Sub-sites, Lists and List Items Programmatically

Often in development, we may have to iterate through all web applications, Site collections, sites, Lists and List Items to get some job done or generate reports. Here are the code snippets to help.

Iterate Through all web applications, Site collections, Sites, Lists and List Items Using C#:

  
      static void Main(string[] args)
        {
                    SPFarm farm = SPFarm.Local;
                   //Get all SharePoint Web services
                    SPWebService service = farm.Services.GetValue<SPWebService>("");

                    foreach (SPWebApplication webapp in service.WebApplications)
                    {
                         Console.WriteLine(webapp.Name);
                        //Enumerate through each site collection
                        foreach (SPSite site in webapp.Sites)
                        {  
                            //Console.WriteLine(site.Url);
                            
                            //Enumerate through each sub-site
                            foreach (SPWeb web in site.AllWebs)
                            {  
                                //Console.WriteLine(web.Title);

                                //Enumerate through each List
                                foreach (SPList list in web.Lists)
                                {
                                    //Console.WriteLine(list.Title);
                                   //Enumerate through each list item
                                     foreach(SPListItem Item in list.Items)
                                     {
                                        //do something
                                     }

                                 }
                               
                            }
                          
                        }
                    }
                     //To pause: 
                    Console.ReadLine();
                }

Starting the Loop from Web Application:
                
                string site;
                Console.WriteLine("Enter the Web Application URL:");
                site = Console.ReadLine();
         
                SPSite tmpRoot = new SPSite(site);
                SPSiteCollection tmpRootColl = tmpRoot.WebApplication.Sites;

                //Enumerate through each site collection
                foreach (SPSite tmpSite in tmpRootColl)
                {
                   //Enumerate through each sub-site
                    foreach (SPWeb tmpWeb in tmpSite.AllWebs)
                    {
                        //Enumerate through each List
                        foreach (SPList tmpList in tmpWeb.Lists)
                        {
                            //Do something
                        }
                     }                 
                 }

You can directly start from Web Application URL also: Lets say, you want to find the content database of all site collection in a web application:

static void Main(string[] args)
        {
            SPWebApplication webApp = SPWebApplication.Lookup(new Uri("http://sharepoint.crescent.com"));

            foreach (SPContentDatabase cdb in webApp.ContentDatabases)
            {
                foreach (SPSite oSPSite in cdb.Sites)
                {
                    Console.WriteLine("Site:" +oSPSite.RootWeb.Title +" at "+oSPSite.RootWeb.Url + " lives in the database:" + cdb.Name);
                }
            }
            Console.ReadLine();
        }

Iterate Through all Web applications, Site collections, Sites, Lists and List Items With PowerShell:

# Get All Web Applications 
$WebApps=Get-SPWebApplication
foreach($webApp in $WebApps)
{ 
    foreach ($SPsite in $webApp.Sites)
    {
       # get the collection of webs
       foreach($SPweb in $SPsite.AllWebs)
        {
            write-host $SPweb.title ":" $spweb.URL
               foreach($list in $web.lists)
               {
                  foreach($item in $list.items)
                      {
                         Write-Host $item.Title
                      }
                 
               }
        }
    }
  } 

Another Shortcut Method: 
Get-SPWebApplication | Get-SPSite | Get-SPWeb -Limit All | ForEach-Object{ write-host Site Name: $_.Title, URL: $_.URL}

E.g. If you want to get all subsites to CSV File:
  
Get-SPSite "http://sharepoint.company.com/sales/" | Get-SPWeb -Limit All | Select Title, Url | Export-Csv -NoTypeInformation -Path "c:\sites.csv"

Iterate Through all Content Databases to get site collections in the DB:
#Get all SharePoint content Databases
$ContentDatabases=  Get-SPContentDatabase 

#Iterate through each Database
Foreach($Database in $ContentDatabases)
{
    Write-host "`nContent Database: "$Database.Name 
    #Get all site collections of the Database
    $Sites = $Database.Sites
    foreach($site in $Sites)
     {
     Write-host $Site.URL
     }
}
     

Similar reading: http://gallery.technet.microsoft.com/Maxer-for-SharePoint-2010-8cd0f26f



You might also like:
SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Document SharePoint Farm
Automatically generate SharePoint documentation.
*Sponsored


Check out these SharePoint products:

9 comments :

  1. Yes Good Article about SPobject model itearting...

    ReplyDelete
  2. nice article on Sharepoint Object model objects iteration

    ReplyDelete
  3. Great blog! I found a few useful articles on your site - well done!

    ReplyDelete
  4. Useful stuff, thanks.

    ReplyDelete
  5. Awesome blog. like the fact that you gave the C# code and the method to use for PowerShell

    ReplyDelete
  6. Yeah, super info, thx!

    ReplyDelete
  7. This is great, but what about disposing of your objects that implement idisposable?

    ReplyDelete
  8. Great article. Really it's helpful to work with iteration throughout the SharePoint object model.

    ReplyDelete

Please Login and comment to get your questions answered!

You might also like:

Related Posts Plugin for WordPress, Blogger...