Manipulating webparts using PowerShell

This post has been republished via RSS; it originally appeared at: Premier Field Engineering articles.

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

Leave a Reply

Your email address will not be published. Required fields are marked *

*

This site uses Akismet to reduce spam. Learn how your comment data is processed.