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
	}
}

Salaudeen Rajack

Salaudeen Rajack is a SharePoint Architect with Two decades of SharePoint Experience. He loves sharing his knowledge and experiences with the SharePoint community, through his real-world articles!

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

  • November 16, 2017 at 7:29 AM

    Could you please show Iterate Through all Web applications, Site collections, Sites, Lists and List Items With PowerShell and using CSOM object model for sharepoint onlien

    Reply
  • January 20, 2017 at 8:28 AM

    This is good site to refer.

    Reply
  • January 20, 2017 at 8:25 AM

    Hi, this is good article for developers ,But I have some doubts on this article
    Iterate Through all web applications, Site collections, Sites, Lists and List Items

    I can iterate up to this loop foreach (SPSite site in webapp.Sites)

    I try to iterate web application based on selected spsite but it is not working fine.

    How to iterate on selected item in sp site I should get only websites under this site?

    Reply
  • October 7, 2015 at 9:30 AM

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

    Reply
  • July 25, 2014 at 8:47 PM

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

    Reply
  • July 9, 2014 at 10:40 AM

    Yeah, super info, thx!

    Reply
  • October 4, 2013 at 9:41 AM

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

    Reply
  • February 21, 2013 at 7:56 PM

    Useful stuff, thanks.

    Reply
  • February 5, 2013 at 7:46 PM

    Great blog! I found a few useful articles on your site – well done!

    Reply
  • November 7, 2012 at 5:34 PM

    Yes.. useful article

    Reply
  • November 7, 2012 at 5:26 PM

    nice article on Sharepoint Object model objects iteration

    Reply
  • September 29, 2012 at 10:16 AM

    Yes Good Article about SPobject model itearting…

    Reply

Leave a Reply