Feeds:
Posts
Comments

Archive for December, 2011

Introduction

This article explains how to manage the layout of your site pages (= wiki pages) with the standard SharePoint 2010 Foundation functionality .
Default SharePoint pages are stored in a “Site Pages” library which is basically a wiki page library.

This article assumes you know how to create SharePoint 2010 projects in Visual Studio 2010 and how to deploy them.

The issue

The layout of your Site Pages is controlled by the “Text Layout” button in the ribbon control. SharePoint 2010 has a few default layouts defined. But there is no way to extend this functionality like you can with page layouts and the publishing feature.
A customer wanted to have the 2 column layout by default when creating a new page. He also wanted to have a logo and some web parts on the page as well.

The solution.. huh hack?

What the “Text Layout” button does is simply saving some HTML code into a property of the page. We can do this ourselves every time a new page is created by I adding an event receiver to the wiki library.

Open your SharePoint 2010 project.

Add resource file

  • Add a resource file to your project.
    This is needed to contain the actual HTML that will define your layout.
  • Here is my resource file with the HTML.
  • The HTML I used in the above screenshot is
    </pre>
    <div class="ExternalClass1E4D6ECE06C64E0C84066BDC723BF78D">
    <table id="layoutsTable" style="width: 100%;">
    <tbody>
    <tr style="vertical-align: top;">
    <td style="width: 66.6%;"></td>
    <td style="width: 33.3%;">
    <div class="ms-rte-layoutszone-outer" style="width: 100%;">
    <div class="ms-rte-layoutszone-inner">
    <div style="text-align: right;"><img src="/_layouts/images/AmToPm.SharePoint/logo.png" alt="logo" border="0" /></div>
    </div>
    </div></td>
    </tr>
    </tbody>
    </table>
    <span id="layoutsData" style="display: none;">false,false,2</span></div>
    <pre>
    

Add an Event receiver

  • Right click on your project and an event receiver
  • Click add. In the next page make sure to select the Wikipage library and “item added”
  • You now have your event handler. It will trigger every time a new wiki page is added to a wiki page library.
  • Add the following code to your event receiver
    ///
    <summary> /// An item was added.
    /// </summary>
    public override void ItemAdded(SPItemEventProperties properties)
    {
       base.ItemAdded(properties);
    
       // TODO: if needed do some checks here
    
       try
       {
    	   SPListItem item = properties.ListItem;
    
    	   this.EventFiringEnabled = false;
    	   item[SPBuiltInFieldId.WikiField] = ResourceFile.Wiki2ColumnMarkup;
    	   item.SystemUpdate(false);
    	   this.EventFiringEnabled = true;
       }
       catch (Exception ex)
       {
    	   Log.ErrorException("Updating the wikipage layout failed.", ex);
       }
    }
    

Make sure to disable the event firing before you update your page.

Read Full Post »

Follow

Get every new post delivered to your Inbox.