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

















August 17th, 2009 at 5:09 pm
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?
August 17th, 2009 at 6:10 pm
@Andy Burns: It seems logical indeed. I guess you would have to try just to know it for sure.
September 21st, 2009 at 4:20 am
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.
September 21st, 2009 at 6:22 am
@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.
October 19th, 2009 at 12:11 pm
Great post! Thanks.
October 27th, 2009 at 2:07 pm
where to add this code?
on which page to add this code?
October 28th, 2009 at 8:43 am
@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.
December 22nd, 2009 at 8:59 am
It is very nice post, it helped me lot, but can u please give me some code for Editing wiki page.
Thanks
janu
December 23rd, 2009 at 10:53 am
@Janu: the first snippet contains the code that you can use to edit the contents of a Wiki Page.
March 11th, 2010 at 1:47 am
Waldek.. awesome stuff, can the same be applied/used in SharePoint 2010 Foundation Client Object Model ?
March 12th, 2010 at 7:12 am
@Shahid: I haven't tried it yet, but it should be doable.
April 20th, 2010 at 1:11 pm
[...] 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 [...]
May 31st, 2010 at 6:34 pm
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.
May 31st, 2010 at 7:02 pm
@Ankit: Is MyWiki the Title or the Name of your Wiki List?
June 3rd, 2010 at 8:04 pm
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 .
}
June 3rd, 2010 at 8:15 pm
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 ?
June 12th, 2010 at 5:01 pm
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 ?
June 13th, 2010 at 1:28 pm
@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.
June 14th, 2010 at 5:04 pm
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 .