How to update WebPart properties with PowerShell

Recently in a project where the platform has already been deployed into production, using sites and sub-sites built from a common web template, it was needed to change one of the properties of a specific WebPart.

There was a lot obs subsites, so for my customer it was impossible to do it by hand, so he asked me to do it by PowerShell. As you can see the code it is very simple:

$site = get-spsite "http://webapp/sc"
  foreach($web in $site.AllWebs)
{
  #Make sure you specify which page needs changes
  $page = $web.GetFile("pages/default.aspx")
  $page.CheckOut()
  $wpm =   $web.GetLimitedWebPartManager($page.Url,[System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)
  #change the property of the WP you need to grab
  $wp = $wpm.WebParts | Where-Object { $_.Title -eq "Title" }
  $wp.Title = "NewTitle"
  $wpm.SaveChanges($wp)
  $page.CheckIn("Update by SA via PowerShell",[Microsoft.SharePoint.SPCheckinType]::MajorCheckIn)
}

After the code has been executed, all the webparts that are equal to the conditions will be updated.

Till next time!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s