Introduction
As we all know wiki page is one of the concept in SharePoint to add content at run time. Let's see how to add this wiki page programmatically.
Wiki Page
At first, Create new SharePoint Project in Visual studio with one feature file. Then, Create an Event Receiver for feature. Paste the below code in FeatureActivated method.
using (SPSite site = new SPSite(webUrl))
{
using (SPWeb web = site.OpenWeb())
{
SPList list = web.Lists["Site Pages"];
web.AllowUnsafeUpdates = true;
SPFolder rootFolder = list.RootFolder;
SPFile wikiPage = SPUtility.CreateNewWikiPage(list, String.Format("{0}/{1}", rootFolder.ServerRelativeUrl, "your page name"));
SPListItem wikiItem = wikiPage.Item;
wikiItem["WikiField"] = "content";
wikiItem.Update();
web.AllowUnsafeUpdates = false;
}
}
{
using (SPWeb web = site.OpenWeb())
{
SPList list = web.Lists["Site Pages"];
web.AllowUnsafeUpdates = true;
SPFolder rootFolder = list.RootFolder;
SPFile wikiPage = SPUtility.CreateNewWikiPage(list, String.Format("{0}/{1}", rootFolder.ServerRelativeUrl, "your page name"));
SPListItem wikiItem = wikiPage.Item;
wikiItem["WikiField"] = "content";
wikiItem.Update();
web.AllowUnsafeUpdates = false;
}
}
Just deploy the solution in your site and activate the feature to see created Wiki page.
Happy Coding.. !!!!
Nice Article....
ReplyDelete