Archive

Archive for April 2, 2009

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

April 2, 2009 1 comment

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();
        }
}

Follow

Get every new post delivered to your Inbox.