SharePoint Power Shell script to add WebPart to Wiki Page

Recently one of my client requested me to remove a Custom webpart(calendar) and add Calendar list to sub sites. Sounds like a juicy task if you we have to remove in one or two site, but there are around 900+ sites.

Another issue is all the web pages are WikiPage and its difficult to get the exact position of the webpart(webpart was placed in the Sidebar at position 3) using the SharePoint API since SharePoint stores the Webpart in the WIKIPAGE using a hidden webpart zone called “wpz”.

After reading through many resources on the internet I found the following blogs/forums that helped me with creating an Powershell.

http://bit.ly/MqfuVc – msdn discussion form

http://bit.ly/MpY2Dj – Bhushan Gawale

http://bit.ly/cg3l3o – Waldek Matykarz… this is excellent stuff

And finally the PowerShell. This script is bit hackish as we are manipulating the WIKI Content and Changing the WebPart Guid to the new webparts Guid. Hope this helps someone.

$siteCollectionUrl = “https://yoursite”
$spsite = get-spweb $siteCollectionUrl

$classSites = @()

foreach($site in $spsite.Webs)
$classSites = $classSites + $site.URL

foreach($classSite in $classSites)
$web = get-spweb $classSite
$wpPage = $web.GetFile(“SitePages/home.aspx”)
if ($wpPage.Exists)
$WpManager = $wpPage.GetLimitedWebPartManager([System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared);
foreach($webpart in $WpManager.WebParts)
#webpart to remove
if($webpart.Title -eq “Yourwebpartname”)
write-host $web.URL ” –> Started”

#get the webpat ID
$schoolCalendarWebPartStorageKey = $webpart.ID.ToString()
$schoolCalendarWebPartGuid = $webpart.ID.Replace(“g_”, “”)
$schoolCalendarWebPartGuid = $schoolCalendarWebPartGuid.Replace(“_”, “-“)

$wpPage = $web.GetFile(“SitePages/home.aspx”)

$subjectCalendarlist = $web.Lists[“yourlist”]

#webpartmanager will be used to remove/add webparts in the wikipage
$WpManager = $wpPage.GetLimitedWebPartManager([System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared);

#delete the school calendar webpart

#generate a new storage key to add the subject calendar webpart
$newGuid = [System.Guid]::NewGuid().ToString()
$storageKey = “g_” + $newGuid.Replace(“-“,”_”)

#create new instance of subject calendar webpart
$newSubjectCalendar = New-Object “Microsoft.SharePoint.WebPartPages.ListViewWebPart”
$newSubjectCalendar.ID = $storageKey
$newSubjectCalendar.ListId = $subjectCalendarlist.ID
$newSubjectCalendar.ViewType = “Calendar”
$newSubjectCalendar.ViewGuid = $subjectCalendarlist.DefaultView.ID.ToString(“B”).ToUpper()
$newSubjectCalendar.Title = “Subject Calendar”

#get a reference to wiki page item
$item = $wpPage.Item

#wiki content is stored in the field “Wiki Content”
$wikicontent = $item[“Wiki Content”]

#replace the Guid values in the HTML content with the new guid
$wikicontent = $wikicontent.Replace($schoolCalendarWebPartGuid, $newGuid)

#update the wiki content with new guid
$item[“Wiki Content”] = $wikicontent


write-host “web site ” $web.URL ” update successfully”