Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I'm new to SharePoint. I'm trying to programmatically create a wiki page within the Pages library of an Enterprise Wiki site in SharePoint 2010. Here is my code:

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;
}

The 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 greyed out.

I have also tried setting the default content like this:

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

But that gives an invalid field error.

I have also tried creating the page with this line of code instead:

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

But the result is exactly the same.

I have confirmed that "SharePoint Server Publishing" feature is turned on for the site and "SharePoint Server Publishing Infrastructure" feature is turned on for the site collection.

Please help.

share|improve this question

2 Answers 2

up vote 0 down vote accepted

Targeting the Pages library in a Enterprise Wiki would not work using the regular Wiki manipulation routines as in the case of Site Pages because this is based on Content Types.

Basically your Pages library is having Enterprise Wiki Page as the default Content type and the easiest way to approach it is to rely on the creating a new Publishing Page instead and update Content type properties accordingly to each of its field types.

See here for an example: http://sharepoint2010mind.blogspot.ch/2012/06/add-publishing-page-to-sharepoint-site.html

share|improve this answer
    
Thanks a lot Marius. That solution worked like a charm. –  Zzz Zzz Oct 30 '12 at 18:09

From all your trials its very much clear that you have already tried all possible approaches. Can you try to use SitePages instead of Pages library. I am sure this will work. Also can you check whether publishing feature was enabled at Site level as well.

share|improve this answer
    
It did stated that he is using "Enteprise Wiki" and Pages library, which is like a Publishing Site. Using Site Pages would not be available in the Enterprise Wiki sites, unless manually created! –  C. Marius - MVP Oct 30 '12 at 17:07

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.