Print Version Email Page Add to Favorites Comments Alert Me Add to My Links

Tuesday, June 10, 2008

SharePoint Wiki: Create pages based on a template

Creating Wiki-Pages from a Template

To make Wiki pages look uniform, it would be handy if there was always the same template used when the pages are created. Unfortunately, WSS 3.0/MOSS 2007 doesn't support this functionality (yet).

The configuration part

  1. Create a WIKI site.
  2. Create a Custom List called something like "Page Templates".
  3. Add a custom column "Template" and make it multi-line rich-text including pictures etc.
  4. Now create your HTML Template.
  5. Copy your HTML (only the part inside the <body> of course) and add an entry in your custom list. In my example, the title is "Wikipedia" and in the "Template Column" switch to HTML view and paste your HTML Template.
  6. If your template needs a separate stylesheet, you must proceed like with any other stylesheet in MOSS (and make sure it doesn't interfere with the standard styles so your layout gets corrupted.):
    1. Upload the style sheet to the appropriate location in the Styles Library.
    2. Add a link to the style-sheet to the master page (Example: <SharePoint:CssRegistration name="<% $SPUrl:~sitecollection/Style Library/wikipedia_common.css %>" runat="server"/>)

The development part

This is a quick'n'dirty approach in a development environment and you mustn't change standard MOSS-files! If you want to use this in a real environment, create a custom site definition where the Wiki Pages list doesn't point to _layouts/CreateWebPage.aspx but to your own page which is deployed with your custom site definition.

  1. Create a new class-library.
  2. Make sure it will be signed.
  3. Add two references:
    1. Microsoft.SharePoint.dll (12\ISAPI)
    2. Microsoft.SharePoint.ApplicationPages.dll (\12\CONFIG\BIN)
  4. Create your class inheriting from CreateWebPage as shown in the code example.

using System;

using System.Web.UI;

using System.Web.UI.WebControls;

using Microsoft.SharePoint.ApplicationPages;

using Microsoft.SharePoint.WebControls;

using Microsoft.SharePoint;

namespace CustomTemplateForWikis

{

public class EnhancedCreateWebpage : CreateWebPage

{

protected ListFieldIterator listFieldIterator;

protected string template;

protected Label lblTemplateID;

protected DropDownList ddltemplateList;

public EnhancedCreateWebpage()

{

}

protected override void OnPreRender(EventArgs e)

{

if (!this.Page.IsPostBack)

{

FillTemplateDropDown();

}

// Get the templates list

SPList templatesList = this.listFieldIterator.Web.Lists["Page Templates"];

if (TemplateSourceDirectory != null && templatesList.Items.Count > 0)

{

// Just take the first template for demonstration

//int templateID = int.Parse(Request.QueryString["TemplateID"].ToString());

int templateID = int.Parse(ddltemplateList.SelectedValue) - 1;

this.template = templatesList.Items[templateID]["Template"].ToString();

}

templatesList = null;

this.FindRichTextControl(listFieldIterator);

base.OnPreRender(e);

}

private void FindRichTextControl(Control control)

{

foreach (Control childControl in control.Controls)

{

if (childControl.GetType().Equals(typeof(Microsoft.SharePoint.WebControls.RichTextField)))

{

((RichTextField)childControl).Value = this.template;

return;

}

this.FindRichTextControl(childControl);

}

}

private void FillTemplateDropDown()

{

if (ddltemplateList != null)

{

SPList templatesList = this.listFieldIterator.Web.Lists["Page Templates"];

ddltemplateList.DataSource = templatesList.Items;

ddltemplateList.DataTextField = "Title";

ddltemplateList.DataValueField = "ID";

ddltemplateList.DataBind();

}

}

}

}

  1. Compile and deploy to the GAC.
  2. Make changes in CreateWebPage.aspx
    1. Open the file "12\TEMPLATE\LAYOUTS\CreateWebPage.aspx" (a backup could maybe be handy…)
    2. Change the header to point to your assembly:

    <%@ Assembly Name="Mho.SharePoint.Trials, Version=1.0.0.0, Culture=neutral, PublicKeyToken=261ba54b9846991b"%>

      1. Set the correct class to inherit from

    <%@ Page Language="C#" Inherits="Mho.SharePoint.Trials.EnhancedCreateWebpage"…

      1. Add the name of the "ListFieldIterator" control you've used in the class code as its ID:

    <SharePoint:ListFieldIterator ID="listFieldIterator"…

      1. Add Dropdown:

    <asp:DropDownList ID="ddltemplateList" AutoPostBack="true" runat="server" />

    1. Recycle the application pool and try it out.

    Code Download:

    6 comments:

    Anonymous said...

    Hello Nanddeep,

    First let me say, this is a great post and it works beautifully!.

    Now, though, here is my question.
    How do you point the library to go to your custom createwebpage instead of the ms one.
    I looked all over into the advanced settings of the wiki pages list and I can't see how to make it point to my custom createwebpage.

    Out of desperation I even created a new feature mimicking the WebPageLibrary one from MS and made sure to replace all links to the CreateWebPage with my custom one; but I can't get it to go there.
    It seems that the Wiki Pages list only wants to go the the MS page...

    If that helps I am using MOSS 2007.
    Hopefully this is not a bad thing...

    Any input would greatly be appreciated.

    Thanks!

    CNB

    Nanddeep Nachan said...

    Hi CNB,

    Thanks for your comments.
    I have not created custom CreateWebPage.aspx, rather I am using the one provided by MS.
    Please have a look at Step 6 (b): Change the header to point to your assembly.
    I hope, this has cleared your doubt.

    Thanks & Regards,
    Nanddeep Nachan

    custom essays said...

    great post keep it up really informative

    che said...

    Hi Nanddeep,

    I have to add multiple web parts on the wiki page's CreateWebPage.aspx and wkpstd.aspx (wiki core) template file. Can you recommend me a path without needing to modify the sharepoint core files.

    And, what do you mean by 'Copy your HTML (only the part inside the body of course)'

    Sorry for the lame comments, I am a newbee at this :)

    Any help would be greatly appreciated!


    /Che

    Carl said...

    Hi Nandeep,

    Can you explain what you mean by creating a custom site definition where the Wiki Pages list point to _layouts/CreateWebPage.aspx. As, the createwebpage.aspx is specified in the Wiki feature section?

    many thanks!

    -Carl

    Locksmiths said...

    Is this the same way to create Wiki pages in SharePoint 2010? I need to create a template with certain web parts that will be used all across each of my Wiki pages.

    World Clock