Programmatically creating Wiki Pages


Recently I had to prepare a demo for one of our customers. Next to the regular things like provisioning sites and lists I had to provision a Wiki Pages Library with some demo pages in it. Although you might expect a Wiki Library to be a regular list it is not and programmatically creating Wiki Pages is slightly different than creating regular items.

As the name says Wiki Pages Library is a Library. If you look at its BaseType property, it says that a Wiki Pages Library is a Document Library. That means that creating a new Wiki Page means creating a new document rather than a new list item.

After doing some research I’ve found that the Windows SharePoint Services (WSS) team has provided us with some public methods to support Wiki programmability. Using these methods allows you to create a new Wiki Page within just a couple of lines of code:

using (SPSite site = new SPSite("http://sharepoint"))
{
  SPWeb rootWeb = site.RootWeb;
  SPList wiki = rootWeb.Lists["MyWiki"];
  SPFolder rootFolder = wiki.RootFolder;
  SPFile wikiPage = rootFolder.Files.Add(String.Format("{0}/{1}", rootFolder, "My Wiki Page.aspx"), SPTemplateFileType.WikiPage);
  SPListItem wikiItem = wikiPage.Item;
  wikiItem[SPBuiltInFieldId.WikiField] = "My Wiki Page with [[wiki link]]";
  wikiItem.UpdateOverwriteVersion();
}

And that’s basically it. After running the code above you will see your Wiki Page created in the Wiki Library:

Custom Wiki Page created programmatically 

A few things to keep in mind while working with Wiki’s in SharePoint

Beware of the Title

Regularly, while creating List Items programmatically you would provide a title for your Item. You could retrieve that Title using the SPListItem.Title property.

In contrary to a List Item a Wiki Page doesn’t have the Title field. It simply doesn’t exist in the Wiki Page Content Type. Trying to get the value of the SPListItem.Title property for a Wiki Page results in the following exception:

System.ArgumentException: Value does not fall within the expected range

The above exception shouldn’t be surprising as the Wiki Page Content Type doesn’t contain the Title field and all the SPListItem.Title property does is to retrieve the value of the Title field for the particular List Item.

You can make your own home

Have you every noticed the difference between a Wiki Library and a regular List/Document Library when you click on a link to it? By default a link to a regular List/Document Library takes you to the default view. However, if you click on link pointing to a Wiki Library what you see is the Wiki Welcome Page:

Default Wiki Page

It turns out that the Wiki Pages Library uses the value of the SPList.RootFolder.WelcomePage property to redirect you to the welcome page. While there is no interface available out-of-the-box to manage the value of that property, you can perfectly set your own value through the code:

using (SPSite site = new SPSite("sharepoint"))
{
  SPWeb rootWeb = site.RootWeb;
  SPList wiki = rootWeb.Lists["MyWiki"];
  SPFolder rootFolder = wiki.RootFolder;
  rootFolder.WelcomePage = "My Welcome Page.aspx";
  rootFolder.Update();
}

After running the above snippet your Wiki would display by default the My Welcome Page.aspx page instead of the default Home.aspx page.

The best part is that you can use the SPList.RootFolder.WelcomePage property on any type of list to set your own Welcome Page. You could even point it to a specific item if you wanted to:

using (SPSite site = new SPSite("sharepoint"))
{
  SPWeb rootWeb = site.RootWeb;
  SPList customList = rootWeb.Lists["MyList"];
  SPFolder rootFolder = customList.RootFolder;
  rootFolder.WelcomePage = "DispForm.aspx?ID=1";
  rootFolder.Update();
}

Running the above snippet would set the default page to the View Form with List Item 1 selected.

Technorati Tags: SharePoint,SharePoint 2007,WSS 3.0,MOSS 2007

Others found also helpful: