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?
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. 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
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 new view in SharePoint modern experience makes is even more 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
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
Here is the PnP PowerShell to create a view in SharePoint Online
#Config Variables
$SiteURL = "https://crescenttech.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.

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.