Use SharePoint API to Create Wiki Page

I had the task of auto generating hundreds of Wiki pages (basically a wiki of commonly used terms for my client's line of business).  The data was read from a Tab delimited file and in order to create Wiki pages, we have to do a few things differently.  Below is the .NET / SharePoint code that can be used to create a new Wiki page.

string
fileName = "TestWiki";
string body = "This is the body of the Wiki. Can contain <b>HTML</b>";

SPSite site = new SPSite("http://localhost");
SPWeb web = site.OpenWeb();
SPList list = web.Lists["Wiki"];

string serverRelativeUrl = web.ServerRelativeUrl + list.Title + "/" +fileName+ ".aspx";

SPListItem item = list.RootFolder.Files.Add(serverRelativeUrl, SPTemplateFileType.WikiPage).Item;
item[
"WikiField"] = body;
item.UpdateOverwriteVersion();

web.Dispose();
site.Dispose();

Hope this helps to save somebody some time.

Eli

Leave a Reply