I have a number of sites where I need to update the view on a web part on the home page. I would like to accomplish this through PowerShell and CSOM. Unfortunately I can't find the property I need to change in the web part.
This is what I have so far:
$siteurl = "https://tenancy.sharepoint.com/sites/sc/site"
Add-Type -Path (Resolve-Path "$env:CommonProgramFiles\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll")
Add-Type -Path (Resolve-Path "$env:CommonProgramFiles\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll")
Add-Type -Path (Resolve-Path "$env:CommonProgramFiles\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.WorkflowServices.dll")
# Create Context
$creds = Get-PnPStoredCredential -Name SPOAdmin -Type PSCredential
$context = New-Object Microsoft.SharePoint.Client.ClientContext($siteUrl)
$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($creds.UserName, $creds.Password)
$context.Credentials = $credentials
# Get Page
$PageUrl = "$SiteUrl/SitePages/Home.aspx"
$page = $Context.Web.GetFileByUrl($PageUrl)
$Context.Load($page)
$Context.ExecuteQuery()
$webpartmanager = $page.GetLimitedWebPartManager([Microsoft.Sharepoint.Client.WebParts.PersonalizationScope]::Shared)
$Context.Load($webpartmanager)
$webParts = $webpartmanager.WebParts
$Context.Load($webParts)
$Context.ExecuteQuery()
foreach($webpart in $webparts){
$Context.Load($webpart.WebPart.Properties)
$Context.ExecuteQuery()
$propValues = $webpart.WebPart.Properties.FieldValues
#Write-Host "Webpart ID: "$webpart.Id
if($propvalues.Title -eq "Briefcase Roles"){
$webPartID = $webpart.ID
#Write-Host "webpart found"
}
}
$webpart = $webParts | Where-Object{$_.ID -eq $webPartID}
This let's me make changes to the web part I need to update. I know I can update it using something like
$webpart.WebPart.Properties["SelectedView"] = "All Items"
$WebPart.SaveWebPartChanges()
$Context.Load($webpart)
$Context.ExecuteQuery()
but SelectedView is not the correct property name.
Can anyone confirm what the property is for changing the Selected View? I want to force it to All Items.
Thanks
