SharePoint 2013: Copying Pages between Wiki Page Libraries using PowerShell

Baris Bikmaz has a good Blog Post on Move a SharePoint Team Wiki Library between SharePoint Sites or Site Collections in SharePoint 2010. There is no good method in SharePoint 2013 to copy pages between Wiki Libraries. Using SharePoint Designer, SharePoint Template Library, or using Export-SPWeb PowerShell command does not produce desired results. The Home.aspx page is not a default page.

Using Explorer view and copying files between the libraries keeps the Home.aspx page as a default page but fails to copy the pages’ content.

PowerShell to the rescue. The following script will copy document from one Wiki Library to another in SharePoint 2013. It assumes both source and destination libraries exist in the same SharePoint Farm. You can create destination Wiki library by navigating to Site Settings / add an app and selecting Wiki Page Library

SharePoint2013WikiPageLibrary

####################################################################################################
#
#  Author.......: David Shvartsman
#  Date.........: 07/15/2016
#  Description..: Copying Pages between Wiki Page Libraries, SharePoint 2013
#
####################################################################################################
if ((Get-PSSnapin 'Microsoft.SharePoint.PowerShell' -ErrorAction SilentlyContinue) -eq $null) {
  Add-PSSnapin 'Microsoft.SharePoint.PowerShell'
}

Function Create-WikiPage([string]$WebUrl,[string]$wikiLibararyName, [string]$PageName,[string]$PageContent)
{
    $web = Get-SPWeb $WebUrl
    $wikiPages = $web.Lists[$wikiLibararyName]
    $rootFolder = $wikiPages.RootFolder;
    $files = $rootFolder.Files;
    $newPage = $files.Add($rootFolder.ServerRelativeUrl + '/' + $PageName, [Microsoft.SharePoint.SPTemplateFileType]::WikiPage);
    $newPage.ListItem
    $wikiItem = $newPage.Item
    $wikiItem['ows_WikiField'] = $pageContent
    $wikiItem.Update()
    $web.dispose()
}

CLS
$destWikiWebUrl      = 'http://sharepoint/sites/site1/'
$destwikiListName    = 'Destination Wiki Library Name'
$sourceWikiWebUrl    = 'http://sharepoint/sites/site2/'
$sourcewikiListName  = 'Source Wiki Library Name'  

$destwikiWeb    = Get-SPWeb $destWikiWebUrl
if ($destwikiWeb -eq $null) {
    Write-Host 'The destination Library not found' -ForegroundColor red
    return
}
$destwiki       = $destwikiWeb.Lists[$destwikiListName]
$newLink        = $destwiki.RootFolder.ServerRelativeUrl.REPLACE(' ', '%20')
$sourcewikiWeb  = Get-SPWeb $sourceWikiWebUrl
if ($sourcewikiWeb -eq $null) {
    Write-Host 'The source Library not found' -ForegroundColor red
    return
}
$sourcewiki     = $sourcewikiWeb.Lists[$sourcewikiListName]
$oldLink        = $Sourcewiki.RootFolder.ServerRelativeUrl.REPLACE(' ', '%20')

foreach ($wikiItem in $sourcewiki.Items) {
    $destContent = ''
    try   {
        Write-Host -foregroundcolor green ('Updating content in: $($wikiItem.Name) ... ') -NoNewline
        # Replace the links in the new content
        $sourceContent   = $wikiItem['ows_WikiField']
        $sourceContent = $sourceContent -replace $oldLink, $newLink
        foreach ($wikiItem1 in $destwiki.Items) {
            if ($wikiItem.Name -eq $wikiItem1.Name) {
                $destContent   = $wikiItem1['ows_WikiField']
                $wikiItem1['ows_WikiField'] = $sourceContent
                $wikiItem1.Update()
            }
        }
        if ($destContent -eq '') {
            Write-Host 'Creating new Wiki Page $($wikiItem.Name) ... )'
            Create-WikiPage -WebUrl $destWikiWebUrl -wikiLibararyName $destwikiListName -PageName $wikiItem.Name -PageContent $sourceContent
        }
        Write-Host -foregroundcolor green ('Success!!!')
    }
    catch [System.Exception] {
       Write-Host 'Error while copying Wiki page: ' -foregroundcolor red  -NoNewline
       $Error[0]
    }
 }

If you need to migrate Wiki Page Libraries between the farms you will need to use CSOM

 

Happy Scripting!

5 comments

  1. Jo

    Hi…
    I want to migrate sharepoint site pages between sitecollections with all the versioning and metadata preserved. How to achieve this using powershell?
    Thanks in advance.
    -Jo

    Like

      • Jo

        Hi,
        Thanks for your reply. But the requirement is like the pages library has to be copied between site collections with versions and metadata preserved without using any tools. I can use either CSOM or powershell. When I tried with some powershell commands I was able to get the version history. But when I tried to restore any of the old version it is not working.

        Like

  2. Alec Blair

    OK – so does this allow me to rename the wiki pages as well? If I had a series of template pages for one topic and wanted to copy them with the new topic name as follows .

    Like

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.