How to update properties of an already provisioned and in use page layout

by James 6/12/2008 1:08:00 AM

Let's say you have a custom page layout and you want to give this page layout an unique Publishing Preview Image. To allow users to easily choose the right page layout among many different layouts.

This can be done easily if it is a NEW page layout. All you need to do is to define the PublishingPreviewImage element when you provisioning it.

[code:xml]

<!-- PageLayout.xml -->

<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <Module Name="PageLayouts" Url="_catalogs/masterpage" Path="PageLayouts" RootWebOnly="TRUE">

<File Url="CustomPageLayout.aspx" Type="GhostableInLibrary" IgnoreIfAlreadyExists="TRUE"  >
      <Property Name="Title" Value="My Custom Page Layout" />
      <!-- new added description -->
      <Property Name="MasterPageDescription" Value="This page layout is used for new sites." />
      <!-- new added preview image -->
      <Property Name="PublishingPreviewImage" Value="~SiteCollection/_layouts/images/pagelayouts/custompagelayout.gif" />
      <Property Name="ContentType" Value="Custom Content Type" />
      <Property Name="PublishingAssociatedContentType"
                Value=";#Custom Content Type;#0x010100C568D1D2D3D0A14D9B2FDCC96666E9F2007123456EC3DB064584E219954237AF123456;#"/>
</File>

</Module>

</Elements>


[/code]
 

Problem -

What if you want to do this for the page layout which already in use and provisioned?  The challenging part is that when page layout has already been provisioned and used across webs. Any metadata changes made in page layout provision feature will NOT update its metadata in SharePoint site.

That means even if you changed PublishingPreviewImage element value. The preview images for the already in use page layouts in CreatePage.aspx will still use default image (DefaultPageLayout.png)

 

This screen shot shows the page layout is still using default preview image

James Tsai .Net SharePoint Blog - Page Layout Preview Image Default 

 

Solution -

Create a SPFeatureReceiver class and register it with same feature that provisions page layout(s) . In the FeatureActivated, update each page layout metadata base on the page layout manifest file.

Every time you want to update custom page layout property, just update manifest file, deactivate and re-activate this page layout provisioning feature.

This screen shot shows after reactivated the feature page layout is now using new preview image and new description.

James Tsai .Net SharePoint Blog - Page Layout Preview Image Default

Note: Although you can update each individual layout manually , you would expect the update should be done automatically every time you change layouts manifest and provision them again. That is the main reason to warp the logic inside feature receiver class to mimic the automation.

 

Please see below for code samples

//Feature receiver class

class PageLayoutProvisioningFeature : SPFeatureReceiver
{
    public override void FeatureActivated(SPFeatureReceiverProperties properties)
    {
        using (SPSite site = (SPSite)properties.Feature.Parent)
        {
            //Load manifest file as XmlDocument.
            XmlDocument manifestXml = LoadPageLayoutsManifestAsXml(properties);

            //Load all page layouts for the site into dictionary
            Dictionary<string, PageLayout> allLayouts = LoadSitePageLayouts(site);

            //Now we have an entire page layouts manifest as XmlDocument.
            //We also have a collection of all page layouts
            //We can now step through each page layout defined in manifest and check every propertie
            //against layouts in allLayouts Dictionary.

            //Update layout property in allLayouts Dictionary with value defined in manifest file

            //Check out page layout
            //Call pagelayoutsDic[pagelayoutName].Update()
            //Check in page layout
        }       
    }

    //LoadPageLayoutsManifestAsXml(){} ... see next section

    //LoadSitePageLayouts(){} .... see next section
}

LoadPageLayoutsManifestAsXml method

[code:c#]

private XmlDocument LoadPageLayoutsManifestAsXml(SPFeatureReceiverProperties properties)
    {
        //Get feature definition as xml node
        XmlNode def = properties.Definition.GetXmlDefinition(System.Globalization.CultureInfo.CurrentCulture);
        //Create xml namespace manager
        string wss = "wss:";
        string xpath = wss + "ElementManifests/" + wss + "ElementManifest";
        XmlNameTable nameTable = new NameTable();
        XmlNamespaceManager manager = new XmlNamespaceManager(nameTable);
        manager.AddNamespace("wss", "http://schemas.microsoft.com/sharepoint/");
        //Create an enumerator and get ElementManifest node
        IEnumerator enumerator = def.SelectNodes(xpath, manager).GetEnumerator();
        XmlNode current = (XmlNode)enumerator.Current;

        //Get manifest file name PageLayouts.xml from ElementManifest node
        string PageLayoutsManifestFileName = current.Attributes["Location"].Value;

        //Load manifest file to XmlDocument object
        XmlDocument doc = new XmlDocument();
        doc.Load(feature.Definition.RootDirectory + "/" + PageLayoutsManifestFileName);

        return doc;
    }

 

[/code]

LoadSitePageLayouts method

[code:c#]

private Dictionary<string, PageLayout> LoadSitePageLayouts(SPSite site)
    {
        PublishingSite publishingSite = new PublishingSite(site);               
        PageLayoutCollection pageCollection = publishingSite.PageLayouts;
        Dictionary<string, PageLayout> pagelayoutsDic = new Dictionary<string, PageLayout>();
        foreach (PageLayout layout in pageCollection)
        {
            pagelayoutsDic.Add(layout.Name, layout);
        }              

        site.Dispose();       

        return pagelayoutsDic;
    }

[/code]

Currently rated 4.0 by 4 people

  • Currently 4/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: , , , , ,

Programming | SharePoint

Comments

Powered by BlogEngine.NET 1.4.5.0
Theme by Mads Kristensen Adopted by James Tsai

About the author

Name of author James Tsai
.NET / SharePoint Consultant
Columbus, OH

E-mail me Send mail

Calendar

<<  September 2010  >>
MoTuWeThFrSaSu
303112345
6789101112
13141516171819
20212223242526
27282930123
45678910
View posts in large calendar

Certifications

MCPD
MCTS

Recent comments

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2010

Sign in