public
Last active

Importing WordPress Export files into Sitefinity CMS (VERY unfinished)

  • Download Gist
WordPressImport.ascx
ASP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="WPimport.ascx.cs" Inherits="SitefinityWebApp.Widgets.WPimport" %>
 
<div>
 
<div style="color: red;">
<asp:Literal ID="Error" Visible="false" runat="server" />
</div>
 
<div>
<asp:Label AssociatedControlID="WordPressFile" runat="server">WordPress Export File: </asp:Label>
<asp:FileUpload ID="WordPressFile" runat="server" />
</div>
 
<div>
<asp:Label AssociatedControlID="BlogDestination" runat="server">Import into this blog: </asp:Label>
<asp:DropDownList ID="BlogDestination" runat="server">
<asp:ListItem Value="">No blogs found!</asp:ListItem>
</asp:DropDownList>
</div>
 
<div>
<asp:Label AssociatedControlID="CreateTags" runat="server">Create the tags? </asp:Label>
<asp:DropDownList ID="CreateTags" runat="server">
<asp:ListItem>Yes</asp:ListItem>
<asp:ListItem>No</asp:ListItem>
</asp:DropDownList>
</div>
 
<div>
<asp:Label AssociatedControlID="CreateCategories" runat="server">Create the categories? </asp:Label>
<asp:DropDownList ID="CreateCategories" runat="server">
<asp:ListItem>Yes</asp:ListItem>
<asp:ListItem>No</asp:ListItem>
</asp:DropDownList>
</div>
 
<div>
<asp:Button ID="Submit" Text="Let's do this!" runat="server"
onclick="Submit_Click" />
</div>
 
</div>
 
<asp:PlaceHolder ID="ImportResults" Visible="false" runat="server">
 
<hr />
 
<asp:Repeater ID="ImportMessagesRepeater" runat="server">
<HeaderTemplate>
<ul>
</HeaderTemplate>
<ItemTemplate>
<li><%# Container.DataItem %></li>
</ItemTemplate>
<FooterTemplate>
</ul>
</FooterTemplate>
</asp:Repeater>
 
 
</asp:PlaceHolder>
WordPressImport.ascx.cs
C#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
using System;
using System.Collections.Generic;
using System.Linq;
using Telerik.Sitefinity;
using System.Xml.Linq;
 
namespace SitefinityWebApp.Widgets
{
public partial class WPimport : System.Web.UI.UserControl
{
protected List<string> ImportMessages = new List<string>();
 
protected void Page_Load(object sender, EventArgs e)
{
var blogs = App.WorkWith().Blogs().Get().ToList();
BlogDestination.DataValueField = "Id";
BlogDestination.DataTextField = "Title";
BlogDestination.DataSource = blogs;
BlogDestination.DataBind();
}
 
protected void Submit_Click(object sender, EventArgs e)
{
if (BlogDestination.SelectedValue == "")
{
ShowError("No blog was selected.");
return;
}
 
if (WordPressFile.HasFile == false)
{
ShowError("No WordPress import file was included.");
return;
}
 
XNamespace wp = "http://wordpress.org/export/1.1/";
XNamespace content = "http://purl.org/rss/1.0/modules/content/";
var xml = XDocument.Load(WordPressFile.FileContent);
 
// Tags
var tags = xml.Descendants(wp + "tag")
.Select(t => new
{
Slug = t.Element(wp + "tag_slug").Value
})
.ToList();
 
// Posts
var posts = xml.Descendants("item")
.Where(t => t.Element(wp + "post_type").Value == "post")
.Where(t => t.Element(wp + "status").Value == "publish")
.Select(t => new {
Title = t.Element("title").Value,
Link = t.Element("link").Value,
PubDate = t.Element("pubDate").Value,
Encoded = t.Element(content + "encoded"),
Tags = t.Elements("category").Where(b => (string)b.Attribute("domain") == "post_tag").Select(c => c.Value).ToList()
})
.ToList();
 
// Tons of things to do:
// --------------------------------------
// - Import Categories
// - Import Images
// - Import Comments
// - Wrap <p> tags around the paragraphs in WordPress posts
// - Create Sitefinity tags, categories, images & blog posts using the Sitefinity API
// - Preserve URL's and/or establish 301 redirects
 
foreach (var tag in tags)
{
ShowImportMessage("Tag: " + tag.Slug);
}
 
foreach (var post in posts)
{
ShowImportMessage("Post: " + post.Title);
}
 
ImportMessagesRepeater.DataSource = ImportMessages;
ImportMessagesRepeater.DataBind();
}
 
private void ShowImportMessage(string Message)
{
ImportResults.Visible = true;
ImportMessages.Add(Message);
}
 
private void ShowError(string ErrorMessage)
{
Error.Visible = true;
Error.Text = ErrorMessage;
}
}
}

Any chance you finished coding this? I need to do a WP->SF migration and have only found one tool available.

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.