5

I'm trying to batch create wiki pages from an xml file, and have found a few posts that I'm trying to combine, such as this one. There's some problem with my code (Sharepoint isn't creating the pages), but it's not giving me an error message. I'm pretty sure this code is over-complicated, but I'm not sure which parts are necessary, and which are extraneous. Thanks for the help.

CODE:

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 CreateTestWiki()
{
[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,
    [Parameter(Mandatory=$false)][System.Net.NetworkCredential]$credentials
    )
#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)
$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


$wikiPages = $ctx.Web.Lists.GetByTitle("Pages")
$load = [Microsoft.SharePoint.Client.ClientContext].GetMethod("Load") 
$listLoad = $load.MakeGenericMethod([Microsoft.SharePoint.Client.List]) 
$listLoad.Invoke($ctx,@($wikiPages,$null)) 
$ctx.ExecuteQuery()

# 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

}

2 Answers 2

1

The following example demonstrates how to create a Wiki pages in SharePoint 2010 using CSOM.

Example

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
}
1
  • Thanks for your help. Is it possible to add keywords from the xml file? I know it's more complicated, since it's a multi-value field. Commented May 14, 2014 at 13:03
3

Thanks for the help

This is what I came up with:

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 CreateWikiPage()
{
param(
        [Parameter(Mandatory=$true)][string]$webUrl,
        [Parameter(Mandatory=$false)][System.Net.NetworkCredential]$credentials,
        [Parameter(Mandatory=$true)][string]$pageName,
        [Parameter(Mandatory=$true)][string]$pageContent,
        [Parameter(Mandatory=$false)][string]$XmlInput
    )
            $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


            $wikiPages = $ctx.Web.Lists.GetByTitle("Pages")
            $load = [Microsoft.SharePoint.Client.ClientContext].GetMethod("Load") 
            $listLoad = $load.MakeGenericMethod([Microsoft.SharePoint.Client.List]) 
            $listLoad.Invoke($ctx,@($wikiPages,$null)) 
            $ctx.ExecuteQuery()


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


            $wikiFile = $wikiPages.RootFolder.Files.Add($file)
            $fileLoad = $load.MakeGenericMethod([Microsoft.SharePoint.Client.File]) 
            $fileLoad.Invoke($ctx,@($wikiFile,$null))

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

}

[xml]$pagesXML = Get-Content $('c:\temp\testWiki.xml')
if ($pagesXML -eq $null) { return }

$pagesXML.Pages.Page | ForEach-Object {
    if($_.PageName -eq $_PageTitle)
    {
        Write-Host "Page ($PageName) exists."
        break;
    }
    $credentials = New-Object System.Net.NetworkCredential('username', 'password','url')
    $PageName = [string]$_.PageTitle
    $PageContent = [string]$_.PageContent
    CreateWikiPage $webUrl $credentials $pageName $pageContent
    Write-Host "Creating $($PageName)"
} #End ForEach Loop

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.