I sat down tonight and started to figure out a way to take the post export feature of WordPress (version 3.5 at the time of this posting) and use it to programmatically create posts in a SharePoint 2013 blog site. The script does work however it doesn’t migrate everything (yet?) and has one fairly major issue.

The WordPress export feature exports all the post information to XML.

image

This is very handy since powershell does awesome with XML. SmileYou can take that XML file and read it in like so:

[xml]$posts = Get-Content posts.xml

Then get the Post list from the destination SharePoint 2013 blog site.

$URL = “http://sp2013.tup.net”

$title = “Posts”

$list = (Get-SPWeb -identity $URL).Lists[$Title]

Then each post in the XML is looped through and a new item is created from the values.

foreach($post in $posts.rss.channel.item)

{

$newItem = $list.Items.Add()

$newItem[“Title”] = $post.title

$newItem[“Body”] = [String]$post.encoded.InnerText

$pubdate = [datetime]::ParseExact($post.post_date,”yyyy-MM-dd HH:mm:ss”,$null)

$newItem[“Published”] = $pubdate

#not working

#$newItem[“Category”] =

#$newItem[“Comments”] =

$newItem.Update()

}

There were a couple things that threw me a bit, but I found some posts that helped (listed below). The first issue is that getting at the CDATA line in the XML is not obvious. BTW the way I found to deal with the CDATA content only seems to work in powershell 3. If you want to use this for SharePoint 2010 you’ll have to find another way to do it. Do no install powershell 3 and .net 4 on a SharePoint 2010 box. Trust me on this. I broke my 2010 dev box this way. Smile

The second gotcha is converting a string into a date object.

The full script is below. Right now it does not handle categories or comments. I may try and get those working at some point. It also doesn’t convert the post content very well (picture below showing what I’m talking about). I originally started this because I was thinking about moving my blog to a SharePoint 2013 foundation install on my home hardware. Unfortunately as of right now, 2013 is running way to slow on my home hardware. So I guess I’ll be staying on WordPress for now. (Maybe I’ll try to make the script work with SharePoint 2010 blog sites.)

[xml]$posts = Get-Content posts.xml

$URL = “http://sp2013.tup.net”

$title = “Posts”

$list = (Get-SPWeb -identity $URL).Lists[$Title]

foreach($post in $posts.rss.channel.item)

{

$newItem = $list.Items.Add()

$newItem[“Title”] = $post.title

$newItem[“Body”] = [String]$post.encoded.InnerText

$pubdate = [datetime]::ParseExact($post.post_date,”yyyy-MM-dd HH:mm:ss”,$null)

$newItem[“Published”] = $pubdate

#not working

#$newItem[“Category”] =

#$newItem[“Comments”] =

$newItem.Update()

}

Here is the result of the script run. Note the published dates.

image

image

And the major fail. Some of the post content is garbled.

image

Even though there are some things to address, I thought I’d post the script anyway since it’s a pretty good example of how to use an XML file to create content in SharePoint. Also the garbled content issue may be a result of past plugins or the fact that I was originally on blogger. So you’re mileage may vary.

Date conversion (string to date object)

http://techibee.com/powershell/powershell-converting-string-to-datetime-format/952

gets content from powershell (the CDATA issue)

http://stackoverflow.com/questions/8305622/powershell-parsing-of-cdata-section

Use powershell to manipulate sharepoint lists

http://blogs.technet.com/b/heyscriptingguy/archive/2010/09/22/use-powershell-to-manage-lists-views-and-items-in-sharepoint.aspx

Update: Another thing I forgot to mention that this doesn’t do in relation to wordpress…It doesn’t migrate uploaded photos that are embedded in posts. 🙁