Compartilhar via


Pruning a web from a Site Collection

A Web within a WSS Site collection cannot be removed without first removing all subwebs beneath it.

The following sample code shows how to programmatically "Prune" a web from a site collection


// Sample code to prune a web from a site collection

// BEGIN PruneWeb.cs

using System;

using Microsoft.SharePoint;

namespace SPPruneWeb

{

      class AppClass

      {

            static public string m_strUrl = null;

            [STAThread]

            static void Main(string[] args)

            {

                  // Collect arguments

                  //

                  for(int iCnt=0; iCnt < args.Length; iCnt++)

      {

                        switch (args[iCnt])

                        {

                              case "-url":

                                    m_strUrl = args[++iCnt];

                                    break;

                        }

                  }

                  if(m_strUrl == null)

                  {

                        Console.WriteLine("No web specified!");

                        return;

                  }

                  SPSite site = new SPSite(m_strUrl);

                  SPWeb web;

                  if (m_strUrl.Length == site.Url.Length)

                  {

                        web = site.RootWeb;

                  }

                  else

                  {

                        string sRelURL = m_strUrl.Substring(site.Url.Length + 1);

                        web = site.OpenWeb(sRelURL);

                  }

                  PruneWeb(web);

                  return;

            }

           

            static public void PruneWeb(SPWeb web)

            {

                  Console.WriteLine("Pruning Web URL " + web.Url);

                  if( web.Webs.Count > 0)

                  {

                        foreach(SPWeb subweb in web.Webs)

                        {

                              PruneWeb(subweb);

                        }

                  }

                  try

                  {

                        web.Delete();

                  }

                  catch(Exception e)

                  {

                        Console.WriteLine("Problem removing web " + e.Message);

                  }

            }

      }

}

// END PruneWeb.cs

Comments