3

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.

enter image description here

Thanks

3
+50

As Vardhman mentioned, a local instance of the view is created. Now, that View has a particular GUID which is equal to the webpart definition's ID.

Using this GUID, you can get the local instance of the listview and modify its ViewQuery,Viewfields and other properties.

Here, I have also loaded the list and list views, so change them as per your requirement.

Try and modify the below code. Works at my end :

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")


#Site collection URL
$siteurl = "url of site collection"


$userName ="userName" 
$password ="password" 

#client context object and setting the credentials   
[Microsoft.SharePoint.Client.ClientContext]$clientContext = New-Object Microsoft.SharePoint.Client.ClientContext($siteurl)   

# convert password into secure string  
$securedpassword = ConvertTo-SecureString $password -AsPlainText -Force   

$clientContext.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($userName, $securedpassword) 

$PageUrl = "$SiteUrl/SitePages/Home.aspx"
$page = $clientContext.Web.GetFileByUrl($PageUrl)
$clientContext.Load($page)
$clientContext.ExecuteQuery()

#load the list and all the list views
$list = $clientContext.Web.Lists.GetByTitle("Briefcase Roles");
$clientContext.Load($list);
$clientContext.Load($list.Views);
$clientContext.ExecuteQuery();

$webpartmanager = $page.GetLimitedWebPartManager([Microsoft.Sharepoint.Client.WebParts.PersonalizationScope]::Shared)

$clientContext.Load($webpartmanager)

$webParts = $webpartmanager.WebParts
$clientContext.Load($webParts)
$clientContext.ExecuteQuery()

foreach($webpart in $webparts){
    $clientContext.Load($webpart.WebPart.Properties)
    $clientContext.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"

        #view guid is the same as webpart definition id
        $listView = $list.Views.GetById($webpart.ID);

        $clientContext.Load($listView);
        $clientContext.ExecuteQuery();

        #remove all the viewfields
        $listView.ViewFields.RemoveAll();

        #internal name of the columns
        $array = @("Name", "ProjectRole");

        for ($i=0; $i -lt $array.length; $i++) {
            $listView.ViewFields.Add($array[$i]);
        }

        # changing the CAML query and setting other properties (strictly optional)      
        # $listView.ViewQuery = "<OrderBy><FieldRef Name=\'Modified\' Ascending='FALSE'/></OrderBy>";
        # $listView.Paged = $true;
        # $listView.RowLimit = 100

        $listView.Update();
        $clientContext.ExecuteQuery();

    }
}
  • Thank you so much! You just saved me a massive amount of work! The bounty is all yours! – Gizmo3k Jul 31 '17 at 7:31
  • Welcome ! Always ready to help ! Could you please accept it as answer, so it wont show up for us in the unanswered section ? Thanks ! – Gautam Sheth Jul 31 '17 at 17:24
  • Done - sorry, thought I had done that already! – Gizmo3k Aug 1 '17 at 8:21
1

Once a view is selected, a local instance of the view is created in the webpart. This means that to set your view, you will have update the View XML directly in the webpart properties:

$webpart.WebPart.Properties["XmlDefinition"] = "<View>...</View>"; //XML of the All Items view
$WebPart.SaveWebPartChanges()
$Context.Load($webpart)
$Context.ExecuteQuery()
  • Thanks, I tried doing this yesterday, but the changes don't seem to save - I will try it again today. – Gizmo3k Jul 25 '17 at 7:44
  • Hi, I have test this again, the changes do not get saved. I used the following: $defn = $webpart.WebPart.Properties.FieldValues.XmlDefinition $defn = $defn.Replace('<ViewFields><FieldRef Name="Name"/></ViewFields>','<ViewFields><FieldRef Name="Name"/><FieldRef Name="ProjectRole"/></ViewFields>') $webpart.WebPart.Properties["XmlDefinition"] = $defn $WebPart.SaveWebPartChanges() $Context.Load($webpart) $Context.ExecuteQuery() However after running this, the new field is not visible. Getting the XMLDdefinition again returns the original, unchanged XMLDefinition – Gizmo3k Jul 26 '17 at 14:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.