Manipulating webparts using PowerShell
Published May 15 2019 03:22 PM 549 Views

First published on TECHNET on Mar 22, 2018
Hello All,

One of my customers is making some changes to there Sites and part of that is to set the owner in a Contact Details webpart, since it is for several site we went and figured out the PowerShell.  The customer changes the title from 'Contact Details' to 'Site Owner'.

I wrote this without Error Checking as this was provided simply as an example, so I would suggest you add that yourself.

Add-PSSnapin Microsoft.SharePoint.PowerShell

$WebAppURL = "http://sp2013app01"
$SiteUrl = "http://sp2013app01/sites/gremlins"
$PageName = "default.aspx"
$WebPartTitle = "Site Owner"
$SiteOwnerLogin = "weaver\administrator"

$Page = (Get-SPSite $SiteUrl).RootWeb.GetFile($PageName)
$WebPartMgr = $Page.GetLimitedWebPartManager([System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)
$WebPart = $WebPartMgr.WebParts | where{$_.Title -eq $WebPartTitle}
$webPart.ContactLoginName = $SiteOwnerLogin
$WebPartMgr.SaveChanges($webPart)

The key to this is the line.

$WebPartMgr = $Page.GetLimitedWebPartManager([System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)

Afterwards we are able to retrieve all the webparts and manipulate the property 'ContactLoginName' which is not obvious as the GUI shows Contact and there is a property named Contact as well.

Finally we run the method SaveChanges to complete everything.

Pax

Version history
Last update:
‎Apr 29 2020 09:32 AM
Updated by: