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);
}
}
}