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
}