Home > MOSS 2007 > You update a PageLayout, re-deploy it but you cant see your changes

You update a PageLayout, re-deploy it but you cant see your changes


Have you had this problem? Well you are not alone. While working on a News Publishing site that utilised the built in MOSS 2007 Publishing Infrastructure Feature we too came across this issue.

My colleague, who was tasked with doing the PageLayout part, had suffered for weeks with this as every time he made a change to the PageLayout and re-deployed it, he then had to delete the Site Collection and re-create a brand new Site Collection to pick up the effects of the changes he had made.

Deleting the SPWeb (Site) will not make a difference. The reason for this is that the PageLayout .aspx files are stored in an SPList called “Master Page Gallery” which exists in the RootWeb of the Site Collection.

For some reason the PageLayout pages are “customised” i.e. stored in the Database. So regardless of what changes you make to the file that is stored on the file system it wont take effect because SharePoint is getting the file from the Database.

Luckily there is a way around this which is to get the instance of the file from the “Master Page Gallery” and call the RevertContentStream() method on it. Calling this method Returns the file to its original uncustomized state so that its logic becomes cached in memory (also known as “ghosted”) rather than stored in the database.

Since the PageLayout is deployed as a Feature you can add a Feature Receiver and add the following code on the “FeatureActivated” event: 


public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
    if (properties == null)
    {
        throw new ArgumentNullException("properties");
    }
    using (SPWeb web = ((SPWeb)properties.Feature.Parent))
    {
        using (SPWeb rootWeb = web.Site.RootWeb)
        {
            // Uncustomises the pagelayout file so that we dont have to tear down the whole site collection
            // every time we make a change to the pagelayout page.
            SPQuery query = new SPQuery();
            StringBuilder queryBuilder= new StringBuilder();
            queryBuilder.Append(
" <Where>");
            queryBuilder.Append(
" <Eq>");
            queryBuilder.Append(
" <FieldRef Name="FileLeafRef" />");
            queryBuilder.Append(
" <Value Type="Text">PageLayout.aspx</Value>");
            queryBuilder.Append(
" </Eq>");
            queryBuilder.Append(
" </Where>");
            // This is the Master Page Gallery List
            SPList pageLayoutList = rootWeb.GetCatalog(SPListTemplateType.MasterPageCatalog);
            query.Query = queryBuilder.ToString();
            SPListItemCollection items = pageLayoutList.GetItems(query);
            items[0].File.RevertContentStream();
        }
}

Advertisements
  1. January 29, 2013 at 12:17 am

    Wow, this piece of writing is pleasant, my younger
    sister is analyzing these things, therefore I am going to let know her.

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: