I've made a template site on which I built a webpage with some webparts, but I'd like to be able to copy this page to all existing sites (I've more than 250 sites, so it wouldn't be possible to do that manually)

My first idea was to write a small script to copy webpage from my source site to the targeted sites, but the targeted web page is a blank one...

Any idea if that kind of development would be possible?

I tried the following code:

$web = Get-SPWeb "SharePointsite/Projects/Test"

$pubWeb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($web)

$publishingPage = $pubWeb.GetPublishingPage("SharePointsite/Projects/Test/SitePages/Test.aspx");


$list = $web.Lists["Web Part Gallery"]   

#use the webpart title or the .webpart file name

$wpl = $list.Items | where {$_.Title -eq "Your Webpart Name"}   

$xmlReader = New-Object System.Xml.XmlTextReader($wpl.File.OpenBinaryStream());   

$errorMsg = ""   

$wpManager = $publishingPage.ListItem.File.GetLimitedWebPartManager([System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared);

$webPart = $wpManager.ImportWebPart($xmlReader, [ref]$errorMsg)   

#insert webpart into header zone at index 1


$publishingPage.CheckIn("Added webpart");




But at the 3rd line, I get the following error : Invalid SPListItem. The SPListItem provided is not compatible with a Publishing Page."

Any idea of what I'm doing wrong?

Thank you for you help

  • I think its possible for sure. As per my knowledge you can use PNP provisioning engine and export only the specific page from the Source site and Import to the Destination site. There could be other solution as well pointed out in this thread sharepoint.stackexchange.com/questions/164018/… – vinit kumar Sep 10 at 11:14
  • Thank you, but I've already tried this solution but it's not working in my case – freedumz Sep 10 at 13:57
  • Error in 3rd line because your page is not a publishing page instead its a wiki page from "Site Pages" library. if you are using Publishing Web , you will have another library called "Pages" , may be you can try to create a page in "Pages" library and Execute above code. As i understood from above code you are just trying to add a webpart in your page dynamically. – vinit kumar Sep 10 at 15:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.