Delete child site from SharePoint using CSOM (PnP Core Component)

This blog contains the code-snippet used to delete the child site based on the provided url.

WebExtensions.DeleteWeb (PnP Core Component) – This extension method first checks the ahild site is available in the specified url and then delete that child site from the parent web.

Supports: SharePoint 2013+, SharePoint Online

Assembly: OfficeDevPnP.Core.dll
Namespace: Microsoft.SharePoint.Client

Method:

bool DeleteWeb(string subsiteUrl)

Parameters:

Parameter Type Description
subsiteUrl string A string that represents the URL leaf name of a subsite
Return Type Description
bool true if the web was deleted; otherwise false if nothing was done

Syntax:

Single Line Code: Web.DeleteWeb(“subsiteurl”)

Code Snippet:

The following example deletes the subsite from the parent website based on the given url and returns true value. If the subsite is not available or problem in deleting the child site, this method returns false.

[code lang=”csharp”]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.SharePoint.Client;
using OfficeDevPnP.Core;

// Assembly Reference Used: OfficeDevPnP.Core, Version=2.4.1605.0, Culture=neutral, PublicKeyToken=3751622786b357c2
namespace SampleApplication
{
class Program
{
static void Main(string[] args)
{
string siteUrl = "https://sharepointonline.sharepoint.com";

AuthenticationManager authManager = new AuthenticationManager();
//Interactive Login to SharePoint site – Opens a Online signin page to authenticate the user
var context = authManager.GetWebLoginClientContext(siteUrl);
//Deletes the child website with the specified subsite URL, from a parent Web, if it exists. Else the method return false value.
bool success = context.Web.DeleteWeb("subsiteurl");

if (success)
Console.WriteLine("Site deleted successfully.");
else
Console.WriteLine("Problem in deleting the site.");

Console.WriteLine("Press any key to exit.");
Console.ReadKey();

}
}
}

[/code]

The above code also available from GitHub

Shantha Kumar
Shantha Kumar
Articles: 280

24,849 Comments

  1. Truely a very good article on how to handle the future technology. After reading your post,thanks for taking the time to discuss this, I feel happy about and I love learning more about this topic. keep sharing your information regularly for my future reference. This content creates a new hope and inspiration with in me. Thanks for sharing article like this. The way you have stated everything above is quite awesome. Keep blogging like this. Thanks.

Comments are closed.