Home > Blogs > Blog Post

Code Sample : How to programmatically delete a web (and it's child webs)

by Mark Jones | Dec 27, 2011

If you have ever tried to delete a web either in code, or via the SharePoint GUI, then you will note that isn't possible if the web has one or more child webs. (or grandchildren). To delete a web, you must first delete it's subwebs.

The code below illustrates this in C# :

public static bool DeleteWeb(SPWeb parentWeb, string webUrl, bool deleteSubWebs)
{
 try
    {
        SPWeb webToDelete = parentWeb.Webs[webUrl];
 if (webToDelete != null)
        {
 if (deleteSubWebs)
            {
                DeleteSubWebs(webToDelete.Webs);
            }
 
            webToDelete.Delete();
        }
 
        LogHelper.LogMethodEnd(logger, "SPWebHelper", "DeleteWeb");
 return true;
    }
 catch (Exception ex)
    {
 return false;
    }
}
 
private static void DeleteSubWebs(SPWebCollection webs)
{
    LogHelper.LogMethodStart(logger, "SPWebHelper", "DeleteSubWebs");
 
 foreach (SPWeb web in webs)
    {
 if (web.Webs.Count > 0)
            DeleteSubWebs(web.Webs);
        web.Delete();
    }
 
    LogHelper.LogMethodEnd(logger, "SPWebHelper", "DeleteSubWebs");
}

Compliance using DocRead

DocRead logoDocRead for SharePoint can help you manage policy compliance by:

  • Targeting documents or policies at specific groups of users
  • Allowing a specific amount of time for users to confirm agreement 
  • Sending email reminders when policy compliance is overdue
  • Users self-certify that they have read and fully understood the policy details
  • Securely storing records of confirmed policy acceptance
  • Monitoring the user acceptance of policies via a reporting suite
  • Providing detailed reading reports and statistics
  • Report drill through to show who has not accepted the policy
  • Automatically sending historic compliance tasks and policies to new users when they are added to a group
  • Bringing policy compliance requests immediately to users attention when they log on

DocRead is simple to install and configure. It seamlessly integrates with SharePoint and can be added to any existing SharePoint site.

To find out more, visit the DocRead product site.


blog comments powered by Disqus