This week I created some webparts. Those webparts must be added to more than 1000 pages in SharePoint. It’s an awful job to do it manually, so I created a script in PowerShell that do the job for me
Code Snippet
- Add-PSSnapin Microsoft.SharePoint.PowerShell
- Start-SPAssignment -Global # This cmdlet takes care of the disposable objects to prevent memory leaks
- function AddCustomWebPart($web,$page,[string]$webpart_nameSpace,[string]$webpartTitle,[int]$index)
- {
- $comment = $webpartTitle + " WebPart Added"
- $webpartmanager=$web.GetLimitedWebPartManager($page.Url, [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)
- $wpsOnPage = $webpartmanager.WebParts | % { $_ }
- foreach($wp in $wpsOnPage)
- {
- if($wp -ne $null)
- {
- if($wp.Title -eq $webpartTitle)
- {
- Write-Host "webpart " $wp.Title " exists on page. "
- $webpartmanager.DeleteWebPart($webpartmanager.WebParts[$wp.ID]);
- }
- }
- }
- $webpart = new-object $webpart_nameSpace
- $webpart.ChromeType = [System.Web.UI.WebControls.WebParts.PartChromeType]::Default;
- $webpart.Title = $webpartTitle
- $webpart.Width = "300px"
- $webpartmanager.AddWebPart($webpart, "RightColumn", $index);
- }
- function CheckInAndPublishPage($comment)
- {
- " Checking in page"
- $page.CheckIn($comment)
- # Publish
- if($page.listItem.ParentList.EnableMinorVersions -eq $true -and $publishingPage.ListItem.File.MinorVersion -ne 0)
- {
- " Publishing"
- $page.listItem.File.Publish($comment)
- }
- # If moderation is being used handle the approval
- if ($page.listItem.ParentList.EnableModeration)
- {
- $modInformation = $page.listItem.ModerationInformation
- " Moderation on, Current Status: " + $modInformation.Status
- # Check for pending approval
- if($modInformation.Status -ne [Microsoft.SharePoint.SPModerationStatusType]::Approved)
- {
- " Approving"
- $page.ListItem.File.Approve($comment)
- }
- }
- }
- $webUrl = "http://intranet/sites/subweb/"
- $web = Get-SPWeb $webUrl
- $list = $web.Lists["Pagina's"]
- Write-Host $list.Title
- $pages = $list.Items
- foreach($item in $pages)
- {
- $page = [Microsoft.SharePoint.Publishing.PublishingPage]::GetPublishingPage($item)
- Write-Host "Page Url: " $item.Url
- Write-Host "Page ContentType: " $item.ContentType.Name
- $page.CheckOut()
- AddCustomWebPart $web $page "SharePoint.Intranet.WebParts.Bijlagen" "Bijlagen" 3
- AddCustomWebPart $web $page "SharePoint.Intranet.WebParts.ProcesbeschrijvingBijlagen" "Procesbeschrijving bijlagen" 4
- CheckInAndPublishPage("Bijlage webparts toegevoegd.")
- }
- # Clean up
- $web.Close()
- Write-Host Done.
- # Add an empty line
- Write-Host ""
- # Clean up
- Write-Host "Cleaning up..."
- Stop-SPAssignment -Global
- Remove-PsSnapin Microsoft.SharePoint.PowerShell
- Write-Host "Press any key to continue ..."
- $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
Isn’t it wonderful? The script also check if the webpart is already on the page or not. If it’s already added than it will be deleted and added again to respect the order of the webparts. For some reason SharePoint doesn’t respect the order.
If someone knows why SharePoint ignores the order of the webparts feel free to let me know.