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 specific 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?

List views enable you to sort, filter, group, and format the data in your SharePoint list, so you can easily access it. 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. On 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
Do not edit the default view unless its really required! Instead You can create a new per your requirements!

SharePoint Online: How to Create a New View in Modern Sites?

Creating a new view in SharePoint modern experience makes it even simpler!

  • Apply the filter to the view, add-remove columns to it, Sort-group by as you wish.
    add new view in sharepoint online
  • Once your view is ready, save the current changes as a “View” by Clicking on the Views Drop-down and then on “Save View as”.
    how to create a view in sharepoint online

SharePoint Online: PowerShell to Create a List View

Are you tired of creating views in SharePoint Online manually? Need to create a view in SharePoint Online, but didn’t want to do it through the web interface? Or maybe you need to create a view based on specific criteria that’s not easily accessible through the UI? If so, PowerShell is your friend. I’ll show you how to create views in SharePoint Online using PowerShell. I’ll also show you how to add filters and columns to your views!

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()
{ 
    param
    (
        [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)
        $Ctx.Load($List)
        $Ctx.ExecuteQuery()

        #Check if the View exists in list already
        $ViewColl=$List.Views
        $Ctx.Load($ViewColl)
        $Ctx.ExecuteQuery()
        $NewView = $ViewColl | where { ($_.Title -eq $ViewName) }
        if($NewView -ne $NULL)  
        {
            Write-host "View '$ViewName' already exists in the List!" -f Yellow
        }
        else
        {
            $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)
            $Ctx.ExecuteQuery()    
            
            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
$SiteURL="https://crescent.sharepoint.com"
$ListName="Projects"
$ViewName="ActiveProjects"
[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

PnP PowerShell offers the ability to create views in SharePoint Online easily. This can be helpful when you need to quickly and easily get a specific view of your data. Here is the PnP PowerShell to create a view in SharePoint Online:

#Config Variables
$SiteURL = "https://Crescent.sharepoint.com"
$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. You can also use the Query parameter to set sorting or group by. E.g.,

  • Sort Order: “<OrderBy> <FieldRef Name= ‘Modified’ Ascending=’false’ /> </OrderBy>”
  • Group By: “<GroupBy><FieldRef Name = ‘Status’ /></GroupBy>”

To update an existing list view using PowerShell, refer: PowerShell to Update List View in SharePoint Online

Salaudeen Rajack

Salaudeen Rajack is a SharePoint Architect with Two decades of SharePoint Experience. He loves sharing his knowledge and experiences with the SharePoint community, through his real-world articles!

2 thoughts on “SharePoint Online: PowerShell to Create a List View

  • Hi, thank you so much for this. Can we assign particular views to users? For example I want users A and B to see columns 1-10 and users C D E to be able to see columns 3-7. Is that possible?

    Reply
    • Column permissions can’t be restricted OOTB. You can use either 3rd party products or use CSOM scripts to achieve this.

      Reply

Leave a Reply