1

Is it possible to create a new wiki page using the client object model and give it some content ("<h1>Welcome to the SharePoint!</h1>")?

I'm using VB.Net

1

How to create Wiki page using SharePoint CSOM

The example below demonstrates how to create wiki page via CSOM (VB.NET version):

Sub CreateWikiPage(context As ClientContext, pageName As String, pageContent As String)

    Const templateRedirectionPageMarkup As String = "<%@ 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"" %>"

    Dim wikiPages = context.Web.Lists.GetByTitle("Pages")
    context.Load(wikiPages)
    context.ExecuteQuery()

    Dim fileInfo = New FileCreationInformation()
    fileInfo.Url = pageName
    fileInfo.Content = Encoding.UTF8.GetBytes(templateRedirectionPageMarkup)
    fileInfo.Overwrite = True

    Dim wikiFile = wikiPages.RootFolder.Files.Add(fileInfo)
    context.Load(wikiFile)
    context.ExecuteQuery()

    Dim wikiPage = wikiFile.ListItemAllFields
    wikiPage("PublishingPageContent") = pageContent
    wikiPage("PublishingPageLayout") = "/_catalogs/masterpage/EnterpriseWiki.aspx, Basic Page"
    wikiPage.Update()
    context.ExecuteQuery()

End Sub

Note: the provided method is intended for creating enterprise wiki pages

Usage

Using ctx = New ClientContext(siteUrl)      
   CreateWikiPage(ctx, "Greetings.aspx", "<H1>Welcome to the SharePoint!</H1>")
End Using

References

Some tips and tricks of using SharePoint Client Object Model in PowerShell. Part 1


How to create a page in Wiki Page Library using CSOM

For SharePoint 2013

Use Utility.CreateWikiPageInContextWeb method to create a page in Wiki Page Library:

Sub CreateWikiPageInLibrary(context As ClientContext, wikiPageLibraryTitle As String, pageName As String, pageContent As String)

    Dim wikiLibrary = context.Web.Lists.GetByTitle(wikiPageLibraryTitle)
    context.Load(wikiLibrary.RootFolder)
    context.ExecuteQuery()

    Dim wikiPageInfo As New WikiPageCreationInformation
    wikiPageInfo.WikiHtmlContent = pageContent
    wikiPageInfo.ServerRelativeUrl = String.Format("{0}/{1}", wikiLibrary.RootFolder.ServerRelativeUrl, pageName)
    Dim wikiFile = Microsoft.SharePoint.Client.Utilities.Utility.CreateWikiPageInContextWeb(context, wikiPageInfo)
    context.ExecuteQuery()

End Sub

For SharePoint 2010

Sub CreateWikiPageInLibrary(context As ClientContext, wikiPageLibraryTitle As String, pageName As String, pageContent As String)

    Dim wikiLibrary = context.Web.Lists.GetByTitle(wikiPageLibraryTitle)
    context.Load(wikiLibrary.RootFolder)
    context.ExecuteQuery()


    Dim pageUrl = String.Format("{0}/{1}", wikiLibrary.RootFolder.ServerRelativeUrl, pageName)
    Dim wikiFile = wikiLibrary.RootFolder.Files.AddTemplateFile(pageUrl, TemplateFileType.WikiPage)
    context.Load(wikiFile)
    context.ExecuteQuery()

    Dim wikiPage = wikiFile.ListItemAllFields
    wikiPage("WikiField") = pageContent
    wikiPage.Update()
    context.ExecuteQuery()

End Sub
  • I got an error: "Column 'PublishingPageLayout' does not exist. It may have been deleted by another user" when executing the last line – maartenvdv Oct 2 '14 at 14:04
  • Hmm.. are you creating a wiki page under Wiki site? – Vadim Gremyachev Oct 2 '14 at 14:09
  • Please verify your Pages library whether it contains ootb PublishingPageLayout column – Vadim Gremyachev Oct 2 '14 at 14:10
  • the page library only contains Wiki Content, Created By, Modified By and Checked Out To. The new wiki is created in the list but i can't open it – maartenvdv Oct 2 '14 at 14:13
  • 1
    thx, your help is really appreciated! It worked! I accepted your answer but I need one more reputation to upvote it :p. – maartenvdv Oct 3 '14 at 7:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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