0

I am migrating sites between two different web apps, both in the same SharePoint 2013 farm. Both sites are pretty heavily customised (master pages, page layouts, content types, etc).

All attempts to migrate pages have failed (assigning different master pages, different page layouts, different content types - they're just too different).

My last resort (before I try and get a band of people to manually copy and paste data from one page to the other, over and over again!), is to hope that I can copy web parts (content editor, summary links, image, etc.) from the first page to another using PowerShell.

I'm hoping that I can do something like:

  1. Identify the content in http://site1/pages/sample1.aspx
  2. Create a new page, using the title and URL from the original page, as in http://site2/pages/sample1.aspx
  3. Grab the web parts from the original page and drop them into the new page.

Is this possible? Can anyone help with the code (or point my in the right direction of a sample code)?

Thank you.

2 Answers 2

0

I would suggest a 3rd party solution. This will ensure you will get all items and are not missing some dependencies you overlooked. This will likely be more hassle free than moving the stuff yourself.

Not suggesting any company out there, but the names should be known.

0

Came to the below. It exports the webparts locally,
deletes them from the page,
uploads them and inserts them into the Wiki Content on the page:

$Web.AllowUnsafeUpdates = $true;

$Page.CheckOut()

#Start moving web parts...

Write-host "========== Starting the web parts on" $Page.url " ======-ForegroundColor Green $errorMsg = "Test Error Message" $exportFolderPath = "D:\SharePoint\webpartsTemp" $wpm = $web.GetLimitedWebPartManager($Page.ListItem.Url, [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared) foreach($wp in $wpm.WebParts) { $wp.ExportMode="All"; $webpartfile = $wp.Title + ".xml" Write-host "=== Exporting " $webpartfile -ForegroundColor Yellow $exportPath = $exportFolderPath + "\"" + $webpartfile $xwTmp = new-object System.Xml.XmlTextWriter($exportPath,$null); $xwTmp.Formatting = 1;#Indent $wpm.ExportWebPart($wp, $xwTmp); $wpm.DeleteWebPart($wpm.WebParts[$wp.ID]) $xwTmp.Flush(); $xwTmp.Close(); Write-host "Done exporting " $webpartfile Write-host "Loading xml..." $wpxml = Get-Content -Path $exportPath # Initialize the XmlReaderSettings Object which is required for the XmlReader Object Write-host "Initialize the XmlReaderSettings Object which is required for the XmlReader Object" $xmlReaderSettings = New-Object System.Xml.XmlReaderSettings # Create the XmlReader Object by using the WebPart Definition file and the ReaderSettings Object Write-host "Create the XmlReader Object by using the WebPart Definition file and the ReaderSettings Object" $xmlReader = [System.Xml.XmlReader]::Create($exportPath,$xmlReaderSettings); #Add Web Part to catalogs folder and Get the WebPart Definition Object based on the webpart definition xml file
Write-host "Add Web Part to catalogs folder and Get the WebPart Definition Object based on the webpart definition xml file " -ForegroundColor Yellow $oWebPartDefinition = $wpm.ImportWebPart($xmlReader,[ref]$errorMsg); ## Create the web part. # Create a guid and storage key Write-host "Create a guid and storage key" $documentsWebPartGuid = [System.Guid]::NewGuid() $documentsWebPartId = "g_" + $documentsWebPartGuid.ToString().Replace("-","_") # Create a new web part Write-host "Create a new web part" $documentsWebPart = $oWebPartDefinition # Give the web part an Id Write-host "Give the web part an Id" $documentsWebPart.ID = $documentsWebPartId # Tell it to use the default view Write-host "Tell it to use the default view" $documentsWebPart.Title = $wp.Title ## Create the Wiki Content Write-host "Create the Wiki Content" $Page.ListItem['Page Content'] = $Page.ListItem['Page Content'] + @" <p>​</p> <div class='ms-rtestate-read ms-rte-wpbox' contenteditable='false' defaultpastemoderesolver='PastePlain'> <div class='ms-rtestate-notify ms-rtestate-read $($documentsWebPartGuid.ToString('D'))' id='div_$($documentsWebPartGuid.ToString('D'))'> </div> <div id='vid_$($documentsWebPartGuid.ToString('D'))' style='display: none;'> </div> </div> <p>&#160;</p> "@ $Page.ListItem.Update() # Add the web parts to the page Write-host "Add the web parts to the page" $wpm.AddWebPart($documentsWebPart,'wpz',0) $Page.ListItem.Update(); # Remove expired xml Write-host "Remove expired xml" Remove-Item $exportPath } #Done with web parts...

May have to add the webparts to an array, and then work through that array (to prevent the script failing as soon as the first web part is removed), but this works.strong text

1
  • 1
    Carefull. This script deletes data. Be sure you understand it correctly before executing. Apr 10, 2018 at 9:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.