SharePoint Online: PowerShell to Create a List

Requirement: Create a List in SharePoint Online using PowerShell.

How to Create a List in SharePoint Online?
SharePoint Online Lists are used to store structured data like you store data in Excel spreadsheet or a database table. E.g. You can create lists to track tasks, contacts, projects, products. When you create a new SharePoint Online site, it comes with a default sets of lists and libraries (E.g. "Documents" library) based on the site template selected. However, you may need to create additional lists and libraries in your site per requirements. So, how to create a new list in SharePoint Online? To create a new list in SharePoint Online, follow these steps:
  1. Browse to the SharePoint Site >> Click on Settings >> Choose "Site contents".
  2. In the Site contents page, Click on the New menu and then choose "List"
    sharepoint online create list

  3. Provide a name for the list. E.g. "Projects"
  4. Leave the "Show in site navigation box" checked if you want this list to be shown in the left navigation menu of the site.
  5. Click on the "Create" button in the bottom of the page to create the list.

This will create a new custom list "Projects" with default "Title" column in it and You can add more columns of different types of data such as text, date, number, currency, etc. Let's see how to create a list in SharePoint Online using PowerShell.


How to Create a List in SharePoint Online using PowerShell?
Here is how to create a list in SharePoint Online using PowerShell:
#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
 
#Variables for Processing
$SiteURL = "https://crescent.sharepoint.com/Sites/Projects"
$ListName="Projects"

#Get Credentials to connect
$Cred = Get-Credential

#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)
    
#Create a custom list in sharepoint online using powershell
$ListCreationInfo = New-Object Microsoft.SharePoint.Client.ListCreationInformation
$ListCreationInfo.Title = $ListName
$ListCreationInfo.TemplateType = 100
$List = $Ctx.Web.Lists.Add($ListCreationInfo)
$List.Description = "Projects List"
$List.Update()
$Ctx.ExecuteQuery()
To create a list or library in SharePoint Online, you need at least "Edit" permissions on the site where you'd like to create the list.

SharePoint Online: How to Create List using PowerShell?
Let's add some error handling to the above script and create a list using PowerShell in SharePoint Online: 
#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
 
#Variables for Processing
$SiteURL = "https://Crescent.sharepoint.com/Sites/Sales"
$ListName="Parent Project"

#Setup Credentials to connect
$Cred = Get-Credential
$Cred = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)

Try {
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = $Cred
    
    #Get All Lists 
    $Lists = $Ctx.Web.Lists
    $Ctx.Load($Lists)
    $Ctx.ExecuteQuery()

    #Check if List doesn't exists already
    if(!($Lists.Title -contains $ListName))
    { 
        #sharepoint online powershell create list
        $ListInfo = New-Object Microsoft.SharePoint.Client.ListCreationInformation
        $ListInfo.Title = $ListName
        $ListInfo.TemplateType = 100 #Custom List
        $List = $Ctx.Web.Lists.Add($ListInfo)
        $List.Description = "Repository to store project artifacts"
        $List.Update()
        $Ctx.ExecuteQuery()
 
        write-host  -f Green "New List has been created!" 
    }
    else
    {
        Write-Host -f Yellow "List '$ListName' already exists!" 
    }
}
Catch {
    write-host -f Red "Error Creating List!" $_.Exception.Message
}

PowerShell to Create List in SharePoint Online:
To create a new list using client-side object model and PowerShell in SharePoint Online, just change the TemplateType property value to 100 and run the above code. E.g.
$ListInfo.TemplateType = "107" #Task List

Result:
create list in sharepoint online using powershell
If you want to create a list from template, use: SharePoint Online: PowerShell to Create List from Template

Create List in SharePoint Online using PnP PowerShell
Here is how to create a list using PnP PowerShell in SharePoint Online:
#Config Variables
$SiteURL = "https://crescenttech.sharepoint.com"
$ListName = "Team Projects"
$ListURL="Lists/TeamProjects"
$Template ="GenericList"

#Get Credentials to connect
$Cred = Get-Credential

Try {
    #Connect to PnP Online
    Connect-PnPOnline -Url $SiteURL -Credentials $Cred

    #sharepoint online create list pnp powershell
    New-PnPList -Title $ListName -Url $ListURL -Template $Template -ErrorAction Stop
    Write-host "List '$ListName' Created Successfully!" -f Green
}
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}
Additionally, the New-PnPList cmdlet supports these parameters: Hidden, EnableVersioning, OnQuickLaunch, EnableContentTypes, etc.

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.