Create SharePoint Subsite Using PnP Core Component

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”.

There are two extension methods available for creating subsite,

  1. CreateWeb(SiteEntity subsite, boolinheritPermissions = true, boolinheritNavigation = true)   
Parameters Description
subsite Details of the Web (site) to add. Only Title, Url (as the leaf URL), Description, Template and Language are used.
inheritPermissions Specifies whether the new site will inherit permissions from its parent site.
inheritNavigation Specifies whether the site inherits navigation.
  1. CreateWeb(string title, stringleafUrl, string description, string template, int language, boolinheritPermissions = true, boolinheritNavigation = true)   
Parameters Description
title The title of the new site.
leafUrl A string that represents the URL leaf name.
description The description of the new site.
template The name of the site template to be used for creating the new site.
language The locale ID that specifies the language of the new site.
inheritPermissions Specifies whether the new site will inherit permissions from its parent site.
inheritNavigation Specifies whether the site inherits navigation.

Follow the steps below to create application with required references used for subsite creation in SharePoint.

  • 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. This code sample creates a subsite called “Sample Site” with url as “samplesite” under root web of the site collection.

Code Snippet

  1. using System;  
  2. usingSystem.Collections.Generic;  
  3. usingSystem.Linq;  
  4. usingSystem.Text;  
  5. usingSystem.Threading.Tasks;  
  6. usingMicrosoft.SharePoint.Client;  
  7. usingOfficeDevPnP.Core;  
  8. namespaceCreatSubSiteApplication  
  9. {  
  10.     classProgram  
  11.     {  
  12.         staticvoid Main(string[] args)  
  13.         {  
  14.             stringsiteUrl = "https://mycompany.sharepoint.com";  
  15.             boolisInheritPermissions = false;  
  16.             boolisInheritNavigation = false;  
  17.             AuthenticationManagerauthManager = newAuthenticationManager();  
  18.             //Interactive Login to SharePoint site - Opens anOffice 365sign in page to authenticate the user  
  19.             var context = authManager.GetWebLoginClientContext(siteUrl);  
  20.             try  
  21.             {  
  22.                 Webweb = context.Site.RootWeb.CreateWeb(  
  23.                     newOfficeDevPnP.Core.Entities.SiteEntity()  
  24.                     {  
  25.                         Title = "Sample Site",  
  26.                             Url = "samplesite",  
  27.                             Description = "Site creating for testing purpose",  
  28.                             Template = "STS#0",  
  29.                             Lcid = 1033  
  30.                     }, isInheritPermissions, isInheritNavigation);  
  31.                 Console.WriteLine(web.Title + " site created successfully");  
  32.             }  
  33.             catch (Exception ex)  
  34.             {  
  35.                 Console.WriteLine(ex.Message);  
  36.             }  
  37.             Console.Read();  
  38.         }  
  39.     }  
  40. }  
Run the application by Pressing F5 to see the output.

Output

Sample Site was created successfully. 

Summary

In this article you will see how to use PnP core component for creating a subsite.