PnP core component contains the extension methods which help to achieve the SharePoint task in simple lines of code. So to know more about PnP Core component, have a look at “Introduction to PnP Core Component”.
WebExtensions.DeleteWeb (PnP Core Component) – This extension method first checks the subsite available in the specified url and then delete that subsite from the parent web.
Method
boolDeleteWeb(stringsubsiteUrl)
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 there's a problem in deleting the child site, this method returns false.
- Create a console application.
- Add PnP core component assembly references to the application. Refer “Introduction to PnP Core Component” to add a references to the VS solution.
- Add Using Microsoft.SharePoint.Client and Using OfficeDevPnP.Core.Extensions as using statement to the top of the file.
- Add the below code snippet and change the values where ever necessary.
- using System;
- usingSystem.Collections.Generic;
- usingSystem.Linq;
- usingSystem.Text;
- usingSystem.Threading.Tasks;
- usingMicrosoft.SharePoint.Client;
- usingOfficeDevPnP.Core;
-
-
- namespaceSampleApplication
- {
- classProgram
- {
- staticvoid Main(string[] args)
- {
- stringsiteUrl = "https://sharepointonline.sharepoint.com";
- AuthenticationManagerauthManager = newAuthenticationManager();
-
- var context = authManager.GetWebLoginClientContext(siteUrl);
-
- 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();
- }
- }
- }
The above codeis also available from GitHub.