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,
- 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. |
- 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
- using System;
- usingSystem.Collections.Generic;
- usingSystem.Linq;
- usingSystem.Text;
- usingSystem.Threading.Tasks;
- usingMicrosoft.SharePoint.Client;
- usingOfficeDevPnP.Core;
- namespaceCreatSubSiteApplication
- {
- classProgram
- {
- staticvoid Main(string[] args)
- {
- stringsiteUrl = "https://mycompany.sharepoint.com";
- boolisInheritPermissions = false;
- boolisInheritNavigation = false;
- AuthenticationManagerauthManager = newAuthenticationManager();
-
- var context = authManager.GetWebLoginClientContext(siteUrl);
- try
- {
- Webweb = context.Site.RootWeb.CreateWeb(
- newOfficeDevPnP.Core.Entities.SiteEntity()
- {
- Title = "Sample Site",
- Url = "samplesite",
- Description = "Site creating for testing purpose",
- Template = "STS#0",
- Lcid = 1033
- }, isInheritPermissions, isInheritNavigation);
- Console.WriteLine(web.Title + " site created successfully");
- }
- catch (Exception ex)
- {
- Console.WriteLine(ex.Message);
- }
- Console.Read();
- }
- }
- }
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.