Showing posts with label SharePoint 2013. Show all posts
Showing posts with label SharePoint 2013. Show all posts

Friday, August 24, 2012

Programmatically creating Wiki Pages


The Windows SharePoint Services Object Model provides some methods for Wiki Pages Library Programmability. Using these methods, we can create new Pages in Wiki Pages Library and set a default page in the Wiki Pages Library. The following snippet is the sample for creating Wiki Pages in a Wiki Pages Library.
using (SPSite site = new SPSite("http://sharepoint"))
{
 SPWeb currentWeb= site.RootWeb;
 SPList spList = currentWeb.Lists["Site Pages"];
  SPFolder wikiRootFolder = spList.RootFolder;
 if (spList != null) {
 try {
  SPFile wikiPage = wikiRootFolder.Files.Add(String.Format("{0}/{1}", wikiRootFolder, "WikiPage.aspx"),                                           SPTemplateFileType.WikiPage);
   SPListItem wikiItem = wikiPage.Item;
   wikiItem[SPBuiltInFieldId.WikiField] = "Wiki Page Content "; 
 wikiItem.UpdateOverwriteVersion();
 wikiRootFolder.Update();
 }
}
}
For getting Wiki Page information, there are some suggestions from Waldek Mastykarz. I would Thank him for his great post. Please refer to the following link: http://blog.mastykarz.nl/programmatically-creating-wiki-pages/

Error occurred in deployment step 'Activate Features': Object reference not set to an instance of an object

Error occurred in deployment step 'Activate Features': Object reference not set to an instance of an object. This is a common Error that every SharePoint developer will do.

We cannot use SPContext.Current.Web in feature receivers. They can be run by the timer service or powershell which does not have access to the SPContext.

So We can create instances for out SPsite and SPWeb as mentioned below.

using (SPSite site = (SPSite)properties.Feature.Parent)
{
   using (SPWeb web = site.OpenWeb())
   {
            Our code
            ---------------------
            ---------------------
---------------------
                       
   }
}