Monday, October 1, 2012

Add/Update SharePoint Webpart with Powershell

I recently had a request to add a banner with the Site Title - Site Description on a yellow background to everyone of my 500 SharePoint Orders Dashboard sites front pages I had created. For jobs this big I always break out Powershell!

I decided to use a Content Editor web part that pointed to a script located in a shared Document Library at the root of my site collection. By doing this I can add an empty Content Editor Web Part to each of my 500 dashboard pages, programatically setting it's properties and placement on the page. I will call this web part Title Banner Web Part and it needs to be placed into the Header zone and it had to be the 1st one in that zone.

How this is going to work depends on using a web part page, which I am already doing, that has the out of the box masterpage so we can target a specific class named s4-pagedescription and a div with an id of ctl00_PlaceHolderSiteName_onetidProjectPropertyTitle to get the information we want. I found these by going to my page that I needed the banner on and using F12 to open the IE developer tools and then locating the 2 items I needed in the html.







Once I had my banner looking the way I wanted I saved the file as TitleBannerWebPart.txt and uploaded it to my a scripts document library located on the root site of my site collection that has domain users with read permissions. You can test out the script by pasting it into a content editor web part and saving the page. I keep my scripts like this so that if a system wide change is required I can update one or two lines of code and be good to go.

Now that I have my script ready we are going to log into our server and open up the Powershell ISE and get to work using a content editor web part and set it's properties to point to our banner script stored in the root document library called scripts and set the height to 80 and the Chrome Style to None so we do not see the title or border of the web part.

## CONTACT INFO
## ------------------------------------------------
## CREATED   : 2013.03.30
## MODIFIED  :
## AUTHOR    : Jason Lasby 
## EMAIL     : digitalslavery@gmail.com 
##
## DESCRIPTION
## ------------------------------------------------
## Script to loop through a set of Subsites and  
## add a content editor web part to a page stored in 
## the SitePages Document Library named Dashboard.aspx
## 
## EXPLANATION
## ------------------------------------------------
## This example illustrates how to dynamically loop
## through a set of sub sites and add a web part 
## to a page in a set of sites that use the same page layout
## and are configured with the same web parts, think about 
## a company dashboard site where each department may 
## report standard data like who is in who is out, 
## work related accidents, finance data, shipping
## information, etc... We can loop through our sites
## and add/update or remove web parts for all sites at once.
##
## BEGIN
## ------------------------------------------------
## Load up the snapin so Powershell can work with SharePoint
## if the snapin was already loaded don't alert the user
Add-PSSnapin microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
## Clear any errors or output to the screen
CLS
## Set variables
## This variable will be the site where I want to start
$parentSite = "http://sp2010/dashboards"

$webPartProperty_Title = "Title Banner Web Part"
$webPartProperty_ZoneName = "Header"
$webPartProperty_Height = "80"
$webPartProperty_Width = ""
$webPartProperty_Visible = $true
## All content editor web parts will point to this script
$webPartProperty_Link = "http://sp2010/dashboards/scripts/spservicesTest.txt"

Start-SPAssignment -Global
$site = Get-SPWeb $parentSite
$webSites = $site.Webs
foreach($webSite in $webSites)
{
    $order = $webSite.Name
    $page = $webSite.GetFile("/dashboards/$order/SitePages/Dashboard.aspx") 
    $webPartManager = $webSite.GetLimitedWebPartManager($page, [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)
 foreach($webPart in $webPartManager.WebParts)
 {
  $listOfAllWebParts = $webPart.Title
 }
 ## Check the page to see if the web part was already on it
 if($listOfAllWebParts -eq $webPartProperty_Title)
 {
  Write-Host "$webSite already had web part on it."
 }
 ## Create the web part and set some of it's properties
 else
 {
  $cewp = New-Object Microsoft.SharePoint.WebPartPages.ContentEditorWebPart
  $cewp.ContentLink = $webPartProperty_Link
     $cewp.Title = $webPartProperty_Title
     $cewp.Height = $webPartProperty_Height
     $cewp.Width = $webPartProperty_Width
  $cewp.Visible = $webPartProperty_Visible
  $cewp.ChromeType = "None"
     $cewp.HorizontalAlign = "Center" 
     ## The AddWebPart takes 3 arguments, first is the web part name, 
     ## then the zone id 
     ## and finally the order number where you want the web part to show in the zone
     $webPartManager.AddWebPart($cewp, $webPartProperty_ZoneName, 0)
     $webPartManager.SaveChanges($cewp)
     $webSite.Dispose()
  Write-Host "Webpart was created for Order $order"
 }
}

Stop-SPAssignment -Global



No comments: