Posts tonen met het label Powershell script. Alle posts tonen
Posts tonen met het label Powershell script. Alle posts tonen

woensdag 6 februari 2013

Add webparts to pages via PowerShell

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 Glimlach

Code Snippet
  1. Add-PSSnapin Microsoft.SharePoint.PowerShell
  2. Start-SPAssignment -Global    # This cmdlet takes care of the disposable objects to prevent memory leaks
  3.  
  4. function AddCustomWebPart($web,$page,[string]$webpart_nameSpace,[string]$webpartTitle,[int]$index)
  5. {
  6.     $comment = $webpartTitle + " WebPart Added"
  7.  
  8.     $webpartmanager=$web.GetLimitedWebPartManager($page.Url,  [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)
  9.     
  10.     $wpsOnPage = $webpartmanager.WebParts | % { $_ }
  11.     foreach($wp in $wpsOnPage)
  12.     {
  13.         if($wp -ne $null)
  14.         {
  15.             if($wp.Title -eq $webpartTitle)
  16.             {
  17.                 Write-Host "webpart " $wp.Title " exists on page. "
  18.                 $webpartmanager.DeleteWebPart($webpartmanager.WebParts[$wp.ID]);
  19.             }
  20.         }
  21.     }
  22.     
  23.     $webpart = new-object $webpart_nameSpace
  24.     $webpart.ChromeType = [System.Web.UI.WebControls.WebParts.PartChromeType]::Default;
  25.     $webpart.Title = $webpartTitle
  26.     $webpart.Width = "300px"
  27.     
  28.     $webpartmanager.AddWebPart($webpart, "RightColumn", $index);    
  29. }
  30.  
  31. function CheckInAndPublishPage($comment)
  32. {
  33.     " Checking in page"
  34.     $page.CheckIn($comment)
  35.  
  36.     # Publish
  37.     if($page.listItem.ParentList.EnableMinorVersions -eq $true -and $publishingPage.ListItem.File.MinorVersion -ne 0)
  38.     {
  39.             " Publishing"
  40.             $page.listItem.File.Publish($comment)
  41.     }
  42.  
  43.     # If moderation is being used handle the approval    
  44.     if ($page.listItem.ParentList.EnableModeration)
  45.     {
  46.  
  47.         $modInformation = $page.listItem.ModerationInformation
  48.         
  49.         " Moderation on, Current Status: " + $modInformation.Status
  50.  
  51.         # Check for pending approval
  52.         if($modInformation.Status -ne [Microsoft.SharePoint.SPModerationStatusType]::Approved)
  53.         {
  54.             " Approving"
  55.             $page.ListItem.File.Approve($comment)
  56.         }
  57.     }
  58. }
  59.  
  60. $webUrl = "http://intranet/sites/subweb/"
  61. $web = Get-SPWeb $webUrl
  62.     
  63. $list = $web.Lists["Pagina's"]
  64. Write-Host $list.Title
  65. $pages = $list.Items
  66.  
  67. foreach($item in $pages)
  68. {
  69.     $page =  [Microsoft.SharePoint.Publishing.PublishingPage]::GetPublishingPage($item)
  70.     Write-Host "Page Url: " $item.Url
  71.     Write-Host "Page ContentType: " $item.ContentType.Name
  72.     
  73.     $page.CheckOut()
  74.         
  75.     AddCustomWebPart $web $page "SharePoint.Intranet.WebParts.Bijlagen" "Bijlagen" 3
  76.     AddCustomWebPart $web $page "SharePoint.Intranet.WebParts.ProcesbeschrijvingBijlagen" "Procesbeschrijving bijlagen" 4
  77.     
  78.     CheckInAndPublishPage("Bijlage webparts toegevoegd.")
  79. }
  80.  
  81. # Clean up
  82. $web.Close()
  83.  
  84. Write-Host Done.
  85.  
  86. # Add an empty line
  87. Write-Host ""
  88.  
  89. # Clean up
  90. Write-Host "Cleaning up..."
  91. Stop-SPAssignment -Global
  92. Remove-PsSnapin Microsoft.SharePoint.PowerShell
  93.  
  94. Write-Host "Press any key to continue ..."
  95. $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.

woensdag 24 oktober 2012

RichHtmlField for custom field not rendering HTML

I had multiple RichHtmlFields on my page, and one was displaying correctly while another was not.  When I viewed the fields via the powershell, I noticed a difference in the SchemaXML, the WORKING field had the following additional properties:

     RichText="True"
     RichTextMode="FullHtml"

I found a script to update the problem field, and the problem went away for me (look below for script). The change has to be made at the list level for it to take affect on existing pages/lists, but I believe making the modification to the column at the root web (or correct subweb) would work as well.     
     $web = $site.OpenWeb($site.RootWeb.ID)
     $list = $web.Lists["Pages"]
     [Microsoft.SharePoint.Publishing.Fields.HtmlField]$field =   [Microsoft.SharePoint.Publishing.Fields.HtmlField]$list.Fields.GetFieldByInternalName("PageContentFld")
     $field.RichText = $true
     $field.RichTextMode = [Microsoft.SharePoint.SPRichTextMode]::FullHtml
     $field.Update()
     $list.Update()

You may want to check your field to make sure those properties are set.