SharePoint PnP PowerShell - How To Create List In SharePoint Online Using PowerShell

Introduction

SharePoint PnP PowerShell solution contains a library of PowerShell commands, which allows you to perform complex provisioning and artifact management actions towards SharePoint. The commands use CSOM and can work against both SharePoint Online and SharePoint On-Premises.

Prerequisites

Install SharePointPnPPowerShellOnline.msi for SharePoint Online.

Create list

Open Notepad and paste XML given below. Save the text file as Inputs.xml.

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <Inputs>  
  3.   <ConnectSPOnline SiteURL="https://c986.sharepoint.com/sites/vijai" UserName="[email protected]" Password="pASSWORD123*"></ConnectSPOnline>  
  4.   <Lists>  
  5.     <List Title="Demo Custom List" URL="DemoCustomList" Template="GenericList"/>  
  6.     <List Title="Demo Custom List1" URL="DemoCustomList1" Template="Announcements"/>  
  7.   </Lists>  
  8. </Inputs>  

Open Notepad and paste the script given below. Save the text file as CreateLists.ps1.

  1. ############################################################## Logging ######################################### 
  2.   
  3. $date= Get-Date -format MMddyyyyHHmmss  
  4. start-transcript -path .\Log_$date.doc   
  5.  
  6. ################################################### Get input parameters from XML ###############################
  7.  
  8. # Get content from XML file  
  9. [xml]$xmlData=Get-Content ".\Inputs.xml"  
  10.  
  11. # ConnectSPOnline node  
  12. [System.Xml.XmlElement]$connectSPOnline = $xmlData.Inputs.ConnectSPOnline  
  13. $siteURL=$connectSPOnline.SiteURL  
  14. $userName=$connectSPOnline.UserName  
  15. $password=$connectSPOnline.Password  
  16.  
  17. # Lists node  
  18. [System.Xml.XmlElement]$lists = $xmlData.Inputs.Lists  
  19.  
  20. ########################################################## Get Credentials ######################################
  21.   
  22. function GetCredentials()  
  23. {   
  24.     write-host -ForegroundColor Green "Get Credentials and connect to SP Online site: " $siteURL  
  25.     # Convert password to secure string    
  26.     $secureStringPwd = ConvertTo-SecureString -AsPlainText $Password -Force  
  27.  
  28.     # Get the credentials  
  29.     $credentials = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $userName,$secureStringPwd   
  30.  
  31.     # Connect to SP online site  
  32.     Connect-PnPOnline –Url $siteURL –Credentials $credentials      
  33. }  
  34.  
  35. #######################################################  Create List and Add Fields #############################
  36.   
  37. function CreateLists()  
  38. {  
  39.     write-host -ForegroundColor Green "Creating Lists"   
  40.  
  41.     # Loop through List XML node  
  42.     foreach($list in $lists.List)  
  43.     {  
  44.         # Get List node parameters  
  45.         $listTitle=$list.Title  
  46.         $listURL=$list.URL  
  47.         $listTemplate=$list.Template  
  48.  
  49.         # Get the list object  
  50.         $getList=Get-PnPList -Identity $listURL  
  51.  
  52.         # Check if list exists  
  53.         if($getList)  
  54.         {  
  55.            write-host -ForegroundColor Magenta $listURL " - List already exists"   
  56.         }  
  57.         else  
  58.         {  
  59.            # Create new list  
  60.            write-host -ForegroundColor Magenta "Creating list: " $listURL  
  61.            New-PnPList -Title $listTitle -Url $listURL -Template $listTemplate  
  62.         }          
  63.     }  
  64. }  
  65.  
  66. #################################################################  Initiate #####################################
  67.   
  68. function Initiate()  
  69. {  
  70.      write-host -ForegroundColor Green "Initiating the script.................. "   
  71.  
  72.      # Get Credentials and connect to SP Online site  
  73.      GetCredentials  
  74.  
  75.      # Call the required functions  
  76.      CreateLists  
  77.  
  78.      # Disconnect from the server  
  79.      Disconnect-PnPOnline  
  80.   
  81.      write-host -ForegroundColor Green "Completed!!!!"   
  82. }  
  83.  
  84. #################################################################################################################
  85.   
  86. Initiate  
  87. Stop-Transcript  

Run Windows PowerShell as an administrator.

Navigate to the folder, where XML and ps1 files are available.

Type .\CreateLists.ps1 and click enter.

 

Result

List created successfully in SharePoint Online site. In this blog, you have seen how to create a list in SharePoint Online, using PowerShell.

Reference

https://github.com/SharePoint/PnP-PowerShell/blob/master/Documentation/NewPnPList.md

Ebook Download
View all
Learn
View all