Question

Like a lot of us I'm green when it comes to SharePoint development. I've been tasked with moving MediaWiki content over to a SharePoint Enterprise wiki. At this time I do not have access to the server the SP is on and have to do this all client side. I can successfully GET information or even change a publishing page's content but I can't create a new wiki page with Microsoft.Sharepoint.Client (I tried via ListItem etc).

 ClientContext context = new ClientContext("http://<site>/");
 ListItem LItem = pagesList.GetItemById(8);
 LItem["PublishingPageContent"] = "This is my new value!!";
 LItem.Update();

 context.ExecuteQuery();  

The above code worked great! Then I tried:

  ListItemCreationInformation newPage = new ListItemCreationInformation();
  ListItem newpageitem = pagesList.AddItem(newPage);
  newpageitem["Title"] = "OtherCompany";
  newpageitem["PublishingPageContent"] = "Info for Other Company";
  newpageitem.Update();

  context.Load(newpageitem);

this didn't work so great. :-(

Does anyone know if this is even possible to do this client side?

Was it helpful?

Solution

How to create a Wiki page via CSOM

/// <summary>
/// Create Wiki page
/// </summary>
/// <param name="pagesList">Wiki Pages list</param>
/// <param name="pageName">Wiki page name</param>
/// <param name="pageContent"></param>
public static ListItem CreateWikiPage(List pagesList, string pageName, string pageContent)
{
    const string templateRedirectionPageMarkup = "<%@ Page Inherits=\"Microsoft.SharePoint.Publishing.TemplateRedirectionPage,Microsoft.SharePoint.Publishing,Version=14.0.0.0,Culture=neutral,PublicKeyToken=71e9bce111e9429c\" %> <%@ Reference VirtualPath=\"~TemplatePageUrl\" %> <%@ Reference VirtualPath=\"~masterurl/custom.master\" %>";
    var pageInfo = new FileCreationInformation
      {
                Url = pageName,
                Content = Encoding.UTF8.GetBytes(templateRedirectionPageMarkup),
                Overwrite = true
      };

    var ctx = pagesList.Context;
    var wikiFile = pagesList.RootFolder.Files.Add(pageInfo);
    ctx.Load(wikiFile);
    ctx.ExecuteQuery();

    var wikiItem = wikiFile.ListItemAllFields;
    wikiItem["PublishingPageContent"] = pageContent;
    wikiItem["PublishingPageLayout"] = "/_catalogs/masterpage/EnterpriseWiki.aspx, Basic Page";
    wikiItem.Update();
    ctx.ExecuteQuery();
    return wikiItem;
}

Usage

using (var ctx = new ClientContext(url))
{ 
    var pagesList = ctx.Web.Lists.GetByTitle("Pages"); 
    var wikiItem = CreateWikiPage(pagesList,"SharePoint Overview.aspx","Welcome to the SharePoint!");
} 
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top