Home
%3CLINGO-SUB%20id%3D%22lingo-sub-571835%22%20slang%3D%22en-US%22%3EManipulating%20webparts%20using%20PowerShell%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-571835%22%20slang%3D%22en-US%22%3E%0A%20%26lt%3Bmeta%20http-equiv%3D%22Content-Type%22%20content%3D%22text%2Fhtml%3B%20charset%3DUTF-8%22%20%2F%26gt%3B%3CSTRONG%3EFirst%20published%20on%20TECHNET%20on%20Mar%2022%2C%202018%20%3C%2FSTRONG%3E%20%3CBR%20%2F%3E%20Hello%20All%2C%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20One%20of%20my%20customers%20is%20making%20some%20changes%20to%20there%20Sites%20and%20part%20of%20that%20is%20to%20set%20the%20owner%20in%20a%20Contact%20Details%20webpart%2C%20since%20it%20is%20for%20several%20site%20we%20went%20and%20figured%20out%20the%20PowerShell.%26nbsp%3B%20The%20customer%20changes%20the%20title%20from%20'Contact%20Details'%20to%20'Site%20Owner'.%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20I%20wrote%20this%20without%20Error%20Checking%20as%20this%20was%20provided%20simply%20as%20an%20example%2C%20so%20I%20would%20suggest%20you%20add%20that%20yourself.%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20Add-PSSnapin%20Microsoft.SharePoint.PowerShell%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%24WebAppURL%20%3D%20%22http%3A%2F%2Fsp2013app01%22%20%3CBR%20%2F%3E%20%24SiteUrl%20%3D%20%22http%3A%2F%2Fsp2013app01%2Fsites%2Fgremlins%22%20%3CBR%20%2F%3E%20%24PageName%20%3D%20%22default.aspx%22%20%3CBR%20%2F%3E%20%24WebPartTitle%20%3D%20%22Site%20Owner%22%20%3CBR%20%2F%3E%20%24SiteOwnerLogin%20%3D%20%22weaver%5Cadministrator%22%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%24Page%20%3D%20(Get-SPSite%20%24SiteUrl).RootWeb.GetFile(%24PageName)%20%3CBR%20%2F%3E%20%24WebPartMgr%20%3D%20%24Page.GetLimitedWebPartManager(%5BSystem.Web.UI.WebControls.WebParts.PersonalizationScope%5D%3A%3AShared)%20%3CBR%20%2F%3E%20%24WebPart%20%3D%20%24WebPartMgr.WebParts%20%7C%20where%7B%24_.Title%20-eq%20%24WebPartTitle%7D%20%3CBR%20%2F%3E%20%24webPart.ContactLoginName%20%3D%20%24SiteOwnerLogin%20%3CBR%20%2F%3E%20%24WebPartMgr.SaveChanges(%24webPart)%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20The%20key%20to%20this%20is%20the%20line.%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%24WebPartMgr%20%3D%20%24Page.GetLimitedWebPartManager(%5BSystem.Web.UI.WebControls.WebParts.PersonalizationScope%5D%3A%3AShared)%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20Afterwards%20we%20are%20able%20to%20retrieve%20all%20the%20webparts%20and%20manipulate%20the%20property%20'ContactLoginName'%20which%20is%20not%20obvious%20as%20the%20GUI%20shows%20Contact%20and%20there%20is%20a%20property%20named%20Contact%20as%20well.%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20Finally%20we%20run%20the%20method%20SaveChanges%20to%20complete%20everything.%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20Pax%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-571835%22%20slang%3D%22en-US%22%3EFirst%20published%20on%20TECHNET%20on%20Mar%2022%2C%202018%20Hello%20All%2COne%20of%20my%20customers%20is%20making%20some%20changes%20to%20there%20Sites%20and%20part%20of%20that%20is%20to%20set%20the%20owner%20in%20a%20Contact%20Details%20webpart%2C%20since%20it%20is%20for%20several%20site%20we%20went%20and%20figured%20out%20the%20PowerShell.%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-571835%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EContact%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3Emanipulate%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EPowerShell%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3ESharePoint%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3Eweb%20part%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
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