kwizcom banner advertisement

SharePoint Online: PowerShell to Create a List View

How to Create View in SharePoint Online?
  •  Navigate to the list in which you want to create new View in SharePoint Online.
  • Under List Tab, Click on "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 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()
{ 
    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

            #Add List 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"
$ViewFields=@("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: PowerShell to Create a List View SharePoint Online: PowerShell to Create a List View Reviewed by Salaudeen Rajack on 3:30 PM Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.