We have been creating multiple MOSS2K7 sites in our development environment and then moving them up to production servers, as you usually do in this game. Instead of creating them by hand I have been using the stsadm.exe -o backup/restore functionality which works bichenly. But, there are a few problems that I have run into. Part of the problem when using the backup/restore functionality is that SharePoint page layouts hold onto the old server information. After doing a successful restore of one of my MOSS2K7 site I tried to edit the page settings through the Page — Edit Page Settings link when I got the error “Value does not fall within the expected range”. This problem is because the newly restored site is holding onto an old site definition and is unable to find the old site def. If you look at the page layout through the Content Structure page you can see the old server referenced. A while ago I came across some code that fixes this issue. However I can’t for the life of me find where the hell I found it. So if you recognize some of the variable names let me know so I can give you some credit. The code wasn’t written as a web part but as a command line utility, so for those who don’t have access to the server this code wasn’t much of use. So czech out the web part that I created, drop this onto your page and the part loops through all the sites and subsites in your site and it fixes up the layout urls.

*Note that if you are running this code from the web part that the code must be run using the SPSecurity.CodeToRunElevated objects. If the code is not run with elevated privileges you will receive an “access” error.

using System;
using System.Text;
using System.ComponentModel;
using System.Collections.Generic;
using System.Security.Permissions;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.Design.WebControls;

using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.Security;
using Microsoft.SharePoint.Publishing;
namespace ScaredPanda.Web.UI.WebControls.WebParts
{
    public class FixLayoutUrls : System.Web.UI.WebControls.WebParts.WebPart
    {

        protected override void CreateChildControls()
        {
            SPSecurity.CodeToRunElevated ElevatedCode = new SPSecurity.CodeToRunElevated(CodeToRunElevated);
            SPSecurity.RunWithElevatedPrivileges(ElevatedCode);

            base.CreateChildControls();
        }

        protected void CodeToRunElevated()
        {
            try
            {
                using (SPSite oSite = new SPSite(SPContext.Current.Web.Url))
                {
                    Context.Response.Write(oSite.RootWeb.Url);
                    FixPages(oSite.RootWeb);
                }

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private void FixPages(SPWeb oWeb)
        {
            try
            {
                if (!PublishingWeb.IsPublishingWeb(oWeb)) return;

                PublishingWeb pw = PublishingWeb.GetPublishingWeb(oWeb);
                SPListItemCollection oList = pw.PagesList.Items;

                string sSiteUrl = oWeb.Site.Url;
                oWeb.AllowUnsafeUpdates = true;
                this.Context.Response.Write("Processing " + oWeb.Title + "(" + oList.Count.ToString() + " pages)...");

                foreach (SPListItem oPageItem in oList)
                {
                    string s = (string)oPageItem[FieldId.PageLayout];
                    if (s != null && !s.StartsWith(sSiteUrl))
                    {
                        this.Context.Response.Write(”Fixing ” + oPageItem.Title + ” (” + oPageItem.Url + “)”);
                        oPageItem[FieldId.PageLayout] = sSiteUrl + s.Substring(s.IndexOf(”/”, 9));
                        oPageItem.SystemUpdate();
                    }
                }

                foreach (SPWeb oSubWeb in oWeb.Webs)
                {
                    FixPages(oSubWeb);
                    oSubWeb.Dispose();
                }
            }
            catch (Exception ex)
            {
                this.Context.Response.Write(”Layout fix failed at site: ” + oWeb.Title + “”);
                this.Context.Response.Write(ex);
            }
        }
    }
}
This post has No comment. Add your own.