banner



How To Create A Wiki Page In Sharepoint

Home » SharePoint » Creating Wiki Pages Programmatically In SharePoint

I still remember battling with different methods of creating wiki/publishing pages within an enterprise wiki site using code in SharePoint 2010. While the issues I encountered were almost frustrating at the time, the overall learning experience was great. So for developers like me who might be tasked with similar problems (and of course for my personal reference), I have put together this article to describe what works and what does not. I have tested the final code and found it to work for both a publishing site as well as a team site with the publishing feature activated.

Before proceeding, it is important to note that if you plan to create wiki pages on any site, the publishing feature must first be activated for the site. If your site is a publishing site, the publishing feature will be activated by default. However, if you are using a team site, you may have to activate this manually. Here's how to activate the publishing feature:

Go to the top level site of the site collection and choose Site Actions > Site Settings. On the site settings page in the site collection administration column choose Site Collection Features. Activate SharePoint Server Publishing Infrastructure Feature. Go back to the Site Settings page and choose Manage site features in the Site Actions column. Activate the SharePoint server Publishing Feature. You can now activate publishing on any site in this site collection. For other site collections repeat the process.

Ok, now back to actually creating the wiki pages in SharePoint 2010.

What DOES NOT Work

Here's a likely familiar piece of code. But it did not work for me:

using (SPSite site = new SPSite(SPContext.Current.Web.Url)) {         SPWeb rootWeb = site.RootWeb;         rootWeb.AllowUnsafeUpdates = true;         SPList wiki = rootWeb.Lists["Pages"];         SPFolder rootFolder = wiki.RootFolder;         SPFile wikiPage = rootFolder.Files.Add(String.Format("{0}/{1}", rootFolder.ServerRelativeUrl, "MyWikiPage.aspx"), SPTemplateFileType.WikiPage);         SPListItem wikiItem = wikiPage.Item;         wikiItem["PublishingPageContent"] = "my demo content";         wikiItem.UpdateOverwriteVersion();         rootWeb.AllowUnsafeUpdates = false; }

I tried the above code on an enterprise wiki site. The wiki page gets created but the problem is that the created page is not editable and the demo content is not inserted. When opened in edit mode, no content space is available and edit options are grayed out. I'm not sure if this is a Microsoft bug or not but changing this line:

wikiItem [ "PublishingPageContent" ] = "my demo content" ;

to this:

wikiItem [ SPBuiltInFieldId . WikiField ] = "my demo content" ;

does not work either. It gives an invalid field error.

And changing this line:

SPFile wikiPage = rootFolder . Files . Add ( String . Format ( "{0}/{1}" , rootFolder . ServerRelativeUrl , "MyWikiPage.aspx" ) , SPTemplateFileType . WikiPage ) ;

to this:

SPFile wikiPage = SPUtility . CreateNewWikiPage ( wiki , String . Format ( "{0}/{1}" , rootFolder . ServerRelativeUrl , "MyWikiPage.aspx" ) ) ;

produces exactly the same results.

What Works For Creating Wiki Pages Programmatically!

The solution that worked for me was to create a new publishing page and update content type properties accordingly.

Here goes:

using (SPSite site = new SPSite(SPContext.Current.Web.Url)) {     using (SPWeb rootWeb = site.RootWeb)     {         SPWebCollection subsites = rootWeb.GetSubwebsForCurrentUser();         SPWeb mySubWeb = subsites["mySubSiteName"]; // Use the code structure in the above previous code if you're not targeting a specific subsite         mySubWeb.AllowUnsafeUpdates = true;         SPList wiki = mySubWeb.Lists["Pages"];         String url = wiki.RootFolder.ServerRelativeUrl.ToString();         PublishingSite pubSite = new PublishingSite(mySubWeb.Site);         string pageLayoutName = "CustomEnterpriseWiki.aspx"; // I had a custom page layout. Default is EnterpriseWiki.aspx         string layoutURL = mySubWeb.Url + "/_catalogs/masterpage/" + pageLayoutName;         PageLayout layout = pubSite.PageLayouts[layoutURL];         PublishingWeb publishingWeb = PublishingWeb.GetPublishingWeb(mySubWeb);         PublishingPage newWikiPage;         string myDateTime = DateTime.Now.ToString("yyyyMMddHHmmss");         string myWikiPage = myDateTime + ".aspx"; // Of course, you can just use a regular page name         newWikiPage = publishingWeb.GetPublishingPages().Add(myWikiPage, layout);         newWikiPage.Title = "";         newWikiPage.Update();         //SPUtility.Redirect(String.Format("{0}/{1}", url, myWikiPage), SPRedirectFlags.Default, HttpContext.Current); // This will open the page created page normally         Page.Response.Redirect(string.Format("{0}/{1}?ControlMode={2}&DisplayMode={3}", url, myWikiPage, "Edit", "Design"), true); // This will open the created page in edit mode         mySubWeb.AllowUnsafeUpdates = false;     } }

You can ignore the extra bells and whistles like targeting a specific sub site, naming the page with a time stamp and opening in edit mode.

This was written for my personal reference too!

Found this article valuable? Want to show your appreciation? Here are some options:

  1. Spread the word! Use these buttons to share this link on your favorite social media sites.
  2. Sign up to join my audience and receive email notifications when I publish new content.
  3. Contribute by adding a comment using the comments section below.
  4. Follow me on Twitter, LinkedIn, and Facebook.

I am a Toronto-based Software Engineer. I run this website as part hobby and part business.

To share your thoughts or get help with any of my posts, please drop a comment at the appropriate link.

You can contact me using the form on this page. I'm also on Twitter, LinkedIn, and Facebook.

Reader Interactions

How To Create A Wiki Page In Sharepoint

Source: https://ehikioya.com/creating-wiki-pages-programmatically/

Posted by: montgomerycourer1950.blogspot.com

Related Posts

0 Response to "How To Create A Wiki Page In Sharepoint"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel