Create wiki page programatically

19/06/2015 11:11

This example creates wiki page on site collection created from Enterprise wiki template


Add reference to Microsoft.SharePoint.Publishing


SPWeb web = SPContext.Current.Web;

string tit = "My code article 3";
string articleTitle = string.Format("{0}.aspx", tit);

PublishingWeb pWeb = PublishingWeb.GetPublishingWeb(web);

SPQuery query = new SPQuery();
query.Query = string.Format("<Where><Eq><FieldRef Name='FileLeafRef' /><Value Type='Text'>{0}</Value></Eq></Where>", articleTitle);
query.RowLimit = 1;

PublishingPageCollection pages = pWeb.GetPublishingPages(query);
if (pages.Count == 0)
{
 PublishingPage page = pWeb.AddPublishingPage(articleTitle, pWeb.DefaultPageLayout); //EnterpriseWiki.aspx is default page layout for wiki site collection
 page.Update();
 
 page.ListItem["PublishingPageContent"] = "<h1>My content</h1>And more.";
 page.Update();

 }