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("https://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 "https://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
}
}
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
This is good site to refer.
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?
Great article. Really it’s helpful to work with iteration throughout the SharePoint object model.
This is great, but what about disposing of your objects that implement idisposable?
Yeah, super info, thx!
Awesome blog. like the fact that you gave the C# code and the method to use for PowerShell
Useful stuff, thanks.
Great blog! I found a few useful articles on your site – well done!
Yes.. useful article
nice article on Sharepoint Object model objects iteration
Yes Good Article about SPobject model itearting…