SharePoint Online: Create a List using PowerShell

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 an Excel spreadsheet or a database table. E.g., You can create lists to track tasks, contacts, projects, and products. When you add a new SharePoint Online site, it comes with a default set of lists and document libraries (E.g., “Documents” library) based on the site template selected. However, you may need to create additional lists and libraries on 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 gear icon >> Choose “Site contents”.
  2. On the Site contents page, click on the New menu and then choose “List” (You can also use New >> App >> Click on “Classic Experience”>> Pick the list template such as “Custom List”, “Announcements”, “Tasks”, etc.).
    sharepoint online create list
  3. Pick the relevant list template, such as From an Excel file, From an existing list, or start from scratch with “Blank list”. You can also pick from available templates such as “Issue tracker”, “Employee onboarding”, etc.create list from template sharepoint online
  4. Provide a name for the list. E.g., “Projects”.
  5. Leave the “Show in site navigation box” checked if you want this list to be shown in the left navigation menu of the site.
  6. Click on the “Create” button at the bottom of the page to create the list.

This will create a new custom list, “Projects” with the default Title column in it, and You can add more columns of different types of data such as single line of text, date, number, currency, etc., as required. Once the list is created, you can set the list configuration options: required fields, Quick launch, default view, modern list or classic SharePoint list, Attachments, versioning settings, etc., from the settings page of the list. E.g., To enable versioning on the list, Navigate to the List >> Click on the setting gear Icon >> List Settings >> Versioning settings >> Set the “Create a version each time you edit an item in this list?” Radio button to Yes, then click “OK”. Creating a new view to filter data in the list also helps!

Let’s see how to create a list in SharePoint Online using PowerShell.

How to Create a List in SharePoint Online using PowerShell?

PowerShell is a powerful scripting language that can be used to automate many tasks in SharePoint Online. Here, in this case, while it is possible to create lists manually, PowerShell provides a more efficient way to do this in a scripted way for automation. 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()

That’s it! Your list has been created and is now ready for you to add data to it. 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. How many lists can a SharePoint site have? You can create up to 2,000 lists and libraries combined per site collection (Including the Root site and subsites underlying).

You can add SharePoint List to Microsoft Teams tab: How to Add SharePoint List or Document Library to Microsoft Teams?

SharePoint Online: How to Create a List using PowerShell?

SharePoint lists are a great way to organize data and keep track of information. 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
}

How do I create a task list in SharePoint? To create a new list using the client-side object model and PowerShell in SharePoint Online, just change the TemplateType property value to 100 and run the above code. E.g., to create a Task list, use the TemplateType as 107.

$ListInfo.TemplateType = "107" #Task List

Result of the script in classic experience:

create list in sharepoint online using powershell

Create a List in SharePoint Online using PnP PowerShell

PowerShell is your friend if you need to create a list in SharePoint Online but don’t want to use the browser-based interface. Here is how to create a list using PnP PowerShell in SharePoint Online:

#Config Variables
$SiteURL = "https://Crescent.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. E.g.

New-PnPList -Title "Config" -Template GenericList -Hidden

If you want to create a list from a template (.stp), use: SharePoint Online: PowerShell to Create List from Template

How do I add items to a SharePoint Online list in PowerShell?

Use the PnP PowerShell cmdlet Add-PnPListItem or CSOM method “List.AddItem” to create a new item in the SharePoint Online list.
More info: Add items to SharePoint Online lists with PowerShell

How do I create a list template in SharePoint Online?

Go to your SharePoint Online List >> Navigate to List settings >> Click on “Save list as Template” to save a list as a template. You can also use the “List.SaveAsTemplate” method to create a list template in SharePoint Online using PowerShell.
More info: Create a List Template in SharePoint Online

How do I create a list from another list in SharePoint Online?

Login to your SharePoint Online site, click on the “New” button in the command bar, and choose “List”. Click on “From existing list” >> Pick any existing list >> Provide a name for your new list and click on “Create”.
More info: How to Copy a List in SharePoint Online?

What is the difference between a list and a library in SharePoint?

In SharePoint, a list is a collection of data that can be easily organized and managed, while a library is a collection of files that can be stored, shared, and collaborated on. Lists are typically used for managing data, such as tasks or contacts, while libraries are used for managing documents like Word or Excel.

Salaudeen Rajack

Salaudeen Rajack - SharePoint Expert with Two decades of SharePoint Experience. Love to Share my knowledge and experience with the SharePoint community, through real-time articles!

Leave a Reply

Your email address will not be published. Required fields are marked *