Friday, May 11, 2018

PnP PowerShell to get and apply the site template


Provision a site using PnP PowerShell


Most of the time we struggle to install the PnP online PowerShell modules. Due to not enough permission on system as administrator we get stuck or keep waiting for the installation done by an administrator. Here is an easy and quick way to get the PnP PowerShell install for current user logged in

Install-Module SharePointPnPPowerShellOnline -Scope CurrentUser


In the process of installation of this module it will download and install other dependent software’s. So keep saying ‘Yes’ for the pop up or messages you get during installation.

Create a site template using PnP

Once your PnP Modules are been installed. You can start using the PnP commands.


#Connect to SP Site

$username = "xyz@domain.com"
$siteURL = "https://abcd/template/"

try
{
$cred = Get-Credential -UserName $username -Message "Please enter password for $username"

    Connect-PnPonline -Url $siteURL -Credentials $cred

    Write-Host "Connected to Site"

    #Get Site template of the Site URL mentioned above
   
    Get-PnPProvisioningTemplate -Out "D:\SiteTemplate\template1.xml"

    Write-Host "Site template created"
} 
catch{ 
    write-host "$($_.Exception.Message)" -foregroundcolor red 
} 

Apply site template using PnP


#Connect to SP Site

$username = "xyz@domain.com"
$siteURL = "https://abcd/template/"
$subSite = "Site2"
$subSiteTitle = "Site2 Title"

try
{
$cred = Get-Credential -UserName $username -Message "Please enter password for $username"

    Connect-PnPonline -Url $siteURL -Credentials $cred

    Write-Host "Connected to Site"
-Template "BLANKINTERNET#0"

    #Apply Site template of the Site URL mentioned above

    Apply-PnPProvisioningTemplate -Web $web -Path "D:\SiteTemplate\template1.xml"


    Write-Host "Site template Applied"
} 
catch{ 
    write-host "$($_.Exception.Message)" -foregroundcolor red 
}