Guest User

Untitled

a guest
Jan 24th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.80 KB | None | 0 0
  1. # Add PowerShell Snapin
  2. $snapin = Get-PSSnapin | Where-Object {$_.Name -eq 'Microsoft.SharePoint.Powershell'}
  3. if ($snapin -eq $null)
  4. {
  5. Add-PSSnapin "Microsoft.SharePoint.Powershell"
  6. }
  7.  
  8. # Get the Site URL
  9. $SiteUrl = "https://MySiteCollectionURL/"
  10.  
  11. # Get the Web URL
  12. $WebUrl = "https://MyWebSiteURL"
  13.  
  14. # Get the Page on which WE are going to Add the WebPart
  15. $PageName = "Test.aspx"
  16.  
  17. # The location of the WEbPart Definition File
  18. $localWebpartPath = "C:WebPartsMyWebPart.webpart"
  19.  
  20. # Error Message which is required as a Reference Parameter while Importing the WEbPart
  21. $errorMsg = "Test Error Message"
  22.  
  23. # Initializing the SPSite Object
  24. $Site = Get-SPSite($SiteUrl)
  25.  
  26. # Get an instance for Publishing Site based on SPSite
  27. $PubSite = New-Object Microsoft.SharePoint.Publishing.PublishingSite($Site)
  28.  
  29. # Get the SPWEb Object
  30. $Web = Get-SPWeb $WebUrl
  31.  
  32. # Get the Publishing Web Based on the SPWeb Object
  33. $PubWeb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($Web)
  34.  
  35. # The below commented line is to get all the Pages
  36. #$PubWeb.GetPublishingPages($PageName);
  37.  
  38. # Get only our Page
  39. $PublishingPage = $PubWeb.GetPublishingPage("https://MyWebURL/Pages/Test.aspx");
  40.  
  41. # Make the Web as AllowUnSafeUpdates as true
  42. $Web.AllowUnsafeUpdates = $true
  43.  
  44. # Checkout the Publishing Page
  45. $PublishingPage.CheckOut();
  46.  
  47. # Get the LimitedWEbPartManager
  48. $limitedWebPartManager = $PublishingPage.ListItem.File.GetLimitedWebPartManager([System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared);
  49.  
  50. # Initialize the XmlReaderSettings Object which is required for the XmlReader Object
  51. $xmlReaderSettings = New-Object System.Xml.XmlReaderSettings
  52.  
  53. # Create the XmlReader Object by using the WebPart Definition file and the ReaderSettings Object
  54. $xmlReader = [System.Xml.XmlReader]::Create($localWebpartPath,$xmlReaderSettings);
  55.  
  56. #Add Web Part to catalogs folder and Get the WebPart Definition Object based on the webpart definition xml file
  57. $oWebPartDefinition = $limitedWebPartManager.ImportWebPart($xmlReader,[ref]$errorMsg);
  58.  
  59. # Add the WebPart to the WebPartManager by specifing the Zone and the Index.
  60. $limitedWebPartManager.AddWebPart($oWebPartDefinition,"RightZone",1);
  61.  
  62. # Checkin the Publishing Page
  63. $PublishingPage.CheckIn("Checkin");
  64.  
  65. # Publish the Page
  66. $PublishingPage.ListItem.File.Publish("Publish");
  67.  
  68. # Revert the AllowUnsafeUpdates to False once we are done.
  69. $Web.AllowUnsafeUpdates = $false
  70.  
  71. # I was trying to Approve the Page. But, if the Approve is enabled on the Pages Library level,
  72. # then only we can do that. Otherwise we cannot. But the Syntax is correct as below.
  73.  
  74. # $PageListItem.File.Approve("Page approved automatically by PowerShell script")
  75.  
  76. - See more at: http://www.sharepointpals.com/post/How-to-Add-WebPart-to-the-Publishing-Page-using-PowerShell-in-SharePoint-2013#sthash.xcdNmxZv.dpuf
Add Comment
Please, Sign In to add comment