Create, Update, Copy, Delete SharePoint List Views using PowerShell
Requirement is: There are lot of document libraries in a SharePoint site. Periodically, in all libraries there is a need to:
Create SharePoint List View in PowerShell:
Update Existing SharePoint View with PowerShell:
Copy SharePoint list view in PowerShell:
Delete SharePoint List View in PowerShell:
Add New Field to Existing View:
Added MOSS 2007 compatibility to support SharePoint 2007 environments
Once tested above snippets, placed them inside my PowerShell script, called those functions wherever required.
BTW, This is a PowerShell version of my existing work, where I did similar thing using .Net Object Model (C#). How to Create Update Delete SharePoint Views Programmatically
- Create a new view
- Update an existing view with new filter condition
- Create new new by copying existing view and modify the filter
- Delete an existing view
- Add new field to existing view
Create SharePoint List View in PowerShell:
#Function to create new View Function CreateNewView() { Param( [Parameter(Mandatory=$true)] [string]$SiteURL, [Parameter(Mandatory=$true)] [string]$ListName, [Parameter(Mandatory=$true)] [string]$ViewName ) $site = Get-SPSite $SiteURL $web = $site.openweb() #Get the List $list=$web.lists[$ListName] #View Fields $viewFields = New-Object System.Collections.Specialized.StringCollection #Add fields to the view $viewFields.Add("Global Entity") > $null $viewFields.Add("Document Type")> $null $viewFields.Add("Risk")> $null $viewFields.Add("Year")> $null #Set View options $viewRowLimit="100" $viewPaged=$true $viewDefaultView=$false $viewQuery="<Where><Eq><FieldRef Name='Document_x0020_Type' /><Value Type='Choice'>Customer Reconciliation</Value></Eq></Where>" #Crete new view $newview = $list.Views.Add($ViewName, $viewFields, $viewQuery, $viewRowLimit, $viewPaged, $viewDefaultView) #Send message to output console write-host "New View $($ViewName) Created Successfully!" -foregroundcolor green $web.Dispose() $site.Dispose() }
Update Existing SharePoint View with PowerShell:
#Function to Update Existing View Function UpdateViewFilter() { Param( [Parameter(Mandatory=$true)] [string]$SiteURL, [Parameter(Mandatory=$true)] [string]$ListName, [Parameter(Mandatory=$true)] [string]$ViewName, [Parameter(Mandatory=$true)] [string]$ViewQuery ) $site = Get-SPSite $SiteURL $web = $site.openweb() #Get the List $list=$web.lists[$ListName] #Get the View $view = $list.Views[$ViewName] #Update the view if($view -ne $null) { $view.Query = $ViewQuery $view.Update() $list.update() #Send message to output console write-host "View $($ViewName) Updated Successfully!" -foregroundcolor green } else { write-host "Could Not find: $($ViewName)!" -foregroundcolor red } $web.Dispose() $site.Dispose() }
Copy SharePoint list view in PowerShell:
#Function to Update Existing View Function CopyView() { Param( [Parameter(Mandatory=$true)] [string]$SiteURL, [Parameter(Mandatory=$true)] [string]$ListName, [Parameter(Mandatory=$true)] [string]$SourceView, [Parameter(Mandatory=$true)] [string]$TargetView ) $site = Get-SPSite $SiteURL $web = $site.openweb() #Get the List $list=$web.lists[$ListName] #Get the source View $view = $list.Views[$SourceView] #Copy the source view if($view -ne $null) { #Get View Fields from an existing view $Viewfields = $list.Views[$SourceView].ViewFields.ToStringCollection() #Set View options $viewRowLimit="100" $viewPaged=$true $viewDefaultView=$false $viewQuery="<Where><Eq><FieldRef Name='Document_x0020_Type' /><Value Type='Choice'>Customer Reconciliation</Value></Eq></Where>" #Crete new view $newview = $list.Views.Add($TargetView, $viewFields, $viewQuery, $viewRowLimit, $viewPaged, $viewDefaultView) #Send message to output console write-host "View $($SourceView) Copied Successfully!" -foregroundcolor green } else { write-host "Could Not find: $($SourceView)!" -foregroundcolor red } $web.Dispose() $site.Dispose() }
Delete SharePoint List View in PowerShell:
#Function to Delete View Function DeleteView() { Param( [Parameter(Mandatory=$true)] [string]$SiteURL, [Parameter(Mandatory=$true)] [string]$ListName, [Parameter(Mandatory=$true)] [string]$ViewName ) $site = Get-SPSite $SiteURL $web = $site.openweb() #Get the List $list=$web.lists[$ListName] #Get the View $view = $list.Views[$ViewName] #Delte the view if($view -ne $null) { $list.Views.Delete($view.ID) #Send message to output console write-host "View $($ViewName) Deleted Successfully!" -foregroundcolor green } else { write-host "Could Not find: $($ViewName)!" -foregroundcolor red } $web.Dispose() $site.Dispose() }
Add New Field to Existing View:
#Function to Update Existing View Function UpdateView() { Param( [Parameter(Mandatory=$true)] [string]$SiteURL, [Parameter(Mandatory=$true)] [string]$ListName, [Parameter(Mandatory=$true)] [string]$FieldName) #Get the site object $site = Get-SPSite $SiteURL #Get the Web object $web = $site.openweb() #Get the List $list=$web.lists[$ListName] #Process Each view 0..($list.Views.Count-1) | foreach-object { #Get the view $view = $list.Views[$_] #Skip Hidden views and Explorer view if ( ($view.Title -ne "Explorer View") -and ($view.hidden -eq $false) ) { #Delete if already exist #while($view.ViewFields.ToStringCollection().Contains($FieldName)) #{ #$view.ViewFields.delete($FieldName) #$view.Update() #} #Check if view has the specific field already! if(!$view.ViewFields.ToStringCollection().Contains($FieldName)) { #Add new field to existing view $view.ViewFields.add($FieldName) $view.Update() write-host "Field Added to View: $($view.Title) in the list $($list.title)" } } } #Dispose the objects $web.Dispose() $site.Dispose() }
Added MOSS 2007 compatibility to support SharePoint 2007 environments
# For MOSS 2007 compatibility [void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint") #Region MOSS2007-CmdLets Function global:Get-SPSite() { Param( [Parameter(Mandatory=$true)] [string]$SiteCollURL ) if($SiteCollURL -ne '') { return new-Object Microsoft.SharePoint.SPSite($SiteCollURL) } } Function global:Get-SPWeb() { Param( [Parameter(Mandatory=$true)] [string]$SiteURL ) $site = Get-SPSite($SiteURL) if($site -ne $null) { $web=$site.OpenWeb(); } return $web } #EndRegion
Once tested above snippets, placed them inside my PowerShell script, called those functions wherever required.
$WebURL="https://sharepoint.crescent.com/sites/finance/" #Get the Target Site $Web=Get-SPWeb $WebURL foreach($List in $web.lists) { if ( ($List.BaseType -eq "DocumentLibrary") -and ($List.Hidden -eq $false) ) { write-host "Processing List: $($list.title)" #Call function to create a new view CreateNewView $WebURL $List.title "Global Customers" #Call the Function to Delete view DeleteView $WebURL $List.title "Journal Vouchers" #Call function to Update existing view UpdateViewFilter $WebURL $List.title "Vendor Reconciliation" "<Where><Eq><FieldRef Name='Document_x0020_Type' /><Value Type='Choice'>Vendor Reconciliation</Value></Eq></Where>" #Call function to copy the existing view CopyView $WebURL $List.title "Vendor Reconciliation" "Customer Reconciliation" #Call the function to update view with Internal Name of the new field to be added UpdateView $WebURL $list.title "Management_x0020_System_x0020_Department" } }
BTW, This is a PowerShell version of my existing work, where I did similar thing using .Net Object Model (C#). How to Create Update Delete SharePoint Views Programmatically
hi, Where i can provide URL and other details?
ReplyDeletegreat post
ReplyDelete