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.
- <?xml version="1.0" encoding="utf-8"?>
- <Inputs>
- <ConnectSPOnline SiteURL="https://c986.sharepoint.com/sites/vijai" UserName="[email protected]" Password="pASSWORD123*"></ConnectSPOnline>
- <Lists>
- <List Title="Demo Custom List" URL="DemoCustomList" Template="GenericList"/>
- <List Title="Demo Custom List1" URL="DemoCustomList1" Template="Announcements"/>
- </Lists>
- </Inputs>
Open Notepad and paste the script given below. Save the text file as CreateLists.ps1.
- ############################################################## Logging #########################################
-
- $date= Get-Date -format MMddyyyyHHmmss
- start-transcript -path .\Log_$date.doc
-
- ################################################### Get input parameters from XML ###############################
-
- # Get content from XML file
- [xml]$xmlData=Get-Content ".\Inputs.xml"
-
- # ConnectSPOnline node
- [System.Xml.XmlElement]$connectSPOnline = $xmlData.Inputs.ConnectSPOnline
- $siteURL=$connectSPOnline.SiteURL
- $userName=$connectSPOnline.UserName
- $password=$connectSPOnline.Password
-
- # Lists node
- [System.Xml.XmlElement]$lists = $xmlData.Inputs.Lists
-
- ########################################################## Get Credentials ######################################
-
- function GetCredentials()
- {
- write-host -ForegroundColor Green "Get Credentials and connect to SP Online site: " $siteURL
- # Convert password to secure string
- $secureStringPwd = ConvertTo-SecureString -AsPlainText $Password -Force
-
- # Get the credentials
- $credentials = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $userName,$secureStringPwd
-
- # Connect to SP online site
- Connect-PnPOnline –Url $siteURL –Credentials $credentials
- }
-
- ####################################################### Create List and Add Fields #############################
-
- function CreateLists()
- {
- write-host -ForegroundColor Green "Creating Lists"
-
- # Loop through List XML node
- foreach($list in $lists.List)
- {
- # Get List node parameters
- $listTitle=$list.Title
- $listURL=$list.URL
- $listTemplate=$list.Template
-
- # Get the list object
- $getList=Get-PnPList -Identity $listURL
-
- # Check if list exists
- if($getList)
- {
- write-host -ForegroundColor Magenta $listURL " - List already exists"
- }
- else
- {
- # Create new list
- write-host -ForegroundColor Magenta "Creating list: " $listURL
- New-PnPList -Title $listTitle -Url $listURL -Template $listTemplate
- }
- }
- }
-
- ################################################################# Initiate #####################################
-
- function Initiate()
- {
- write-host -ForegroundColor Green "Initiating the script.................. "
-
- # Get Credentials and connect to SP Online site
- GetCredentials
-
- # Call the required functions
- CreateLists
-
- # Disconnect from the server
- Disconnect-PnPOnline
-
- write-host -ForegroundColor Green "Completed!!!!"
- }
-
- #################################################################################################################
-
- Initiate
- 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