Shantha Kumar T
WebExtensions.CreateWeb (PnP Core Component)
WebExtensions.CreateWeb – This CSOM extension method from PnP Core Component used to create a new sub-site under the parent web in SharePoint.
Supports: SharePoint 2013+, SharePoint Online
Assembly: OfficeDevPnP.Core.dll
Namespace: Microsoft.SharePoint.Client
There are two extension CreateWeb methods are provided in PnP Core Component for creating a subsite under WebExtensions class.
Method 1:
Web CreateWeb(SiteEntity subsite, bool inheritPermissions = true, bool inheritNavigation = true)
Parameters:
Parameter | Type | Description |
subsite | string | Details of the Web (site) to add. Only Title, Url (as the leaf URL), Description, Template and Language are used. |
inheritPermissions | bool | Specifies whether the new site will inherit permissions from its parent site. Default value is true. |
inheritNavigation | bool | Specifies whether the site inherits navigation. Default value is true. |
Example:
The following code snippet example used to add a new subsite “Sample Site” with url “/samplesite” under the root website in SharePoint. And the newly added site is not inhering permissions and navigation from parent site.
[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";
bool isInheritPermissions = false;
bool isInheritNavigation = false;
AuthenticationManager authManager = new AuthenticationManager();
//Interactive Login to SharePoint site – Opens a Online signin page to authenticate the user
var context = authManager.GetWebLoginClientContext(siteUrl);
Web web = context.Site.RootWeb.CreateWeb(
new OfficeDevPnP.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!");
Console.WriteLine("Press any key to exit.");
Console.ReadKey();
}
}
}
//OUTPUT:
//Sample Site site created sucessfully!
//Press any key to exit.
[/code]
Method 2:
Web CreateWeb(string title, string leafUrl, string description, string template, int language, bool inheritPermissions = true, bool inheritNavigation = true)
Parameters:
Parameter | Type | Description |
title | string | The title of the new site. |
leafUrl | string | A string that represents the URL leaf name. |
description | string | The description of the new site. |
template | string | The name of the site template to be used for creating the new site. |
language | int | The locale ID that specifies the language of the new site. |
inheritPermissions | bool | Specifies whether the new site will inherit permissions from its parent site. Default value is true. |
inheritNavigation | bool | Specifies whether the site inherits navigation. Default value is true. |
Example:
The following code snippet example used to add a new subsite “Sample Site” with url “/samplesite” under the root website in SharePoint. And the newly added site is not inhering permissions and navigation from parent site.
[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";
bool isInheritPermissions = false;
bool isInheritNavigation = false;
AuthenticationManager authManager = new AuthenticationManager();
//Interactive Login to SharePoint site – Opens a Online signin page to authenticate the user
var context = authManager.GetWebLoginClientContext(siteUrl);
Web web = context.Site.RootWeb.CreateWeb(
"Sample Site",
"samplesite",
"Site creating for testing purpose",
"STS#0", 1033, isInheritPermissions, isInheritNavigation);
Console.WriteLine(web.Title + " site created successfully!");
Console.WriteLine("Press any key to exit.");
Console.ReadKey();
}
}
}
//OUTPUT:
//Sample Site site created successfully!
//Press any key to exit.
[/code]