SharePoint Modern Site PnP Provisioning with PowerShell and Managed C# Code

Let’s see how we can remotely provision artifacts (Composed Look, Pages, Sections, WebParts etc.) to SharePoint Modern Site.

We will use PowerShell and Managed C# Code with PnP Provisioning Engine.


Let’s start with PowerShell. Firstly we need to install PnP PowerShell. Because we use SharePoint Online, we need to install PnP PowerShell for SharePoint Online.

Install-Module SharePointPnPPowerShellOnline

1. Disable Modern Page Experience

We want connection to our SharePoint Online Site Collection which is Modern Site.

$cred = Get-Credential
Connect-PnPOnline -Url -Credentials $cred

We can prevent Modern Site creation with next command.

Disable-PnPFeature -Identity B6917CB1-93A0-4B97-A84D-7CF49975D4EC -Scope Web

On the other hand we can enabled it back.

Enable-PnPFeature -Identity B6917CB1-93A0-4B97-A84D-7CF49975D4EC -Scope Web

2. Create Modern Page

Example below show us how to create new Modern Page named “Test Site”. Than we add three column section on it. On first column we add default Bing Map Web Part.

$cred = Get-Credential
Connect-PnPOnline -Url -Credentials $cred

Add-PnPClientSidePage -Name "Test Site"
Add-PnPClientSidePageSection -Page "Test Site" -SectionTemplate ThreeColumn
Add-PnPClientSideWebPart -Page "Test Site" -DefaultWebPartType BingMap

3. Save Modern Site as XML Template

We want to save our custom home modern page from specific site to XML file which will be applied to other sites later. You could you JSON instead.

$cred = Get-Credential
Connect-PnPOnline -Url -Credentials $cred

Get-PnPProvisioningTemplate -Out "C:\Temp\template.xml" -Handlers PageContents

In code above you can see Handlers parameter. It allows you to only process a specific type of artifact in the site – in my example just page content. More about Get-PnPProvisioningTemplate parameters here.

4. Apply Modern Site Template to specific page

Then we want to apply custom home modern page to the second site like this below:

$cred = Get-Credential
Connect-PnPOnline -Url -Credentials $cred

Apply-PnPProvisioningTemplate C:\Temp\template.xml

[ Complete code on GitHub ]

Managed C# Code

Let’s switch to C# and create new Console Application. Then go to NuGet Package Manager and install SharePointPnPCoreOnline package.

1. Get specific user credetials

Here we want to connect to specific site with specific user credentials.

using (ClientContext ctx = new ClientContext(""))
    Console.Write("Password: ");
    SecureString passWord = GetPassword();

    ctx.Credentials = new SharePointOnlineCredentials("", passWord);

2. Create Modern Page

We want to create new modern page with some simple text.

private static void CreatePage(ClientContext siteContext, string pageName)
    ClientSidePage page = new ClientSidePage(siteContext);

    ClientSideText txt1 = new ClientSideText() { Text = "Hello world!" };
    page.AddControl(txt1, 0);


    Console.WriteLine("Page successfully created.");

3. Modify Modern Page

We want to update our page with another text.

private static void ModifyPage(ClientContext siteContext, string pageName)
    ClientSidePage page = ClientSidePage.Load(siteContext, pageName);

    ClientSideText txt1 = new ClientSideText() { Text = "Hello world 2!" };
    page.AddControl(txt1, 0);


    Console.WriteLine("Page successfully modified.");

4. Add Web Part to Modern Page

We can add Default Web Part or custom SPFx Web Part to existing modern page.

private static void AddDefaultWebPart(ClientContext siteContext, string pageName)
    // add video embed web part..
    ClientSidePage page = new ClientSidePage(siteContext);
    ClientSideWebPart videoEmbedWp = page.InstantiateDefaultWebPart(DefaultClientSideWebParts.VideoEmbed);
    videoEmbedWp.Properties["videoSource"] = "[specify-url-here]";
    videoEmbedWp.Properties["captionText"] = "[specify-title-here]";
    videoEmbedWp.Properties["showInfo"] = false;
    videoEmbedWp.Properties["embedCode"] = "http://[specify-url-here]";
    videoEmbedWp.Title = "Associated video";

    // add Yammer embed web part..
    ClientSideWebPart yammerEmbedWp = page.InstantiateDefaultWebPart(DefaultClientSideWebParts.YammerEmbed);
    yammerEmbedWp.Properties["yammerFeedURL"] = "[specify-url-here]";
    yammerEmbedWp.Title = "Associated Yammer group";


    Console.WriteLine("Default Web Part added.");

private static void AddCustomSPFxWebPart(ClientContext siteContext, string wpName, string pageName, int wpOrder)
    ClientSidePage page = new ClientSidePage(siteContext);

    var components = page.AvailableClientSideComponents();

    var webPartToAdd = components.Where(wp => wp.ComponentType == 1 && wp.Name == wpName).FirstOrDefault();
    if (webPartToAdd != null)
        ClientSideWebPart clientWp = new ClientSideWebPart(webPartToAdd) { Order = wpOrder };


5. Add Section to Modern Page

We want to create modern page with two sections – three column first and two column after that.

var page = ctx.Web.AddClientSidePage("Test page.aspx", true);
page.AddSection(CanvasSectionTemplate.ThreeColumn, 5);
page.AddSection(CanvasSectionTemplate.TwoColumn, 10);

[ Complete code on GitHub ]

Gašper Rupnik



2 thoughts on “SharePoint Modern Site PnP Provisioning with PowerShell and Managed C# Code

Add yours

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Powered by

Up ↑

%d bloggers like this: