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