SP2010: copying wiki pages and updating relative links in Powershell
On our intra- /extranet, we have a seperate section in which we allow our customers to log in. Each customer gets a private portal site to collaborate with us. Of course, we wanted to add a help section to help users with the usage of this site. For our internal users, we have a wiki site setup on which several functions of the intranet are explained, as well as the customer section. For our customers, I only want to publish the customer related helpfiles without giving them access to the entire help library.
To solve this issue, I created a seperate help section for customer. In this section, I only want the customer subset of the help wiki to show. To do this; I placed the wiki pages for the customer section in a seperate wiki document library. This document library and the matching pictures library are exported to disk, and imported into the help section for customers. This way, we only have to maintain one set of wiki pages, and the other set is being updated automatically once a day. But doing this, you’ll run into a problem: relative links and images will break, because they will point to the internal location, to which the customer doesn’t have access. Ok so now what? Powershell to the rescue!
The following script does the following things:
- Export the wiki page library to disk
- Import the wiki page library to the other site
- Export the image library to disk
- Import the image library to the other site
- Loop all items in the wiki page library, and use string replace to update the relative links.
$ver = $host | select version if ($ver.Version.Major -gt 1) {$host.Runspace.ThreadOptions = "ReuseThread"} if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null) { Add-PSSnapin "Microsoft.SharePoint.PowerShell" } Export-SPWeb -Identity http://site/wiki -ItemUrl /wiki/HelpLibrary -Path "d:\backup\Help.bak" -Force Import-SPWeb -Identity http://site/common -Path "D:\backup\Help.bak" -Force Export-SPWeb -Identity http://site/wiki -ItemUrl /wiki/PublishingImages -Path "d:\backup\PublishingImages.bak" -Force Import-SPWeb -Identity http://site/common-Path "D:\backup\PublishingImages.bak" -Force $list = Get-SPList http://site/common/HelpLibrary foreach ($item in $list.Items) { if (!($item["ows_WikiField"] -eq $null)) { $item["ows_WikiField"] = $item["ows_WikiField"].Replace("/wiki", "/common"); $item.Update(); } }
Of course the replace in the above sample is pretty basic. But you could easily replace that with a if/else or regex if you want. In my case, this was enough.
Leave a Comment