SharePoint Online: PowerShell to Create a List View

SharePoint Online Views helps to organize columns together and present the data with sort, group and filtering capabilities. Views can be either Private or Public - A public view is available to all users, whereas a private view is available only to the user who created it. When you create a list or library it comes with default views E.g "All Items" with certain columns. You can create any number of views to support different use cases in the list or library.

How to Create View in SharePoint Online?
To create a new view, do the following:
  •  Navigate to the list in which you want to create a new View in SharePoint Online.
  • Under List Tab, Click on the "Create View" button from the Ribbon. In the next page, choose a view type such as "Standard view", "Datasheet View", "Gantt View", etc.
    sharepoint online PowerShell to create list view
  • Provide the Name to your view, Select Fields to display in the view, set Sort and Filter options, and click on "OK" to create a view in SharePoint Online List.
    sharepoint online create view

SharePoint Online: PowerShell to Create a List View
Here is how to create a list view 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"

#Custom function to Create List view in SharePoint Online
Function Create-ListView()
        [Parameter(Mandatory=$true)] [string] $SiteURL,
        [Parameter(Mandatory=$true)] [string] $ListName,
        [Parameter(Mandatory=$true)] [string] $ViewName,
        [Parameter(Mandatory=$true)] [String[]] $ViewFields,
        [Parameter(Mandatory=$true)] [string] $viewQuery,
        [Parameter(Mandatory=$false)] [string] $ItemLimit = "30",
        [Parameter(Mandatory=$false)] [string] $IsDefaultView="$True"

    Try {
        $Cred= Get-Credential
        $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)

        #Setup the context
        $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
        $Ctx.Credentials = $Credentials
        #Get the List
        $List = $Ctx.Web.Lists.GetByTitle($ListName)

        #Check if the View exists in list already
        $NewView = $ViewColl | where { ($_.Title -eq $ViewName) }
        if($NewView -ne $NULL)  
            Write-host "View '$ViewName' already exists in the List!" -f Yellow
            $ViewCreationInfo = New-Object Microsoft.SharePoint.Client.ViewCreationInformation
            $ViewCreationInfo.Title = $ViewName
            $ViewCreationInfo.Query = $ViewQuery
            $ViewCreationInfo.RowLimit = $ItemLimit
            $ViewCreationInfo.ViewFields = $Viewfields 
            $ViewCreationInfo.SetAsDefaultView = $IsDefaultView

            #sharepoint online powershell create view
            $NewView =$List.Views.Add($ViewCreationInfo)
            Write-host "New View Added to the List Successfully!" -ForegroundColor Green  
    Catch {
        write-host -f Red "Error Adding View to List!" $_.Exception.Message

#Set parameter values
[email protected]("Title","IsActive","Priority") # Comma separated - Internal Names
$ViewQuery="<OrderBy><FieldRef Name='Title' /></OrderBy><Where><Eq><FieldRef Name='IsActive' /><Value Type='Boolean'>1</Value></Eq></Where>"

#Call the function to Create View in the list
Create-ListView -SiteURL $SiteURL -ListName $ListName -ViewName $ViewName -ViewFields $ViewFields -ViewQuery $ViewQuery

SharePoint Online PnP PowerShell to Create List View
Here is the PnP PowerShell to create a view in SharePoint Online
#Config Variables
$SiteURL = ""
$ListName= "Projects"
$ViewName= "Active Projects"
$ViewFields = @("ProjectID", "Title","Category","ProjectStatus")
$Query = "<Where><Eq><FieldRef Name = 'ProjectStatus' /><Value Type = 'Choice'>Active</Value></Eq></Where>"

#Get Credentials to connect
$Cred = Get-Credential

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

    #sharepoint online pnp powershell create view
    Add-PnPView -List $ListName -Title $ViewName -ViewType Html -Fields $ViewFields -Query $Query -ErrorAction Stop
    Write-host "View '$ViewName' Created Successfully!" -f Green
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
Additionally, The Add-PnPView cmdlet supports -RowLimit, -SetAsDefault switches.

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.