SharePoint Blogs / SharePoint University
SharePoint Blogs and SharePoint University - all in one place!
Need SharePoint Training? Attend a SharePoint Bootcamp!

Please delete cookies related to sharepointblogs.com and sharepointu.com to resolve login issues!

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 below paragraphs show the concept on how to achieve this functionality. Neither the code nor the configuration steps described are suitable for a production environment – they are just the proof-of-concept that it is possible. Adopt it freely!

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 Teplate

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…):

a.       Upload the style sheet to the appropriate location in the Styles Library

b.      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

Before I show the development steps this warning: 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. However, the scope of this instruction is only to show how it is supposed to work. Please keep this also in mind when looking at the code!

1.       Create a new class-library

2.       Make sure it will be signed

3.       Add two references:

a.       Microsoft.SharePoint.dll (12\ISAPI)

b.      Microsoft.SharePoint.ApplicationPages.dll (\12\CONFIG\BIN)

4.       Create your class inheriting from CreateNewWeb as shown in the code example. I guess the code is self-speaking:

using System;
using System.Web.UI;
using Microsoft.SharePoint.ApplicationPages;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint;

namespace Mho.SharePoint.Trials
{
    public class EnhancedCreateWebpage : CreateWebPage
    {
        protected ListFieldIterator listFieldIterator;
        protected string template;

        public EnhancedCreateWebpage() { }

        protected override void OnPreRender(EventArgs e)
        {
            if (!this.Page.IsPostBack)
            {
                // 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
                    this.template = templatesList.Items[0]["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);
            }
        }

    }
}

5.       Compile and deploy to the GAC

6.       <Warning! See above paragraph – this is not supported!>

a.       Open the file “12\TEMPLATE\LAYOUTS\CreateWebPage.aspx” (a backup could maybe be handy…)

b.      Change the header to point to your assembly:
<%@ Assembly Name="Mho.SharePoint.Trials, Version=1.0.0.0, Culture=neutral, PublicKeyToken=261ba54b9846991b"%>

c.       Set the correct class to inherit from
<%@ Page Language="C#" Inherits="Mho.SharePoint.Trials.EnhancedCreateWebpage"…

d.      Add the name of the “ListFieldIterator” control you’ve used in the class code as its ID:
<SharePoint:ListFieldIterator ID="listFieldIterator"

7.       Recycle the application pool and try it out.

  

 


Posted 09-27-2007 10:13 AM by mhofer1976

Comments

sezai wrote re: SharePoint Wiki: Create pages based on a template
on 09-27-2007 7:31 AM

When you extend and override SharePoint classes to add your own code you can do some pretty powerful stuff.

I originally fealt restricted developing in SharePoint, but I soon realised that you can inherit from classes and then switch references to make use of your custom class, the possibilities are endless and you are free to customise with code a great deal more.

Great post !

Links (9/27/2007) « Steve Pietrek’s SharePoint Stuff wrote Links (9/27/2007) &laquo; Steve Pietrek&#8217;s SharePoint Stuff
on 09-27-2007 8:00 PM

Pingback from  Links (9/27/2007) &laquo; Steve Pietrek&#8217;s SharePoint Stuff

Blog del CIIN wrote WSS 3.0 & MOSS: Recopilación de enlaces interesantes (VIII)
on 10-08-2007 1:56 AM

Después de una semana un tanto ajetreada en el CII, noi podíamos faltar al tradicional recopilatorio

lutz.heil@pentasys.de wrote re: SharePoint Wiki: Create pages based on a template
on 11-15-2007 5:15 PM

Great idea but the function throws following Exception:

Exception Type: System.Web.HttpException  Exception Message: Die Datei /_layouts/_controltemplates/InputFormSection.ascx ist nicht vorhanden.

Can you tell why and how to fix it?

Tnx in advance

SharePoint, SharePoint and stuff wrote SharePoint Kaffeetasse 33
on 11-21-2007 2:22 AM

Tipps Setting the Global Navigation Use Conditional Formatting in Data Views based on User Security Permissions

judy wrote re: SharePoint Wiki: Create pages based on a template
on 12-12-2007 12:58 PM

This is an interesting site.

useful sharepoint links « SharePoint, Wiki, Create, Column, Data, Business, Free, Catalog « konfabulieren wrote useful sharepoint links &laquo; SharePoint, Wiki, Create, Column, Data, Business, Free, Catalog &laquo; konfabulieren
on 12-22-2007 12:43 AM

Pingback from  useful sharepoint links &laquo; SharePoint, Wiki, Create, Column, Data, Business, Free, Catalog &laquo; konfabulieren

Karen wrote re: SharePoint Wiki: Create pages based on a template
on 01-11-2008 10:54 AM

We have created a 3D Virtual Mall that allows business owner's to create their very own mall based web pages to sell their products online. It is a cost efficient method of bringing together everything retail. Our shopping cart templates are very user friendly. In virtually minutes a storefront can be open for business, displaying products and or services for the shoppers to purchase. Shoppers can enjoy going to the mall and never leave the chair. We invite everyone to check it out. http://www.uwalkthemall.com

Tasteful criticism is welcome.

wsspectacular wrote Wiki in the knees
on 01-12-2008 2:01 AM

So, after hyping Sharepoint Wiki to a customer, I went about trying to put one together... only to find

SHAREPOINTBlogs.com Mirror wrote Wiki in the knees
on 01-12-2008 2:13 AM

So, after hyping Sharepoint Wiki to a customer, I went about trying to put one together... only to find

Semi wrote re: SharePoint Wiki: Create pages based on a template
on 06-18-2008 8:43 PM

I keep getting an erro after deploying this. could you give a full example of the SharePoint:ListFieldIterator ID="listFieldIterator" that control has a few options and I am not sure if I am using it right. Once I build the class and deploy it and when I click on the new wiki page button I get the following error

Object reference not set to an instance of an object.   at WC.WikiWicked.WikiWicked.OnPreRender(EventArgs e)

  at System.Web.UI.Control.PreRenderRecursiveInternal()

  at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)  

Sorry I have never written code for SP using the listFieldIterator control.

Nikki wrote re: SharePoint Wiki: Create pages based on a template
on 06-20-2008 9:31 AM

Thank you for the instructions, it works great, however, I was wondering if you could help me by detailing a little bit more how to change the Wiki Pages list so it doesn’t point to _layouts/CreateWebPage.aspx .

I created a custom site definition but I am having trouble located where to make the change to my own page...

Thanks!

Kanwal Khipple wrote re: SharePoint Wiki: Create pages based on a template
on 03-18-2009 8:14 AM

Good job on the article.   You are now part of SharePoint Buzz

Edwin wrote re: SharePoint Wiki: Create pages based on a template
on 05-23-2009 7:05 AM

I am looking for Wiki template extensions. Trying to avoid customizations. Any good ones? Thanks

Shiva Kumar wrote re: SharePoint Wiki: Create pages based on a template
on 05-25-2009 3:44 AM

Interesting post. But, as mentioned not suitable for prod environmemt.

Add a Comment

(required)  
(optional)
(required)  
Remember Me?
Need SharePoint Training? Attend a SharePoint Bootcamp!
Posts (c) their respective authors. Everything else (c) 2009 SharePoint Experts, Inc.