For a SharePoint Administrator managing the changes in the view for a webpart is a very tiresome job. If a single view changes in the library or list then a page which uses the webpart view has to be manually changed. To change the view in the webpart the administrator has to edit the page, modify webpart and save the page. Imagine if you have 5 pages and each page have 5 webparts and a view is changed. You have to open each page in edit mode and the each webpart edit, change view and save one by one.
To effectively change the webpart view in a page, CSOM have an api to get the webpart in a page as a XML. Manipulate the XML and get the webpart name and view name from xml. Usually when a webpart is added to the page the view xml is cached to the page. So when the view is changed in the library / list the updated view will not be reflected in the page. The PowerShell code will get the webpart and update the view xml
The full code is here you can download
#### To change the webpart view #### Get the webpart from the page. get the WebPart XmlDef and find the View ID ### List has hidden view for the web part added. So get the view from the List and change that view. ### Its all about that hidden View Add-Type -Path ".\CSOM\Microsoft.SharePoint.Client.dll" Add-Type -Path ".\CSOM\Microsoft.SharePoint.Client.Runtime.dll" $now=Get-Date -format "dd-MMM-yy,HH:mm:ss" $fileFormat = Get-Date -format "dd-MMM-yy_HHmmss" Write-Host "Script Start : '$($now)'" -ForegroundColor Yellow $username = "" $password = "" $srcUrl = "" # full url like hhtp://my.sharepoint.com/ $sitePath = "/SitePages/" $srcLibrary = "Documents" $pagesList = @("Home.aspx") # you can specify many pages to be changed array here $wepPartChangeList = @{"Documents" = "All Documents" ; } ### dictionary value Key = Web part Title seen on page, Value = View Name from the Library $securePassword = ConvertTo-SecureString $password -AsPlainText -Force $credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $securePassword) Write-Host "Authenticating ..." -ForegroundColor White $srcContext = New-Object Microsoft.SharePoint.Client.ClientContext($srcUrl) $srcContext.Credentials = $credentials $srcWeb = $srcContext.Web $srcList = $srcWeb.Lists.GetByTitle($srcLibrary) $srcContext.Load($srcWeb) $srcContext.Load($srcList) try{ $srcContext.ExecuteQuery() }catch{ Write-Host $_.Exception.Message -ForegroundColor Red exit } Write-Host "Connected to the web..." -ForegroundColor Green foreach($page in $pagesList) { $filePath = $sitePath + $page $pageObject = $srcWeb.GetFileByServerRelativeUrl($filePath) $srcContext.Load($pageObject) $srcContext.ExecuteQuery() $WPM = $pageObject.GetLimitedWebPartManager("Shared") $webParts = $WPM.WebParts $srcContext.Load($WPM) $srcContext.Load($webParts) $srcContext.ExecuteQuery() foreach($wp in $webParts) { $srcContext.Load($wp.WebPart) $srcContext.Load($wp.WebPart.Properties) $srcContext.ExecuteQuery() if( $wepPartChangeList[$wp.WebPart.Title] -ne $null) #if($wp.WebPart.Title.IndexOf("Documents") -gt 0) { Write-Host "---------- Processing Page = $($pageObject.Name) Webpart = $($wp.WebPart.Title) ------- " -ForegroundColor Yellow $wpXmlContent = $wp.WebPart.Properties["XmlDefinition"] $wpXml = New-Object -TypeName XML $wpXml.LoadXml($wpXmlContent) $viewGUID = $wpXml.View.Name $viewName = $null $viewName = $wepPartChangeList[$wp.WebPart.Title] if($viewName) { $view = $srcList.Views.GetByTitle($viewName) $wpView = $srcList.Views.GetById($viewGUID) $srcContext.Load($wpView) $srcContext.Load($view) $srcContext.ExecuteQuery() #$viewXml = New-Object -TypeName XML #$viewXml.LoadXml($view.ListViewXml) write-host "### WebPart Xml -------Before Xml Change -----" -foreground cyan write-host "$($wpView.ListViewXml)" $wpViewFields = $wpView.ViewFields $viewFields = $view.ViewFields $srcContext.Load($viewFields) $srcContext.Load($wpViewFields) $srcContext.ExecuteQuery() $wpViewFields.RemoveAll() foreach($vField in $viewFields) { $wpView.ViewFields.Add($vField) } $wpView.RowLimit = $view.RowLimit $wpView.ViewQuery = $view.ViewQuery $wpView.Update() $srcContext.Load($wpView) $srcContext.ExecuteQuery() write-host "### WebPart Xml -------After Xml Change -----" -foreground cyan write-host "$($wpView.ListViewXml)" write-host "*******************************************************************" -foreground cyan } else { Write-Host "Unable to fing view for " $wp.WebPart.Title -ForegroundColor Cyan } #$wp.SaveWebPartChanges() } } #Write-Host "Next Page " } #> $srcContext.Dispose() $now=Get-Date -format "dd-MMM-yy,HH:mm:ss" Write-Host "END : '$($now)'" -ForegroundColor Yellow<span id="mce_SELREST_start" style="overflow:hidden;line-height:0;"></span>