Portal Engine Questions on portal engine and web parts.
Version 7.x > Portal Engine > Forum Posts View modes: 
User avatar
Kentico Legend
Kentico Legend
Brenden Kehren - 1/22/2013 7:46:43 PM
   
Forum Posts
I've got close to 5 years of forum posts exported in a .csv file and I would like to import them into Kentico. I can use the Import Wizard to import the actual Forums or Groups but can't to import the actual posts and responses. Anyone done this before? I'd be happy with setting some of the dynamic values (i.e.: user who made post/response) to static values for the import. If not, I guess it looks like something I could build!

User avatar
Certified Developer 13
Certified Developer 13
josefd@kentico.com - 1/23/2013 4:01:08 AM
   
RE:Forum Posts
Hello,

Unfortunately, there is no tool for this in Kentico CMS. You would have to create a custom script to re-create the posts from .csv export. You might find this thread useful How to import forums.

Regards,
Josef Dvorak

User avatar
Kentico Legend
Kentico Legend
Accepted solutionAccepted solution
Brenden Kehren - 1/24/2013 1:42:03 AM
   
RE:Forum Posts
Thank you Josef! I was able to create a recursive method that gathers all the posts (and their children), creates them and attaches any files associated with the posts. Here is my code if anyone else was interested:
SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["OldConnectionString"].ConnectionString);
ForumInfo forum = ForumInfoProvider.GetForumInfo("YourForumCodeName", CMSContext.CurrentSiteID);
// get a list of all the sites users for later use
DataSet dsUsers = UserInfoProvider.GetUsers("", "");

protected void Page_Load(object sender, EventArgs e)
{
lblMessage.Text = "";
}

protected void btnInsertPosts_Click(object sender, EventArgs e)
{
CreateParentPosts();
}

private void CreateParentPosts()
{
try
{
// get the old parent posts
SqlCommand cmd = new SqlCommand();
cmd.Connection = cn;
cmd.CommandType = CommandType.Text;
cmd.CommandText = "select top 20 cs_posts.*, m.Email from cs_Posts left outer join cs_users on cs_posts.UserID = cs_users.UserID left outer join aspnet_Membership m on cs_users.MembershipID = m.UserId where SectionID = 3 and parentid = -1 and settingsid = 1000 order by threadid, SortOrder, PostLevel";

SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);

if (!DataHelper.DataSourceIsEmpty(ds))
{
if (forum != null)
{
// loop all parent posts
foreach (DataRow dr in ds.Tables[0].Rows)
{
// get the old post id
int oldPostId = ValidationHelper.GetInteger(dr["PostID"], -1);

// Create new forum post object
ForumPostInfo newPost = new ForumPostInfo();
// set values and actually insert the post
CreatePost(dr, ref newPost, -1);
// attach images/files to post
AttachImages(oldPostId, newPost.PostId);
// create the child posts
CreateChildPosts(oldPostId, newPost.PostId);
}
}
}
}
catch (Exception ex)
{
lblMessage.Text += ex.ToString();
}
}

private void CreateChildPosts(int OldPostId, int NewParentId)
{
try
{
// get the old child records
DataSet ReturnValue = new DataSet();
SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["OldConnectionString"].ConnectionString);
SqlCommand cmd = new SqlCommand();
cmd.Connection = cn;
cmd.CommandType = CommandType.Text;
cmd.CommandText = "select m.Email, cs_posts.* from cs_Posts left outer join cs_users on cs_posts.UserID = cs_users.UserID left outer join aspnet_Membership m on cs_users.MembershipID = m.UserId where SectionID = 3 and ParentID = " + OldPostId.ToString() + " and postlevel > 1 order by threadid, SortOrder, PostLevel";

SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);

if (!DataHelper.DataSourceIsEmpty(ds))
{
if (forum != null)
{
// loop all old child records
foreach (DataRow dr in ds.Tables[0].Rows)
{
int oldPostId = ValidationHelper.GetInteger(dr["PostID"], -1);

// Create new forum post object
ForumPostInfo newPost = new ForumPostInfo();
// set values and actually insert the post
CreatePost(dr, ref newPost, NewParentId);
// attach images to post
AttachImages(oldPostId, newPost.PostId);
// recursive action for child posts
CreateChildPosts(oldPostId, newPost.PostId);
}
}
}
}
catch (Exception ex)
{
lblMessage.Text += ex.ToString();
}
}

private void CreatePost(DataRow dr, ref ForumPostInfo newPost, int NewParentId)
{
UserInfo ui = null;
int kenticoUserId = 53;
// get the old email address and attempt to match via email with a current user
// doing this because usernames changed from one CMS to the other although email addresses remained similar
foreach (DataRow udr in dsUsers.Tables[0].Rows)
{
if (ValidationHelper.GetString(udr["Email"], "") == ValidationHelper.GetString(dr["Email"], "not valid"))
{
kenticoUserId = ValidationHelper.GetInteger(udr["UserID"], 53);
break;
}
}

ui = UserInfoProvider.GetUserInfo(kenticoUserId);

// set all the values of the post
newPost.PostUserID = ui.UserID;
newPost.PostUserMail = ui.Email;
newPost.PostUserName = ui.UserName;
newPost.PostForumID = forum.ForumID;
newPost.PostTime = ValidationHelper.GetDateTime(dr["PostDate"], DateTime.Now);
newPost.PostApproved = true;
newPost.PostText = HTMLHelper.StripTags(ValidationHelper.GetString(dr["Body"], ""), false, "");
newPost.PostSubject = HTMLHelper.StripTags(ValidationHelper.GetString(dr["Subject"], ""), false, "");
newPost.PostLevel = ValidationHelper.GetInteger(dr["PostLevel"], 1);
newPost.PostViews = ValidationHelper.GetInteger(dr["TotalViews"], 0);

// do some checking to see if this is a parent record or not
if (NewParentId > -1)
{
newPost.PostParentID = NewParentId;
}
// Save the forum post
ForumPostInfoProvider.SetForumPostInfo(newPost);
}

private void AttachImages(int OldPostId, int NewPostId)
{
// get the physical path on the server D:\Apps\Site\3\PostID
string path = HostingEnvironment.ApplicationPhysicalPath + @"3\" + OldPostId.ToString() + @"\";
if (CMS.IO.Directory.Exists(path))
{
// get all the files in the directory
// might need to filter by *.jpg but previous app already did that so no worries
string[] files = CMS.IO.Directory.GetFiles(path);
// for every file in the directory, create a new forum attachment
foreach (string s in files)
{
ForumAttachmentInfo attachInfo = new ForumAttachmentInfo(s, 0, 0, 1000);
attachInfo.AttachmentPostID = NewPostId;
attachInfo.AttachmentSiteID = 3;
ForumAttachmentInfoProvider.SetForumAttachmentInfo(attachInfo);
}
}
}

User avatar
Kentico Legend
Kentico Legend
Brenden Kehren - 1/24/2013 1:51:34 AM
   
RE:Forum Posts
I do have one question though, my old forum posts have HTML tags in them and I'd like to keep them as it keeps the formatting in the post. Right now I'm stripping out the tags and it works although the post looks horrible. Aside from doing a string.replace() is there another anything else you suggest?

User avatar
Certified Developer 13
Certified Developer 13
josefd@kentico.com - 1/24/2013 7:34:19 AM
   
RE:Forum Posts
Hello,

Thank you for sharing the code. As for the question, I'm afraid there is no easy way to replace HTML tags. And I don't know of any other way than string.replace() or maybe regular expressions.

Regards,
Josef Dvorak

User avatar
Kentico Legend
Kentico Legend
Brenden Kehren - 1/25/2013 4:09:30 PM
   
RE:Forum Posts
Thank you again. I was able to use the html in the new site so no need to strip it out. I'm running the code now against my test site (diffenet code base and database than my local environment) and when I get to inserting an attachment the process stalls out or takes 5-10 minutes for a 45kb image to be attached. Whereas with my local test site it worked just fine. This is the code block that is stalling, in particular when the attachment is saved. When I check my SQL Server statistics it shows there are several database locks. So I stop and start my sql service and start fresh and perform the action and still happens on the same line of code when I attach an image.

Are there any site settings that may cause this particular action?
private void AttachImages(int OldPostId, int NewPostId)
{
// get the physical path on the server D:\Apps\Site\3\PostID
string path = HostingEnvironment.ApplicationPhysicalPath + @"3\" + OldPostId.ToString() + @"\";
if (CMS.IO.Directory.Exists(path))
{
// get all the files in the directory
// might need to filter by *.jpg but previous app already did that so no worries
string[] files = CMS.IO.Directory.GetFiles(path);
// for every file in the directory, create a new forum attachment
foreach (string fp in files)
{
ForumAttachmentInfo attachInfo = new ForumAttachmentInfo(fp, 0, 0, 1000);
attachInfo.AttachmentPostID = NewPostId;
attachInfo.AttachmentSiteID = 1;
try
{
// stalls out here
ForumAttachmentInfoProvider.SetForumAttachmentInfo(attachInfo);
}
catch { }
}
}
}

User avatar
Certified Developer 13
Certified Developer 13
josefd@kentico.com - 2/8/2013 6:30:59 AM
   
RE:Forum Posts
Hello,

Have you been able to resolve the issue yet? Would it be possible to turn on SQL debug, to see what is going on, where the DB stalls? Thank you.

Regards,
Josef Dvorak

User avatar
Kentico Legend
Kentico Legend
Brenden Kehren - 2/8/2013 2:30:35 PM
   
RE:Forum Posts
I have not had time since my last post. I will attempt to debug in SQL and report back what I find.