About Me

I've been working with SharePoint for about 4 years now, which is a lot of a 24 year old's career! I both love it and hate it :-) I wouldn't say I specialise in anything, just SharePoint! I am a dev, so some of my posts will contain XML, C#, references to the 14 hive, but I'm also an admin so will often talk about stsadm, powershell and central admin. Some of my blog posts may be very simple references to improve coverage of common pitfalls, some may be in-depth walkthroughs, and some may just be a rant!

Monday, 21 March 2011

Upgrading SharePoint 2007 Page Layouts when migrating to 2010

It's blog number two! This is a dev one. I'm not going to tell you what code changes are needed to update 2007 page layouts to 2010, I'm going to address the less covered but more annoying issue of how do I get these new page layouts into the 2010 environment?

Before you read on, I'm going to assume that you were not using the standard MOSS page layouts, "defaultLayout.aspx", "searchmain.aspx" etc, and that you were creating your own custom ones (you better not have customised the default ones!).

Sounds easy, well you just upload them! If you upload them how are you going to associate every page with every new layout? There is a way to do that and it's documented here, just wrap the layouts into a feature to automatically upload them, or upload them manually, then use this code (adapted to your environment) to update all pages to use the new page layouts (http://sharepointers.blogspot.com/2008/09/programmatically-update-page-layouts.html). Nice idea I think! But the only issue with it is, we're trying to do the migration from 2007 to 2010 in a weekend, this process took about 8 hours to update all of our pages and we have about 200GB of content which I'd say is pretty small.

So there is another way. First of all you will want to create your upgraded page layouts. Then you should put them into a feature in Visual Studio 2010. These steps you should know how to do, but if not, just create a new feature, then create a new module, add your new page layouts into the module, then in the elements.xml file add in a module to upload them one by one into the master page gallery, sample xml code below (I'm being brief because this isn't the point of this blog)...

<Module Name="CompanyABC.PageLayouts" Url="_catalogs/masterpage">

      <File Path="CompanyABC.PageLayouts\CompanyABC-AdvancedSearchLayoutv4.aspx" Url="CompanyABC-AdvancedSearchLayoutv4.aspx" Type="GhostableInLibrary">

        <Property Name="Title" Value="CompanyABC - Search - Advanced v4"/>

        <Property Name="ContentType" Value="Welcome Page"/>

        <Property Name="PublishingPreviewImage" Value="~SiteCollection/_catalogs/masterpage/Preview Images/CompanyABC-ContentOnly.jpg, ~SiteCollection/_catalogs/masterpage/Preview Images/CompanyABC-AdvancedSearchLayout.jpg" />

        <Property Name="Description" Value="This is the advanced search page for the CompanyABC intranet search area v4."/>

        <Property Name="PublishingAssociatedContentType" Value=";#Welcome Page;#0x010100C568DB52D9D0A14D9B2FDCC96666E9F2007948130EC3DB064584E219954237AF390064DEA0F50FC8C147B0B6EA0636C4A7D4;#" />

      </File>

</Module>

Ok so here's the magic part. SharePoint won't let you copy over the top of something because it complains that the page layout has a load of associations therefore it can't be overwritten... but what you can do, is upload all of your new page layouts with a slight adjustment to the file name (e.g. "v4" as I've done), then simply rename them to be the same as the old ones! It works! So, we did this with a feature reciever...

public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            // Move new Page Layouts to replace old 2007 ones
            SPSite thisSite = (SPSite)properties.Feature.Parent;

            using (SPWeb thisWeb = thisSite.RootWeb)
            {                
                ArrayList pageLayoutOldAndNew = new ArrayList();

                string[] pair = new string[2];
                // Format: Page Layout original, new version
                pageLayoutOldAndNew.Add(new string[2] { "CompanyABC-AdvancedSearchLayout.aspx", "CompanyABC-AdvancedSearchLayoutv4.aspx" });
                pageLayoutOldAndNew.Add(new string[2] { "CompanyABC-Article.aspx", "CompanyABC-Articlev4.aspx" });
                pageLayoutOldAndNew.Add(new string[2] { "CompanyABC-ContentOnlyFullWidth.aspx", "CompanyABC-ContentOnlyFullWidthv4.aspx" });
                pageLayoutOldAndNew.Add(new string[2] { "CompanyABC-ContentOnly.aspx", "CompanyABC-ContentOnlyv4.aspx" });
                pageLayoutOldAndNew.Add(new string[2] { "CompanyABC-Dashboard.aspx", "CompanyABC-Dashboardv4.aspx" });
                pageLayoutOldAndNew.Add(new string[2] { "CompanyABC-Home-Subsites.aspx", "CompanyABC-Home-Subsitesv4.aspx" });
                pageLayoutOldAndNew.Add(new string[2] { "CompanyABC-Home.aspx", "CompanyABC-Homev4.aspx" });
                pageLayoutOldAndNew.Add(new string[2] { "CompanyABC-PeopleResults.aspx", "CompanyABC-PeopleResultsv4.aspx" });
                pageLayoutOldAndNew.Add(new string[2] { "CompanyABC-SearchMain.aspx", "CompanyABC-SearchMainv4.aspx" });
                pageLayoutOldAndNew.Add(new string[2] { "CompanyABC-SearchResults.aspx", "CompanyABC-SearchResultsv4.aspx" });
                pageLayoutOldAndNew.Add(new string[2] { "CompanyABC-ZonesContentFullWidth.aspx", "CompanyABC-ZonesContentFullWidthv4.aspx" });
                pageLayoutOldAndNew.Add(new string[2] { "CompanyABC-ZonesContent.aspx", "CompanyABC-ZonesContentv4.aspx" });

                SPFolder masterPages = thisWeb.GetFolder("/_catalogs/masterpage/");
                if (masterPages.Exists)
                {
                    for (int pag = 0; pag < masterPages.Files.Count; pag++)
                    {
                        SPFile thisFile = masterPages.Files[pag];
                        foreach (string[] _pair in pageLayoutOldAndNew)
                        {
                            if (thisFile.Name.ToLower().Equals(_pair[0].ToLower()))
                            {
                                if (web.GetFile("/_catalogs/masterpage/" + _pair[1]).Exists == true)
                                {
                                    web.GetFile("/_catalogs/masterpage/" + _pair[1]).MoveTo("/_catalogs/masterpage/" + _pair[0], true);
                                }
                            }
                        }
                    }
                }
            }
        }

Then with this feature activated on the top level web for the site collection, this will update all the page layouts and will only take a few seconds to run, enjoy!

No comments:

Post a Comment