Migrate pages from EPiServer 4.x site to CMS

by: Atul Chaturvedi

Stuck while importing pages to EPiServer CMS site from your older 4.x site, no need to worry, here is a simple recursive method to create those un-imported Pages from older into newer database. Some times the "Import tool" in admin mode of CMS site fails to import all Pages as in 4.x site but thanks to EPiServer.DataFactory.Instance.Save, a very useful method using which and an algorithm which I'm going to discuss after few more irritating lines can still evolve yourself from such migration issues.

I'm assuming that you have both the databases (the older of 4.x and newer of CMS) on the same SQL Server and created a root page in CMS site under which you like to create all un-imported Pages from 4.x site.

So lets start with what you are exactly looking for, the code part of this blog post. First create a stored procedure as below in the database of 4.x site as I do, (you can do it other way also by taking care of the way of accessing database like DataBaseName.dbo.SPName) to fetch all immediate child Pages of Parent page passed as a parameter to this stored procedure with their respective PageIDs from 4.x Database and CMS Database (if exist), PageTypes, MainBody etc.

 

CREATE PROCEDURE getChildren
@ParentPageName NVARCHAR(50) = NULL
AS
BEGIN
    DECLARE @PageID INT

    select @PageID=pkID  from tblPage where Name = @ParentPageName


    select p4.[Name] PageName, p4.pkID PageID4, p5.pkID PageID5, pr4.LongString MainBody, pt4.[Name] PageTypeName
    from db4Site.dbo.tblPage p4
    left join db5Site.dbo.tblPage p5 ON p4.Name = p5.Name
    left join (db4Site.dbo.tblProperty pr4
    inner join db4Site.dbo.tblPageDefinition pd4 ON pd4.pkID = pr4.fkPageDefinitionID and pd4.Name = 'MainBody') ON pr4.fkPageID = p4.pkID
    inner join db4Site.dbo.tblPageType pt4 ON pt4.pkID = p4.fkPageTypeID
    where p4.fkParentID = @PageID

END

 

Then make a call to below recursive method in the EPiServer CMS Site page.

        /// <summary>
        /// Recursive Method to migrate unimported pages of EPi4 to EPi5
        /// </summary>
        /// <param name="parentPageName">PageName of parent which exist in EPiServer CMS site.</param>
        /// <param name="parentPageID">PageID of parent in EPiServer CMS site</param>
        private void CreateChildren(string parentPageName,int parentPageID)
        {
            SqlConnection conn;
            SqlCommand cmd;
            SqlDataReader reader;
            SqlParameter[] sprmPage = new SqlParameter[1];
            try
            {

                conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["EPiServer4DB"].ToString());
                sprmPage[0] = new SqlParameter("@ParentPageName", parentPageName);
                cmd = new SqlCommand("getChildren", conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(sprmPage[0]);
                conn.Open();
                reader = cmd.ExecuteReader();
                DataTable dtPages = new DataTable();
                dtPages.Load(reader);

                conn.Close();

                foreach (DataRow dr in dtPages.Rows)
                {
                    if (dr["PageID5"].ToString() != "")
                    {
                        CreateChildren(dr["PageName"].ToString(), Convert.ToInt32(dr["PageID5"]));
                    }
                    else
                    {
                        try
                        {
                            PageData newPage = DataFactory.Instance.GetDefaultPageData(new PageReference(parentPageID),

                                                              dr["PageTypeName"].ToString(), EPiServer.Security.AccessLevel.NoAccess);
                            newPage.PageName = dr["PageName"].ToString();
                            if (newPage["MainBody"] != null)
                            {
                                newPage["MainBody"] = dr["MainBody"].ToString();
                            }
                            DataFactory.Instance.Save(newPage, EPiServer.DataAccess.SaveAction.Save, EPiServer.Security.AccessLevel.NoAccess);

                            //**********************************************************************//
                            conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["EPiServerDB"].ToString());
                            string sql = "select pkID from tblPage where Name = '" + dr["PageName"].ToString() + "' and fkParentID = " + parentPageID;
                            cmd = new SqlCommand(sql, conn);
                            cmd.CommandType = CommandType.Text;
                            conn.Open();
                            int PageID = Convert.ToInt32(cmd.ExecuteScalar());

                            conn.Close();
                            //**********************************************************************//
                            CreateChildren(dr["PageName"].ToString(), PageID);
                        }
                        catch(Exception ex)
                        {

                        }
                    }
                }
            }
            catch(Exception E)
            {

            }
        }

 

You can also customize this method and check for any specific page which gives any

particular error while saving. Please reply in case of any clarification regarding the code.

17 April 2008

Tags:


    Comments

    1. Very Helpful and Time saving.
    Post a comment    
    User verification Image for user verification  
    Atul Chaturvedi

    About me

    I'm a computer science engineer from MUMBAI, INDIA. Also awarded with "NIIT Award of Excellence in Technology 2007-08" (NIIT Technologies Ltd is a NASSCOM Top 20 company).

    My technical interest includes architecture, OOAD and implementation of web applications.


     MCP logo

    Syndications


    Archive


    Tag cloud

    EPiTrace logger