SharePoint Online: PowerShell to Create a List

Requirement: Create List in SharePoint Online using PowerShell

SharePoint Online PowerShell to Create List:
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. 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/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 = $Context.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 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
Related Post: Create document library in SharePoint Online with PowerShell

Create List in SharePoint Online using PnP PowerShell
#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

    #Create New List
    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.
SharePoint Online: PowerShell to Create a List SharePoint Online: PowerShell to Create a List Reviewed by Salaudeen Rajack on December 04, 2014 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.