This question already has an answer here:

I need to create a power shell script for creating wiki pages in bulk from a XML file. Please help.


marked as duplicate by Eric Alexander Sep 17 '15 at 13:10

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • Please provide a bit more information, e.g. the structure of the XML etc. – Paul Strupeikis Sep 17 '15 at 11:51

Yes you can do it with both powershell and CSOM


  • Powershell


    function New-SPPage{
    The New-SPPage function creates one or more Publishing Pages in a SharePoint Publishing Site.
    The New-SPPage function uses the PublishingSite and PublishingWeb objects to call the Add() method for provisioning of SharePoint Publishing Pages.
    C:\PS>New-SPPage -SiteUrl http://intranet `
    -PageTitle "My Page" `
    -PageUrl MyPage.aspx `
    -PageContent "This is my text." `
    -PageLayout "Article Page" `
    -CheckIn -Publish   
    This example creates a single page in the http://intranet site.
    C:\PS>New-SPPage -CreateFromXml -XmlInput "C:\Pages.xml" -Checkin -Publish
    This example creates pages based on an xml file with the following schema:
<?xml version="1.0" encoding="utf-8"?>
        <PageTitle>Page 1</PageTitle>
        This is some text.
        This is another line of text.
        <PageLayout>Article Page</PageLayout>
    Name: New-SPPage
    Author: Ryan Dennis
    Last Edit: 7/25/2011
    Keywords: New-SPPage
#Requires -Version 2.0
#Region CreateFromXml
if ($CreateFromXml) {
# Read in list of pages from XML
[xml]$pagesXML = Get-Content $($XmlInput)
if ($pagesXML -eq $null) { return }
# Get publishing web
$site = New-Object Microsoft.SharePoint.SPSite($SiteUrl)
$psite = New-Object Microsoft.SharePoint.Publishing.PublishingSite($site)
$web = $site.OpenWeb()
$pWeb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($web)
# Loop through each page node to extract filename
$pagesXML.Pages.Page | ForEach-Object {
    $PageTitle = [string]$_.PageTitle
    $PageUrl = [string]$_.PageUrl
    $PageLayout = [string]$_.PageLayout
    $PageContent = [string]$_.PageContent
    $ctype = $psite.ContentTypes[$PageLayout]
    $layouts = $psite.GetPageLayouts($ctype, $true)
    $layout = $layouts[0]
    Write-Host "Creating $($PageTitle)"

    # Create blank page
    $pages = $pWeb.GetPublishingPages($pWeb)
    $page = $pages.Add($PageUrl, $Layout)
    #$newPage = $pWeb.AddPublishingPage($PageUrl,$PageLayout)

    # Update the filename to the one specified in the XML
    $item = $page.ListItem
    $item["Title"] = $PageTitle;
    $item["Page Content"] = $PageContent;
    # Check-in and publish page
    if ($CheckIn){$item.File.CheckIn("")}
    if ($Publish){$item.File.Publish("");}
} #End ForEach Loop
# Dispose of the web
} #End CreateFromXml
#EndRegion CreateFromXml
#Region CreateSinglePage
else {
Start-SPAssignment -Global
$site = New-Object Microsoft.SharePoint.SPSite($SiteUrl)
$psite = New-Object Microsoft.SharePoint.Publishing.PublishingSite($site)
$ctype = $psite.ContentTypes[$PageLayout]
$layouts = $psite.GetPageLayouts($ctype, $true)
$layout = $layouts[0]
$web = $site.OpenWeb(); #Site.Rootweb
$pweb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($web)
$pages = $pweb.GetPublishingPages($pweb)
$page = $pages.Add($PageUrl, $layout)
$item = $page.ListItem
Write-Host "Creating $($PageTitle)"
$item["Title"] = $PageTitle;
$item["Page Content"] = $PageContent;
if ($CheckIn){$item.File.CheckIn("")}
if ($Publish){$item.File.Publish("")}
Stop-SPAssignment -Global
} #End single page from else
} #End function



Add-Type -Path 'C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\ISAPI\Microsoft.SharePoint.Client.dll'
Add-Type -Path 'C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\ISAPI\Microsoft.SharePoint.Client.Runtime.dll'

Function Invoke-LoadMethod() {
   $ClientObject = $(throw "Please provide an Client Object instance on which to invoke the generic method")
   $ctx = $ClientObject.Context
   $load = [Microsoft.SharePoint.Client.ClientContext].GetMethod("Load") 
   $type = $ClientObject.GetType()
   $clientObjectLoad = $load.MakeGenericMethod($type) 

function CreateWikiPage()
            $templateRedirectionPageMarkup = '<%@ Page Inherits="Microsoft.SharePoint.Publishing.TemplateRedirectionPage,Microsoft.SharePoint.Publishing,Version=,Culture=neutral,PublicKeyToken=71e9bce111e9429c" %> <%@ Reference VirtualPath="~TemplatePageUrl" %> <%@ Reference VirtualPath="~masterurl/custom.master" %>';

            $ctx = New-Object Microsoft.SharePoint.Client.ClientContext($webUrl)
            $ctx.Credentials = $credentials

            $file = New-Object Microsoft.SharePoint.Client.FileCreationInformation
            $file.Url = $pageUrl
            $file.Content = [System.Text.Encoding]::UTF8.GetBytes($templateRedirectionPageMarkup)
            $file.Overwrite = $true

            $wikiPages = $ctx.Web.Lists.GetByTitle("Pages")
            $wikiFile = $wikiPages.RootFolder.Files.Add($file)
            Invoke-LoadMethod -ClientObject $wikiFile

            $wikiPage = $wikiFile.ListItemAllFields
            $wikiPage["PublishingPageContent"] = $pageContent
            $wikiPage["PublishingPageLayout"] = "/_catalogs/masterpage/EnterpriseWiki.aspx, Basic Page"


$filePath = "C:\Temp\PagesStructure.xml" 
$UserName = "username"
$webUrl = "https://contoso.intranet.com/kb"
$Password = "password"
$credentials = New-Object System.Net.NetworkCredential($UserName,$Password,$Domain)

[xml]$pagesStructure = Get-Content $filePath
$pagesStructure.Pages.Page | ForEach-Object {
    $PageTitle = [string]$_.PageTitle
    $PageUrl = [string]$_.PageUrl
    $PageContent = [string]$_.PageContent

    Write-Host "Creating $($PageTitle)"
    CreateWikiPage $webUrl $credentials $pageUrl $pageContent

Reference:Using Powershell to create wiki pages from an xml file

  • Thanks for the help. I am able to create the pages with custom page layout but i am not able to create an Enterprise Wiki page. – Amit Khandelwal Sep 18 '15 at 8:36

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