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.

Possibly related posts

19 Responses to “Programmatically creating Wiki Pages”

  1. Andy Burns Says:

    As the RootFolder is just an SPFolder, could this be done for any folder? I.e. can you control what page is shown when someone enters a folder?

  2. Waldek Mastykarz Says:

    @Andy Burns: It seems logical indeed. I guess you would have to try just to know it for sure.

  3. @AaronMeis Says:

    Great Post Waldek, We recently began to use the Wiki feature in SharePoint. One question, where would you put the "ability" to create a set of pages? Feature? Powershell script? Thanx.

  4. Waldek Mastykarz Says:

    @AaronMeis: Thanks. You mean like prefilling a Wiki with default content? It all depends on who do you want it to do. For example the end users have no access to PowerShell so if you want them to create the Wiki prefilled with content you would have to wrap it in a Feature.

  5. Peter Varsanyi Says:

    Great post! Thanks.

  6. DM Gathoni Says:

    where to add this code?
    on which page to add this code?

  7. Waldek Mastykarz Says:

    @DM Gathoni: You can use it in a custom application running on the server, custom application page, Web Part, custom control, User Control: everywhere where you can use code.

  8. Janu Says:

    It is very nice post, it helped me lot, but can u please give me some code for Editing wiki page.

    Thanks
    janu

  9. Waldek Mastykarz Says:

    @Janu: the first snippet contains the code that you can use to edit the contents of a Wiki Page.

  10. Shahid Says:

    Waldek.. awesome stuff, can the same be applied/used in SharePoint 2010 Foundation Client Object Model ?

  11. Waldek Mastykarz Says:

    @Shahid: I haven't tried it yet, but it should be doable.

  12. Bloody Wikis in SharePoint 2007 » Andy Burns’ SharePoint Blog Says:

    [...] off, calling the SPListItem.Title for a Wiki pages causes an ArgumentException. (Waldek Mastykarz has written about this). Kind of makes sense – the wiki item doesn't have a title – but I'd prefer [...]

  13. Ankit Says:

    Hi,
    This is really a good article .
    I followed it but i got some error.
    we have own sharpoint server. i created wikki site on it .there is wikki page folder .
    if i want to access it using outside of applicaiton can the same code worked or i need webservices for it.

    on shrepoint server i installed .net and run above code

    we have sharpoint application which run in browser show code like

    http://compnayapp02:221/
    in that i created wikki sitenamed sharepointwikki

    http://compnayapp02:221/sharepointwikki

    SPSite site = new SPSite("http://compnayapp02:221/sharepointwikki");

    {
    SPWeb rootWeb = site.RootWeb;
    SPList wiki = rootWeb.Lists["MyWiki"];

    //here i got error on getting Mywiki folder .

    }

    // how to pass spsite name please tell me.
    i am trying to find solutions since many days but i am not able to get solution .
    please help me.

    i have only few days for finish my work.
    Regards,
    ankit.

  14. Waldek Mastykarz Says:

    @Ankit: Is MyWiki the Title or the Name of your Wiki List?

  15. ankit Says:

    Thank you very much for ur reply.
    yes its [wikki] folder name on wikki site.its default created when we create sharepoint wikki site and i want to put new wikki page in that folder.

    i want to ask u one more thing

    can we access existing site using like below code

    SPSite site = new SPSite("http://compnayapp02:221/sharepointwikki");

    {
    SPWeb rootWeb = site.RootWeb;
    SPList wiki = rootWeb.Lists["MyWiki"];

    //here i got error on getting Mywiki folder .

    }

  16. ankit Says:

    Hi,
    wikki is wikki folder name how can we get wikki list name to add the page on wikki sharepoint new created site .

    how can we get wikki lists name from sharepoint site ?

  17. ankit Says:

    hi,
    i need help in finding list of pages which have been changed and where it has been changed .
    please if you have idea provide me code for finding list of sharpoint wikki page ,what page has been changed and what changed had done in history .
    how to find it ?

  18. Waldek Mastykarz Says:

    @ankit: you could try using the SPList.GetChanges method, although I'm not sure it will provide you with the exact changes in the content.

  19. ankit Says:

    hi,
    i found the solution .

    we have to track collection,then List then item in which there is property modified and all so from there we can find .
    sharepoint is storing different changes in different folder for each page .

Leave a Reply

Security Code:

WP Theme & Icons by N.Design Studio
Entries RSS Comments RSS
Copyright © 2007 - 2010 Waldek Mastykarz

Creative Commons License