Create list in SharePoint online using PnP PowerShell

Top 2 best ways to create list in SharePoint online using PnP PowerShell provisioning template

One comment

 88,995 total views,  7 views today

Create list in SharePoint online using PnP PowerShell – in this article, we will learn about how to create a SharePoint online list using the PnP provisioning template and how to create a list template from SharePoint Online using PnP PowerShell.

Demo:

Create SharePoint online list using...
Create SharePoint online list using PnP provisioning template

Introduction to PnP provisioning template

What is PnP provisioning?

With the introduction of Microsoft Office 365 and Microsoft SharePoint Online, developers are facing the new Cloud Add-in model as a new way of creating custom software solutions for Microsoft SharePoint, SharePoint Online, and Office 365 in general. However, earlier SharePoint developers used to write the SharePoint solution in CAML/XML-based Feature Framework, either with full trust solutions (farm-based) or sandbox solutions.

Now, Microsoft has introduced many modern ways to customize or write SharePoint solutions instead of the featured-based frameworks, PnP is one among them. PnP is an engine(software tool), it must be installed in the machine where you want to work, it can be installed either thru the .exe installer or PowerShell. It is completely remote-based, developed on the client object model, and developer, administrator, and SharePoint users seamlessly can communicate to SharePoint objects remotely – without logging in to the server(in the case of SharePoint on-premise).

Types of Template in PnP – PnP provisioning template

There are basically two types of templates in PnP:

  • Site Templates(also called Provisioning Templates) and the extended version(Tenant Templates)
  • Tenant Templates.

I am not going into the template details like the scope of this article to create a SharePoint list or library using the PnP provisioning template.

Use cases of PnP provisioning template in a SharePoint list creation

In this article, we will learn about the below two templates techniques:

  • Create a SharePoint list using the PnP template provisioning (template created manually)
  • Get a PnP template from an existing list and provision another list.

Create a SharePoint list using the PnP template provisioning (template created manually) – PnP provisioning template

Below is the default PnP template XML, using this we can provision or create any list on SharePoint Online site.

<?xml version="1.0"?>
<pnp:Provisioning xmlns:pnp="http://schemas.dev.office.com/PnP/2018/07/ProvisioningSchema">
  <pnp:Preferences Generator="OfficeDevPnP.Core, Version=3.7.1903.0, Culture=neutral, PublicKeyToken=5e633289e95c321a" />
  <pnp:Templates ID="CONTAINER-TEMPLATE-C1181F206BE84C09AAAC4B3CB21FBE32">
    <pnp:ProvisioningTemplate ID="TEMPLATE-C1181F206BE84C09AAAC4B3CB21FBE32" Version="1" Scope="Web">
      <pnp:Lists>
        <pnp:ListInstance Title="Customer List Feb 2020" TemplateType="100" Url="Lists/CustomerListFeb2020" EnableFolderCreation="false">
          <pnp:ContentTypeBindings>
            <pnp:ContentTypeBinding ContentTypeID="0x01" Default="true" />
            <pnp:ContentTypeBinding ContentTypeID="0x0120" />
          </pnp:ContentTypeBindings>
          <pnp:Views>
            <View Name="AllItems" DefaultView="TRUE" Type="HTML" DisplayName="All Items" Level="1" BaseViewID="1" ContentTypeID="0x">
              <Query>
                <OrderBy>
                  <FieldRef Name="ID" />
                </OrderBy>
              </Query>
              <ViewFields>
                <FieldRef Name="LinkTitle" />
                <FieldRef Name="CustomerID" />
                <FieldRef Name="Name" />
                <FieldRef Name="PhoneNumber" />
                <FieldRef Name="Address" />
                <FieldRef Name="Country" />                
                <FieldRef Name="Email" />
				<FieldRef Name="Notes" />
              </ViewFields>
              <RowLimit Paged="TRUE">30</RowLimit>
            </View>
          </pnp:Views>
          <pnp:Fields>
            <Field ID="{1212a012-1012-4cd9-900b-411f01f2b112}" DisplayName="CustomerID" Name="CustomerID" Type="Text"/>
            <Field ID="{3450c003-7607-46de-8345-6c64cd2a3345}" DisplayName="Name" Name="Name" Type="Text"/>
            <Field ID="{70ef44ad-091f-4955-a957-b0fb9b42833b}" DisplayName="PhoneNumber" Name="PhoneNumber" Type="Text"/>
            <Field ID="{156ada44-fd5f-4ed2-85af-d9be52a79f73}" DisplayName="Address" Name="Address" Type="Text"/>
            <Field ID="{f3340212-afe4-404e-bbe3-06edf38e4e12}" DisplayName="Country" Name="Country" Type="Text"/>
			<Field ID="{6c612e60-e32d-4d34-9e38-32fadedc9341}" DisplayName="Email" Name="Email" Type="Text"/>
			<Field ID="{d9c0a3c9-b572-3412-92d2-35ebc603bed8}" DisplayName="Notes" Name="Notes" Type="Text"/>
          </pnp:Fields>           
            
        </pnp:ListInstance>
      </pnp:Lists>
    </pnp:ProvisioningTemplate>
  </pnp:Templates>
</pnp:Provisioning>

 

Description About the above PnP template XML: PnP provisioning template

  • Using the above template we can quickly create a SharePoint list and create columns to the list, exactly the way we do create columns in the SharePoint list or library.
  • <pnp:ListInstance> tag:

In this tag, we can pass the list title, list types like custom list, announcement list or library, etc., and list URL like below:

<pnp:ListInstance Title=”Customer List Feb 2020″ TemplateType=”100″ Url=”Lists/CustomerListFeb2020″ EnableFolderCreation=”false”>

We will get the various template types available in SharePoint from my other article “List Template IDs In SharePoint Online/SharePoint 2019/2016/2013/2010/2007

  • <pnp:Views> tag:

Using this we can configure the default view “AllItems” along with the columns to be displayed in the view, like below:

<View Name="AllItems" DefaultView="TRUE" Type="HTML" DisplayName="All Items" Level="1" BaseViewID="1" ContentTypeID="0x"><FieldRef Name="ID" /><FieldRef Name="LinkTitle" /><FieldRef Name="CustomerID" /><FieldRef Name="Name" /><FieldRef Name="PhoneNumber" /><FieldRef Name="Address" /><FieldRef Name="Country" /><FieldRef Name="Email" /><FieldRef Name="Notes" /><RowLimit Paged="TRUE">
  • <pnp:Fields> tag:
  1. In this section, we can pass all column details those need to be created in the list.
  2. In the field ID, we can give any GUID format value.
  3. The “DisplayName” indicates the display name of the column, the “name” indicates the internal name of the column and the “Type” indicates the type of the column, like text, number, etc.

Get to know about the difference between the display and the internal name of the column from this article “SharePoint On-Premise – Online O365 : Static Name vs. Internal Name vs. Display Name in SharePoint

 

Note (take-away):

Now we can say that by changing the TemplateType value in this XML we can create any type of list or library using this inbuilt XML.

Create list in SharePoint online using PNP PowerShell – script execution

Execute the below PnP PowerShell script to create a custom list.

############################Description#######################################################
#The below script will create a list in SharePoint online using PnP template provisioning template.
##############################################################################################

CLS
$userName = "Global-sharepoint2019@globalsharepoint2019.onmicrosoft.com"
$passWord = "YourSPOPassword"
$encPassWord = convertto-securestring -String $passWord -AsPlainText -Force
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $userName, $encPassWord

Connect-PnPOnline -Url "https://globalsharepoint2019.sharepoint.com/sites/SharePointRNDClassic" -Credentials $cred

Apply-PnPProvisioningTemplate -Path "C:\Temp\PnP\SPListPnPProvisioning\ListTemplate.xml"

Write-host "Successfully a list has been created in SharePoint online."

Execution of the script while in progress:

PnP provisioning template - create a SharePoint list using PnP Provisioning execution in progress
Create a SharePoint list using PnP Provisioning execution in progress

Execution of the script is completed:

PnP provisioning template - create a SharePoint list using PnP Provisioning - execution completed
Create a SharePoint list using PnP Provisioning – execution completed

Verify the list on SharePoint online site:

Create a SharePoint list using PnP Provisioning - PnP provisioning template
Create a SharePoint list using PnP Provisioning – verification

Verify the columns are created in the list:

PnP provisioning template - create a SharePoint list using PnP Provisioning - columns verification
Create a SharePoint list using PnP Provisioning – columns verification

Create a list using PnP Provisioning Template Demo

In the above demo, we have seen how to create a SharePoint custom list using the PnP provisioning template, now we’ll see how to create a complex SharePoint list from the existing list template using the PnP provisioning template.

Generate a PnP template from an existing list and provision another list – PnP provisioning template

In the below example, we will see how we can generate a PnP template from an existing list and provision another list.

############################Description#######################################################
#The below script will download a PnP provisioning list template using PnP.
##############################################################################################

CLS
$userName = "Global-sharepoint2019@globalsharepoint2019.onmicrosoft.com"
$passWord = "YourSPOPassword"
$encPassWord = convertto-securestring -String $passWord -AsPlainText -Force
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $userName, $encPassWord

Connect-PnPOnline -Url "https://globalsharepoint2019.sharepoint.com/sites/SharePointRNDClassic" -Credentials $cred
#Apply-PnPProvisioningTemplate -Path "C:\Temp\PnP\SPListPnPProvisioning\ListTemplate.xml"
#Write-host "Successfully a list has been created in SharePoint online."

Get-PnPProvisioningTemplate -Out "C:\Temp\PnP\SPListPnPProvisioning\ListTemplateDownloaded.xml" -Handlers Lists
Write-host "Successfully list PnP template has been downloaded."



Let’s execute the above script:

Create list in SharePoint online using PnP PowerShell

Once we execute the above script we’ll get the below-consolidated list templates:

<?xml version="1.0"?>
<pnp:Provisioning xmlns:pnp="http://schemas.dev.office.com/PnP/2019/09/ProvisioningSchema">
  <pnp:Preferences Generator="OfficeDevPnP.Core, Version=3.16.1912.0, Culture=neutral, PublicKeyToken=5e633289e95c321a" />
  <pnp:Templates ID="CONTAINER-TEMPLATE-66E0B2B712B145C6943A0D8D74962F7A">
    <pnp:ProvisioningTemplate ID="TEMPLATE-66E0B2B712B145C6943A0D8D74962F7A" Version="1" BaseSiteTemplate="STS#0" Scope="RootSite">
      <pnp:Lists>
        <pnp:ListInstance Title="Customer List Feb 2020" Description="" DocumentTemplate="" TemplateType="100" Url="Lists/CustomerListFeb2020" MinorVersionLimit="0" MaxVersionLimit="0" DraftVersionVisibility="0" TemplateFeatureID="00bfea71-de22-43b2-a848-c05709900100" EnableFolderCreation="false" DefaultDisplayFormUrl="{site}/Lists/CustomerListFeb2020/DispForm.aspx" DefaultEditFormUrl="{site}/Lists/CustomerListFeb2020/EditForm.aspx" DefaultNewFormUrl="{site}/Lists/CustomerListFeb2020/NewForm.aspx" ImageUrl="/_layouts/15/images/itgen.png?rev=47" IrmExpire="false" IrmReject="false" IsApplicationList="false" ValidationFormula="" ValidationMessage="">
          <pnp:ContentTypeBindings>
            <pnp:ContentTypeBinding ContentTypeID="0x01" Default="true" />
            <pnp:ContentTypeBinding ContentTypeID="0x0120" />
          </pnp:ContentTypeBindings>
          <pnp:Views>
            <View Name="{9212FE0C-2D07-420F-BDA9-A137ED3276A5}" DefaultView="TRUE" Type="HTML" DisplayName="All Items" Url="{site}/Lists/CustomerListFeb2020/All Items.aspx" Level="1" BaseViewID="1" ContentTypeID="0x" ImageUrl="/_layouts/15/images/generic.png?rev=47">
              <Query>
                <OrderBy>
                  <FieldRef Name="ID" />
                </OrderBy>
              </Query>
              <ViewFields>
                <FieldRef Name="LinkTitle" />
                <FieldRef Name="CustomerID" />
                <FieldRef Name="Name" />
                <FieldRef Name="PhoneNumber" />
                <FieldRef Name="Address" />
                <FieldRef Name="Country" />
                <FieldRef Name="Email" />
                <FieldRef Name="Notes" />
              </ViewFields>
              <RowLimit Paged="TRUE">30</RowLimit>
              <Aggregations Value="Off" />
              <JSLink>clienttemplates.js</JSLink>
            </View>
          </pnp:Views>
          <pnp:Fields>
            <Field ID="{1212a012-1012-4cd9-900b-411f01f2b112}" DisplayName="CustomerID" Name="CustomerID" Type="Text" SourceID="{{listid:Customer List Feb 2020}}" StaticName="CustomerID" ColName="nvarchar4" RowOrdinal="0" />
            <Field ID="{3450c003-7607-46de-8345-6c64cd2a3345}" DisplayName="Name" Name="Name" Type="Text" SourceID="{{listid:Customer List Feb 2020}}" StaticName="Name" ColName="nvarchar5" RowOrdinal="0" />
            <Field ID="{70ef44ad-091f-4955-a957-b0fb9b42833b}" DisplayName="PhoneNumber" Name="PhoneNumber" Type="Text" SourceID="{{listid:Customer List Feb 2020}}" StaticName="PhoneNumber" ColName="nvarchar6" RowOrdinal="0" />
            <Field ID="{156ada44-fd5f-4ed2-85af-d9be52a79f73}" DisplayName="Address" Name="Address" Type="Text" SourceID="{{listid:Customer List Feb 2020}}" StaticName="Address" ColName="nvarchar7" RowOrdinal="0" />
            <Field ID="{f3340212-afe4-404e-bbe3-06edf38e4e12}" DisplayName="Country" Name="Country" Type="Text" SourceID="{{listid:Customer List Feb 2020}}" StaticName="Country" ColName="nvarchar8" RowOrdinal="0" />
            <Field ID="{6c612e60-e32d-4d34-9e38-32fadedc9341}" DisplayName="Email" Name="Email" Type="Text" SourceID="{{listid:Customer List Feb 2020}}" StaticName="Email" ColName="nvarchar9" RowOrdinal="0" />
            <Field ID="{d9c0a3c9-b572-3412-92d2-35ebc603bed8}" DisplayName="Notes" Name="Notes" Type="Text" SourceID="{{listid:Customer List Feb 2020}}" StaticName="Notes" ColName="nvarchar10" RowOrdinal="0" />
          </pnp:Fields>
          <pnp:Webhooks>
            <pnp:Webhook ServerNotificationUrl="https://0-centralindia1.pushp.svc.ms/notifications?token=w2-69e198e1-dc61-480e-bd5e-1caeed518fd7" ExpiresInDays="1" />
          </pnp:Webhooks>
        </pnp:ListInstance>
      </pnp:Lists>
    </pnp:ProvisioningTemplate>
  </pnp:Templates>
</pnp:Provisioning>

 

Notes:

Unfortunately, there is no PnP PowerShell command for getting a template from a single list. So, the above command will list out all lists and libraries from the particular site.

  • The above command will be helpful if you want to replicate the same set of lists or libraries on another site, otherwise, we need to do some workaround to get the specific list from the downloaded list template XML.

Below is the workaround, how to get a particular list or library from the consolidated list template XML.

#########The below code is used to filter the specific list template from the consolidated templates############
$listName = "Customer List Feb 2020";
$outputTemplateFileName = "C:\Temp\PnP\SPListPnPProvisioning\ListTemplateDownloaded.xml";
$templates = Get-PnPProvisioningTemplate -OutputInstance -Handlers Lists
$singleListTemplate = $templates.Lists | Where-Object { $_.Title -eq $listName }
$templates.Lists.Clear()
$templates.Lists.Add($singleListTemplate)
Save-PnPProvisioningTemplate -InputInstance $templates -Out $outputTemplateFileName;

Write-host "Successfully PnP list template has been customized for a single list."

Explanation of the above code: Create list in SharePoint online using PnP PowerShell

  • $ListName – where you can pass your desired list title for which you want to download the template.
  • $outputTemplateFileName – You can pass any location path here to save the template XML file.
  • $templates – getting all lists templates from a site.
  • $singleListTemplate – extracting only the list that you have passed in line number one, i.e. $ListName
  • Then it is clearing all lists and only retaining the desired list you want.
  • Finally, saving the list template XML which has the details only about the list that you need, the rest will be cleaned up.

Workaround to filter only a particular list template from the consolidated templates, demo: PnP Provisioning Template

In the below demo, we’ll see how to filter only a particular PnP list template from the consolidated templates.

Create list in SharePoint online using PnP PowerShell
Create-list-using-PnP-Provisioning-Template-Demo

 

Note:

Now, from the filtered list template what we have got from the above demo, we can create any new list in the new site using the PnP provisioning template.

Summary: Create list in SharePoint online using PnP PowerShell

Hence, in this article – we have learned the below:

  • How to create a list in Sharepoint Online using the PnP provisioning template.
  • How to generate a SharePoint list template using PnP provisioning.
  • How to filter a specific SharePoint list template from a consolidated PnP template.

See Also: SharePoint Online PowerShell Tutorial

You may also like the below SharePoint Online PowerShell Tutorial:

About Post Author

1 comments on “Top 2 best ways to create list in SharePoint online using PnP PowerShell provisioning template”

Do you have a better solution or question on this topic? Please leave a comment