How To: Modify Standard (non enterprise) SharePoint 2010 WIKI Page Layout

In SharePoint 2010 WIKI page infrastructure, you get to modify the WIKI page in a standard free-text format.

The WIKI page is a list item in the WIKI page library. As every list item, WIKI pages can have custom fields, both content type inherited and specific library fields.

When editing WIKI pages, you actually need to switch to the List Item view of the page to modify its properties. Also, those properties don’t render anywhere on your WIKI page.

SharePoint 2010 WIKI Page

SharePoint 2010 WIKI Page

Above you see two custom fields I have in a library and how those render in the WIKI list item properties view, not on the WIKI page.

If you’re not using enterprise WIKI, there is no easy way for you to render those elements on your WIKI page to allow users modify the properties right on the page or even render their values in read only mode. In this post, I’d like to show you how that can be achieved using a custom Visual Studio solution.

We’re going to be deploying a farm solution since this customization doesn’t apply to the cloud hosted environments.

  1. Create new SharePoint 2010 project in Visual Studio, set it to deploy as a farm solution.
  2. Right click on the project to add a new delegate control: New Item | User Control. I called mine WikiDelegate. Ensure when you create the control – it’s placed in ControlTemplates/[ProjectName], in my case ControlTemplates/SharePointProject1.

3. Right click on the project in solution explorer again to add a new item of type EmptyElement. I called mine WIKIDelegate. This element will link our delegate control to the master page.

4. Add the following content to the WikiDelegate.ascx:

<SharePoint:ListFieldIterator

ExcludeFields="WikiField;#FileLeafRef" ControlMode="Display" runat="server"/>

 

Replace or merge the content of WikiDelegate.ascx.cs with the following:This is actually out-of-the-box control which recognizes fields available to the list items and automatically renders corresponding controls, very handy piece, but that’s not all.

  1. using System;
    
    using System.Web.UI;
    
    using System.Web.UI.WebControls;
    
    using System.Web.UI.WebControls.WebParts;
    
    using Microsoft.SharePoint;
    
    namespace SharePointProject1.ControlTemplates.SharePointProject1
    
    {
    
    public partial class WikiDelegate : UserControl
    
    {
    
    Control controlToFind = null;
    
    protected override void OnInit(EventArgs e)
    
    {
    
    // ensure we're on the WIKI page and not some other page,
    
    //after all our delegate is attached to every page which uses the masterpage
    
    if (SPContext.Current.ListItem.ContentType.Name.ToLowerInvariant().Contains("wiki page"))
    
    {
    
    // find the wiki field
    
    foreach (Control control in this.Page.Controls)
    
    {
    
    FindControl(control, "WikiField");
    
    }
    
    Control wikiParent = controlToFind.Parent;
    
    int wikiFieldPositionIndex = 0;
    
    // get the position of the wiki field, the content of the wiki page
    
    foreach (Control control in wikiParent.Controls)
    
    {
    
    wikiFieldPositionIndex++;
    
    if (control.ID == "WikiField")
    
    {
    
    break;
    
    }
    
    }
    
    // get a old of our iterator control, since we need to reposition where it'll be rendered
    
    foreach (Control control in this.Page.Controls)
    
    {
    
    FindControl(control, "ListFieldIterator1");
    
    }
    
    Control fieldIteratorControl = this.ListFieldIterator1;
    
    // remove the iterator from the old location
    
    wikiParent.Controls.Remove(controlToFind);
    
    // add the iterator right before the wiki content
    
    wikiParent.Controls.AddAt(wikiFieldPositionIndex-1, fieldIteratorControl);
    
    // if we're the edit mode, the page posts back and we switch the control rendering behavior
    
    // to allow user to edit fields
    
    if (Page.IsPostBack)
    
    {
    
    this.ListFieldIterator1.ControlMode = Microsoft.SharePoint.WebControls.SPControlMode.Edit;
    
    }
    
    }
    
    base.OnInit(e);
    
    }
    
    // this is a helper function to find controls within the page
    
    protected void FindControl(Control container, string ID)
    
    {
    
    if (container.HasControls())
    
    {
    
    foreach (Control child in container.Controls)
    
    {
    
    if (child.ID == ID)
    
    {
    
    controlToFind = child;
    
    }
    
    else
    
    {
    
    FindControl(child, ID);
    
    }
    
    }
    
    }
    
    else
    
    {
    
    if (container.ID == ID)
    
    {
    
    controlToFind = container;
    
    }
    
    }
    
    }
    
    }
    
    }

     

    Replace or merge the content with the following:The delegate control is ready, now let’s register it. Open the Elements.xml within WIKIDelegate Element we created earlier.

 

<?xml version="1.0" encoding="utf-8"?>

<Elements xmlns="http://schemas.microsoft.com/sharepoint/">

<Control Sequence="90" ControlSrc="~/_ControlTemplates/SharePointProject1/WikiDelegate.ascx" />

</Elements>

 

That’s it, build and deploy the solution and navigate to the wiki page (team site home page for example).Above, make sure you specify the path to your ASCX control and it’s name if you used a different name.

Add some custom fields to your WIKI library and you should see them being rendered on the wiki page, looking something like this:

SharePoint WIKI library

SharePoint WIKI library

SharePoint WIKI library fields

SharePoint WIKI library fields

If this post is something you need to do in a branding scenario, check out my SP2010 branding book here. You’ll find many similar examples like this one here.
Enjoy!

If this post is something you need to do in a branding scenario, check out my SP2010 branding book here. You’ll find many similar examples like this one here.

Enjoy!

Share this on...

Rate this Post:

Share:

Topics:

General

Tags: