Thursday, September 1, 2011

Provisioning Wiki Pages in SharePoint 2010 Web Template

By now, you may have noticed that the venerable Web Part Page we all knew and loved in SharePoint 2007 is being replaced by the Wiki page in 2010.  The main difference is that Wiki pages can accept HTML intermixed with web parts, which gives you more layout options.

This posting is about how to provision Wiki pages into a library from a Web Template definition.  There doesn't seem to be much information about how to do this and there are a couple of tricks that took us quite some time to figure out.  Hence, this post.

The goal is to create a web template that includes pages pre-populated into the SitePages library. It works a little bit like provisioning a Web Part Page, but there are a couple of hidden and poorly documented additions.

I'm assumng that you already have a web template to start with and just want to add Wiki pages to the SitePages library.

Phase 1 - Getting the Raw Material

  1. Create the Page in SharePoint. Add web parts and edit HTML until you're happy with it.
  2. Save the Site into the Solution Gallery (From Site Settings, select Save site as template)
  3. Download the Solution (Open the Solution Gallery and download the file)
  4. Import the WSP into a new Visual Studio 2010 project. This is a working project from which you'll extract the components you'll need.
  5. Open another copy of Visual Studio with Your Live Project
Phase 2 - Moving components

You'll need to locate and copy the following components. 
  1. In SitePages_DocumentTemplates: Copy the Elements.xml file into your live project.  This contains the basic page definitions.  (More about the contents of this file later).
  2. PropertyBags.  Locate and copy the entries for your library and file(s) to your live project.  This contains the HTML used to generate pages at runtime.
Phase 3 - Package, Deploy and Test

Using Visual Studio, build and package the solution.  Add the solution to your SharePoint server and create a test site. 

How I Think it Works

As I mentioned, documentation on this is extremely scarce, but here are some notes about how I think the Wiki page works and how it differs from the old Web Part Page.

At first glance the CAML for Wiki pages appears to be identical to Web Part Pages. but there are several important differences:
  1. In the File element, the Path must be set to "wkpstd.aspx" which is the base Wiki page.  Your page is rendered using this page as a starting point.
  2. As before, web parts added to the page are declared using AllUsersWebPart elements, but
    1. The WebPartOrder attribute has no effect on display order
    2. The WebPartZoneID is always "wpz"
    3. The ID is set to a GUID used to locate the HTML (See below)
  3. Inside the Wiki web part there are two properties that contain the HTML used in the body of the web part.  These are encoded using HTMLEncode.  These are:
    1. MetaInfo
    2. WikiField 
  4. In the PropertyBag element, the WikiField described above is repeated.  It appears that this HTML is what is actually used when the web part is rendered. The HTML inside the Web Part spec doesn't seem to be used.
The GUID in the Web Part ID attribute appears in the HTML in the WikiField.  I believe the code that renders the Wiki web part uses the GUIDs to determine where in the HTML to insert the web parts.