Using Save Site as Template with Publishing Sites – Part 4, Fixing Up the Page Module

Fixing up the page is a bit more complex than the page layout. Some publishing pages are based on page layouts that consist of Web Part Zones while others contain wiki content. The former is less complicated than the latter but I have selected the latter as it is the more common scenario in SharePoint 2010. As with the page layout, the page has some properties that need to be preserved and some properties that need to be preserved but edited by hand. In addition the page also has one very important property that is completely missing in the export that must be added by hand!

Switch to the extract solution in Visual Studio and edit elements.xml for the Pages_ module. I’ve highlighted the pieces you need to apply to the MyPublishingFeatures solution below.

Notice the BinarySerializedWebPart element. The Web Part is actually a plain old Content Editor Web Part. If the Web Part was placed into a normal Web Part zone instead of in wiki content it would appear in an AllUsersWebPart element instead. As it stands because it is a BinarySerializedWebPart we will need to edit this XML in a way that has some downsides I’ll discuss in a bit.

Also notice the PublishingPageContent property. The original page has some formatting which is missing from this field. We have some work to do here as well.

Switch back to the MyPublishingFeatures solution and edit the elements.xml file for the MyPublishingPage feature. Start by editing the Module and File elements so that the Url and Type attributes are correct and so that the File element is not self-closing. The result should look like this:

Next, copy the BinarySerializedWebPart and the highlighted properties shown above and paste them between the opening and closing File elements. What you copy will depend on the actual content fields of your target page, but you do not want system properties like _ModerationStatus, FileLeafRef, or ID. SharePoint will populate these values as is appropriate when it provisions the publishing page on your target site.

You are now ready to fix up the BinarySerializedWebPart if necessary. The problem in my example is that I used a site collection with a different server relative URL to create the prototype compared to the actual target for my destination site. Because of this I need to edit the XML.

This means you must know ahead of time what your publishing site’s URL scheme will be and it detracts from your solution’s flexibility. An alternative is to insert the Web Part programmatically, but this discussion is outside the scope of this series of posts. There is an important disclaimer I need to make here: It is possible to have BinarySerializedWebPart elements in a traditional Web Part zone. For example, this is the case if you have an uncustomized list view. You don’t have to fix up the XML in this case.

Once you’ve dealt with the BinarySerializedWebPart, you need to fix the PublishingPageContent property value. You don’t need the extract solution for this. The content is in the Sample-Page.aspx file contained by the module. Open Sample-Page.aspx in the editor. Locate the PublishingPageContent value and copy it to the clipboard.

Switch back to elements.xml and replace the current value of PublishingPageContent with the contents of the clipboard. The chances are good that your XML is no longer valid. In particular, you will need to edit any instances of &nbsp and change them to &nbsp. Pay close attention to the editor, it will show any invalid XML with a red squiggly line beneath the problem text.

At this point you should remove all of the green text from Sample-Page.aspx. As is the case with the extraneous properties, SharePoint will take care of this when it provisions the module.

Only one step remains; the addition of the very important missing PropertyPublishingPageLayout. This property is what tells SharePoint which page layout to use for the page. Without it you will get a runtime error. Add the following Property within the opening and closing File elements.

<Property
				Name="PublishingPageLayout" Value="~SiteCollection/_catalogs/masterpage/Sample.aspx, Sample Layout"/>

 

The completed Module XML looks like this:

Deploy the solution and navigate to the page. The result looks like this!

Table of Contents

Using Save Site as Template with Publishing Sites – Introduction

Using Save Site as Template with Publishing Sites – Part 1, Saving the Site and Importing the Solution to Visual Studio

Using Save Site as Template with Publishing Sites – Part 2, Creating the New Custom Visual Studio Solution

Using Save Site as Template with Publishing Sites – Part 3, Fixing Up the Page Layout Module

Using Save Site as Template with Publishing Sites – Part 4, Fixing Up the Publishing Module

Using Save Site as Template with Publishing Sites – Conclusion

 

Author: Doug Ware