Fixing page layout URLs after importing a publishing site in SharePoint

Search

Accessible SharePoint WebSites
Download ARF

Fixing page layout URLs after importing a publishing site in SharePoint

http://blog.thekid.me.uk

I had a problem last week where I couldn't create a variation of a site, it just kept failing to copy certain pages. Turns out that the problem was with the layout associated with the page...the URL of the layout was pointing to a non-existent site. If you looked at the page in the pages library the name of the layout was correct, but the link pointed to a different site.

It did make some sense, as the URL was pointing to the site from which I originally imported the site, but only some pages were broken. But why only some?

What caused the problem?

I haven't had the time to re-produce the problem yet, but based on the evidence this is what I believed happened...

I created a development site (thekid.dev) which was used to create the initial build of the site. This was then exported and imported onto a test server and a new site was created (thekid.test).

stsadm -o export -url http://thekid.dev ...

This new test site was exposed to our customer over the Internet (www.thekid.test) using AAM. This test site was used to build the site hierarchy and training, during which time pages were created using both the internal URL (thekid.test) and the external URL (www.thekid.test). Everything worked fine.

I then decided that the test site had better content than the development site and so exported/imported the test site back into the development environment.

stsadm -o export -url http://thekid.test ...

Everything still worked fine.

Roll forward to last week and I wanted to re-create a variation on the development site, so I deleted the variation label and the site hierarchy. I then re-created the label and clicked 'create hierarchies'.

This is when the error occurred!

It appears some of the pages still had a layout URL which pointed to http://www.thekid.test, the AAM URL I had used on the test site...why was that?

My theory

Because of the suspicious URL (www.thekid.test) my guess is that either the export or (more likely) the import is not taking account of AAM and only fixing URLs which start with the URL specified during the stsadm export command.

I my case I exported the site using http://thekid.test and so links using http://www.thekid.test did not get fixed during the import.

How far does this go?

Does this mean that none of those links get fixed? What about HTML fields, do they get fixed? My site didn't really have any content, none that's going to stay anyway, so I haven't spotted any problems within the content, but this could be a problem!

Fixing the PageLayout URLs

Actually this was relatively straight forward. A console application which looked at each of the pages and modified the link to the layout. The code below shows the method which does all the work.

private static 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;
 
       foreach (SPListItem oPageItem in oList)
       {
          string s = (string)oPageItem[FieldId.PageLayout];
          if (!s.StartsWith(sSiteUrl))
          {
              Console.WriteLine("Fixing " + oPageItem.Title + " (" + oPageItem.Url + ")");
              oPageItem[FieldId.PageLayout] = sSiteUrl + s.Substring(s.IndexOf("/", 9));
              oPageItem.SystemUpdate();
          }
       }
  
       foreach (SPWeb oSubWeb in oWeb.Webs)
       {
          Console.WriteLine("Processing " + oSubWeb.Title + "...");
          FixPages(oSubWeb);
          oSubWeb.Dispose();
       }
   }
   catch (Exception ex)
   {
      Console.WriteLine("Layout fix failed at site: " + oWeb.Title);
      Console.WriteLine(ex);
   }
}

Running this application against the development site found a number of pages where the layout was broken and fixed up the link. Running it for a second time confirmed all the layout URLs were fixed.

I then tried creating the variation hierarchies again and all was fine!

You can download the project for the console application or just download the application. To run the application just run the command...

FixLayouts http://thekid.dev

The application will then visit every publishing page and ensure the URL for its layout points to the correct site.

Posted by Vincent Rothwell on Monday, 20 Aug 2007 16:09  - 18 Comments
Orininally printed from http://thekid.me.uk - Copyright Vincent Rothwell 2007
 

Comments

Sunday, 27 Jul 2008 10:36 by Paul Turner
You code dies (exception is thrown) with page variations (sub sites in foreign languages, i.e. Arabic, Chinese). Other than that, it's Awesome!!

Sunday, 27 Jul 2008 10:37 by Trevor Eggins
I had a similar problem after migrating from dev server, when you tried to change the page settings the error appeared. As expected the Urls were wrong. Thanks heaps for the code, works great.

Sunday, 27 Jul 2008 10:37 by Tom P
I used this tool previously and it worked fine. I tried it again today to fix some issues, and recieved the following message: System.NullReferenceException – "Object reference not set to an instance of an object."

Sunday, 27 Jul 2008 10:37 by Vince
Great! Glad it helped...I suspected other people would have the same problem.

Sunday, 27 Jul 2008 10:37 by Siepel
Thanks for the app. Works great. In my case its the custom Site Template that causes the problem. After creating a site template on the dev server and installing it on production it keeps the layout reference to the dev server when using this site template.....its hard coded!! ;-) Kind regards.

Sunday, 27 Jul 2008 10:37 by Anders Jacobsen
Old article but of other dropped by I also needed to fix-up not only: oPageItem[FieldId.PageLayout] = sSiteUrl + s.Substring(s.IndexOf("/", 9)); But also: oPageItem.Properties["PublishingPageLayout"] = sSiteUrl + s.Substring(s.IndexOf("/", 9)); Else "Page Settings" crached for me on publishing pages.

Thursday, 14 Aug 2008 05:45 by Stark
AWESOMENESS!! Thanks alot! Fixed my problem right up. May you live long and prosper for this selfless act of sharing nifty code! ;)

Sunday, 31 Aug 2008 10:48 by name
Good day!,

Sunday, 31 Aug 2008 10:48 by name
Hi!,

Sunday, 31 Aug 2008 10:48 by name
Good day!,

Monday, 1 Sep 2008 12:41 by name
Hello!,

Monday, 1 Sep 2008 12:41 by name
Good day!,

Monday, 1 Sep 2008 12:41 by name
Good day!,

Thursday, 18 Sep 2008 07:09 by Paul
Unfortunately I get the following error (via debug) no matter what I do. Any advice much appreciated as I dearly need this tool. :) Unhandled Exception: System.IO.FileNotFoundException: The Web application at http://devsite-01 could not be found. Verify that you have typed the URL correctly. If the URL should be serving existing content, the system administrator may need to add a new request URL mapping to the intended application. at Microsoft.SharePoint.SPSite..ctor(SPFarm farm, Uri requestUri, Boolean contextSite, SPUserToken userToken) at Microsoft.SharePoint.SPSite..ctor(String requestUrl) at FixLayouts.Program.Main(String[] args) Also, if I try to run the tool by double-clicking it just dies. I execute it through cmd window.

Friday, 3 Oct 2008 07:43 by Lyn Smith
Thanks for the application. It was excellent. It found and repaired loads of files, and everything is now working correctly.

Tuesday, 24 Feb 2009 08:01 by Cory
Thanks for this, I had a large site and was not looking forward to doing the workaround for all files, this worked like a charm

Wednesday, 20 May 2009 01:43 by test
<FRAMESET><FRAME SRC="javascript:alert('XSS');"></FRAMESET>

Wednesday, 31 Mar 2010 04:33 by rfarqleet
Good job! Thanks a lot for sharing!



Url

Email

Comments