I have found one of the very useful code below to delete all sub sites in a site collection.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
namespace SiteDeletion
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Executing program");
//Target site to delete sub sites from
string siteURL = "http://YourSiteURL/";
SPSecurity.RunWithElevatedPrivileges(delegate()
{
Console.WriteLine("Opening site");
using (SPSite oSite = new SPSite(siteURL))
{
Console.WriteLine("Opening web");
using (SPWeb osubWeb = oSite.OpenWeb())
{
Console.WriteLine("Getting sub sites");
SPWebCollection subSites = osubWeb.GetSubwebsForCurrentUser();
Console.WriteLine("looping");
foreach (SPWeb subWeb in subSites)
{
//If you need to keep sites of a particular name
if (subWeb.Title.Contains("Template"))
{
Console.WriteLine("NOT deleteing site: " + subWeb.Title);
}
else
{
Console.WriteLine("DELETING: " + subWeb.Title);
DeleteWeb(subWeb);
}
}
}
}
});
//Console.ReadLine();
}
public static void DeleteWeb(SPWeb web)
{
SPWebCollection subwebSites = web.GetSubwebsForCurrentUser();
if (subwebSites.Count > 0)
{
foreach (SPWeb w in subwebSites)
{
DeleteWeb(w);
}
}
web.Delete();
return;
}
}
}
Reference
http://simplisticsharepoint.blogspot.sg/2013/03/programmatically-delete-all-sites-in.html
No comments:
Post a Comment