-1

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.

Thanks

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
0

Yes you can do it with both powershell and CSOM

**

  • Powershell

**

    function New-SPPage{
<#
.Synopsis
    The New-SPPage function creates one or more Publishing Pages in a SharePoint Publishing Site.
.Description
    The New-SPPage function uses the PublishingSite and PublishingWeb objects to call the Add() method for provisioning of SharePoint Publishing Pages.
.Example
    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.
.Example
    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"?>
<Pages>
  <Page>
        <PageTitle>Page 1</PageTitle>
        <PageUrl>Page1.aspx</PageUrl>
        <PageContent>
        This is some text.
        This is another line of text.
        </PageContent>
        <PageLayout>Article Page</PageLayout>
  </Page> 
</Pages>
.Notes
    Name: New-SPPage
    Author: Ryan Dennis
    Last Edit: 7/25/2011
    Keywords: New-SPPage
.Link
    http://www.sharepointryan.com
    http://twitter.com/SharePointRyan
.Inputs
    None
.Outputs
    None
#Requires -Version 2.0
#>
[CmdletBinding()]
    Param(
    [Parameter(Mandatory=$true)]
    [string]$SiteUrl,
    [Parameter(Mandatory=$false)]
    [string]$PageTitle,
    [Parameter(Mandatory=$false)]
    [string]$PageUrl,
    [Parameter(Mandatory=$false)]
    [string]$PageContent,
    [Parameter(Mandatory=$false)]
    [string]$PageLayout,
    [Parameter(Mandatory=$false)]
    [switch]$CheckIn,
    [Parameter(Mandatory=$false)]
    [switch]$Publish,
    [Parameter(Mandatory=$false)]
    [switch]$CreateFromXml,
    [Parameter(Mandatory=$false)]
    [string]$XmlInput
    )
#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)
    $page.Update()

    # Update the filename to the one specified in the XML
    $item = $page.ListItem
    $item["Title"] = $PageTitle;
    $item["Page Content"] = $PageContent;
    $item.Update()
    # Check-in and publish page
    if ($CheckIn){$item.File.CheckIn("")}
    if ($Publish){$item.File.Publish("");}
} #End ForEach Loop
# Dispose of the web
$web.Dispose()
} #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;
$item.Update() 
if ($CheckIn){$item.File.CheckIn("")}
if ($Publish){$item.File.Publish("")}
$site.Dispose()
$web.Dispose()
Stop-SPAssignment -Global
} #End single page from else
} #End function

URL:http://sharepointryan.com/2011/07/25/bulk-creation-of-pages-using-powershell/

CSOM

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() {
param(
   $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) 
   $clientObjectLoad.Invoke($ctx,@($ClientObject,$null))
}


function CreateWikiPage()
{
param(
        [Parameter(Mandatory=$true)][string]$webUrl,
        [Parameter(Mandatory=$false)][System.Net.ICredentials]$credentials,
        [Parameter(Mandatory=$true)][string]$pageUrl,
        [Parameter(Mandatory=$true)][string]$pageContent
    )
            $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" %>';

            $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"
            $wikiPage.Update()
            $ctx.ExecuteQuery();

}




$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.