Sharepoint - Importing WordPress to SharePoint

I know this is old, but I was also frustrated, that nobody had done this before. I decided to write my migration script in powershell. If you ever need something like this again, give it a try: http://memyselfandbenchase.blogspot.de/2015/07/migrate-your-wordpress-blog-to.html

Hope it helps someone else.

Here is the script. Sorry that it is so long.

#requires -version 3  
 <#  
 .SYNOPSIS  
  This script migrates a wordpress blog to a sharepoint blog.  
 .DESCRIPTION  
  This script migrates a wordpress blog to a sharepoint blog.  
  It is based on the xml export from wordpress. It then parses all the  
  posts, comments and categories. It changes all of the links and  
  uploads the files/images in the posts to the sharepoint library.  
  In order for this to work, the wordpress blog is expected to still be  
  reachable through the links in the export. The user names are preserved  
  as long as the users are also available in sharepoint. Otherwise, the  
  Post/comment is carried out as another defined user.  
 .PARAMETER   
  none  
 .INPUTS  
  none  
 .OUTPUTS  
  Saves uploads from wordpress in .\uploads  
 .NOTES  
  Version:    1.0  
  Author:     Benjamin Chase  
  Creation Date: 15 July 2015  
 .EXAMPLE  
  Migrate-Wordpress.ps1  
 #>  
 #----------------------------------------------------------[Declarations]----------------------------------------------------------  
 if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null)   
 {  
   Add-PSSnapin "Microsoft.SharePoint.PowerShell"  
 }  
 $sourceURL = "https://my.wordpress.blog"  
 $destinationURL = "https://my.sharepoint.server"  
 #Name of sharepoint lists  
 $postListName = "Posts"  
 $mediaListName = "Pictures"  
 $categoryListName = "Categories"  
 $commentsListName = "Comments"  
 #local folder to save files from source server  
 $uploadsFromSourceServer = ($PSScriptRoot + "\uploads")  
 $xmlFilePath = Get-ChildItem ($PSScriptRoot + "\*.*") -Include "*.xml" | foreach-object {$_.Fullname}  
 #This user is used for posts and comments if the imported account does not exist in sharepoint  
 #$defaultUser = $Web.EnsureUser((Get-SPFarm).DefaultServiceAccount.Name)  
 $defaultUser = $Web.EnsureUser("guest")  
 #-----------------------------------------------------------[Execution]------------------------------------------------------------  
 $regex = [System.Text.RegularExpressions.Regex]::Escape($sourceURL) + '(.*?)\/uploads\/(.*?)"'  
 $Web = Get-SPWeb $destinationURL  
 $postList = $Web.Lists[$postListName]  
 $mediaList = $web.Lists[$mediaListName]  
 $categoryList = $web.Lists[$categoryListName]  
 $commentsList = $web.Lists[$commentsListName]  
 # load it into an XML object:  
 $xml = New-Object -TypeName XML  
 $xml.Load($xmlFilePath)  
 foreach($wpPost in $Xml.rss.channel.item )  
 {  
 $newPost = $postList.Items.Add()  
 $newPost["Title"] = $wpPost.title  
 #see if user exists in sharepoint, otherwise post as farm admin  
 try{  
      $newPost["Author"] = $Web.EnsureUser($wpPost.creator.innertext)  
 }  
 catch{  
      $newPost["Author"] = $defaultUser  
 }  
 $newPost["Teaser"] = $wpPost.description  
 #Convert CDATA to string and replace newline with HTML Syntax  
 [string]$body = ($wpPost.Encoded | Foreach {"$($_.innertext)"}).replace("`n","<br/>")  
 #if upload folder does not exist, create it  
 if(!(Test-path $uploadsFromSourceServer)){  
      New-Item -Path $uploadsFromSourceServer -ItemType Directory | Out-Null  
 }  
 #Get all uploaded files and upload them to sharepoint library  
 try{  
      $body | select-string -pattern $regex -AllMatches | Foreach {$_.Matches} | ForEach-Object {   
           $fileURL =($_.Value).trim('"')  
           $filename = ($fileURL.Substring($fileURL.LastIndexOf("/") + 1))  
           try{  
                $webClient=new-object system.net.webclient  
                $webClient.downloadfile($fileURL, ($uploadsFromSourceServer +"\$($filename)"))  
                write-host "Downloading: " $filename  
           }  
           catch{  
                write-error "Error while processing: " $fileURL  
           }  
      }  
 }  
 catch{  
      write-error "Error while parsing content."  
 }  
 #Convert Urls to new server url for the pictures  
 $body = $body | % {$_ -replace ([System.Text.RegularExpressions.Regex]::Escape($sourceURL)+"(.*?)\/uploads\/\d{4}\/\d{2}\/"), "$($destinationURL)/Lists/$($mediaListName)/"}   
 $newPost["Body"] = $body  
 $pubDate = Get-Date -Date $wpPost.post_date  
 $newPost["PublishedDate"] = $pubDate  
 [Microsoft.SharePoint.SPFieldLookupValueCollection] $categoryValues = New-Object Microsoft.SharePoint.SPFieldLookupValueCollection  
 #If category exists and is a category and not a post tag then add it to post  
 foreach($wpCategory in $wpPost.category | Where-object{$_.domain -eq "category"}){  
      if($categoryList.items.name -notcontains $wpCategory.innertext){  
           write-host "Adding category: " $wpCategory.innertext -ForegroundColor green  
           $newCategory = $categoryList.Items.Add()  
           $newCategory["Title"] = $wpCategory.innertext  
           $newCategory.Update()  
           #Update list to get new items and select category from list  
           $categoryList.Update()  
      }  
      #get category  
      $category = $categoryList.items | Where-object {$_.Name -eq $wpCategory.innertext}  
      $categoryValues.Add($category.id)  
 }  
 $newPost["PostCategory"] = $categoryValues  
 $newPost.Update()  
 $postList.Update()   
 #Generate lookup value to reference the post  
 $postToComment = $postList.items | Where-object {$_.Title -eq $newPost["Title"]}  
 $postLookupValue = New-Object Microsoft.Sharepoint.SPFieldLookupValue($postToComment["ID"],$postToComment["Title"])  
 #Migrate existing comments  
 if($wpPost.comment){  
      foreach($wpComment in $wpPost.comment){  
           $newComment     = $commentsList.Items.Add()  
           #Use comment as title, but cut it off so it fits in the field, this is the standard behaviour in SP  
           if($wpComment.comment_content.innertext.length -gt 26){  
                $newComment["Title"] = $wpComment.comment_content.innertext.Substring(0,26) + "..."  
           }  
           else{  
                $newComment["Title"] = $wpComment.comment_content.innertext  
           }  
           #see if user exists in sharepoint, otherwise post as farm admin  
           try{  
                $newComment["Author"] = $Web.EnsureUser($wpComment.comment_author_email)  
                $newComment["Editor"] = $Web.EnsureUser($wpComment.comment_author_email)  
           }  
           catch{  
                $newComment["Author"] = $defaultUser  
                $newComment["Editor"] = $defaultUser  
           }  
           $newComment["Body"] = $wpComment.comment_content.innertext  
           $createdDate = Get-Date -Date $wpComment.comment_date  
           $newComment["Created"] = $createdDate  
           $newComment["Modified"] = $createdDate  
           $newComment["PostTitle"] = $postLookupValue  
           $newComment.Update()  
      }  
 }  
 }  
 #Upload Files from posts  
 $fileCollection = $mediaList.RootFolder.Files  
 $files = Get-ChildItem $uploadsFromSourceServer  
 foreach ($file in $files)  
 {  
   $stream = $file.OpenRead()  
   $uploaded = $fileCollection.Add($file.Name, $stream, $TRUE)  
   write-host "Uploaded: " $file.Name -ForegroundColor green  
   if ($stream) {$stream.Dispose()}  
 }  
 if ($web) {$web.Dispose()}  

Here is a tool to migrate your blog you might want to buy http://www.metalogix.com/Products/Migration-Manager-for-SharePoint/Blogs-and-Wikis-Edition.aspx?id=12

Tags:

Migration

Blog