Quantcast
Channel: TechNet Blogs
Viewing all articles
Browse latest Browse all 34890

Manipulating webparts using PowerShell

$
0
0

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 = "weaveradministrator"

$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


Viewing all articles
Browse latest Browse all 34890

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>