Create publishing pages from Xml file
#Input Xml's
#SampleBlogs.xml (File name is derived from title, just illegal characters removed and replaced by dash)
<?xml version="1.0" encoding="utf-8"?>
<Pages>
<Page>
<Title>Page 1</Title>
<IntroText>Intro</IntroText>
<PageContent>
content
</PageContent>
<Author>Lukas</Author>
<Category>Cat1</Category>
<PublishingDate>10/19/2012</PublishingDate>
</Page>
<Page>
<Title>Page title</Title>
<IntroText>Some intro 2</IntroText>
<PageContent>
Page content of page2
</PageContent>
<Author>Marian</Author>
<Category>Cat2</Category>
<PublishingDate>06/23/2012</PublishingDate>
</Page>
</Pages>
#SampleBasePages.xml (here filename is set directly)
<Pages>
<Page>
<Title>Page 3</Title>
<FileName>mypage3</FileName>
<Author>Michal</Author>
<PublishingDate></PublishingDate>
</Page>
<Page>
<Title>Page 4</Title>
<FileName>mypage4</FileName>
<PageContent>Some content</PageContent>
<Author></Author>
<PublishingDate></PublishingDate>
</Page>
</Page>
# ============================== Initial Settings ==================================
$siteUrl="https://server/sitecollection"
$location="c:/temp"
# page layouts
$pageLayoutBlog = @{
xmlPath = $location + '\SampleBlogs.xml';
url = "/_catalogs/masterpage/CustomPageLayout1.aspx";
ID = '0x010100C568DB52D9D0A14D9B2FDCC96666E9F2007948130EC3DB064584E219954237AF390009FBB3819E595546BF3173275BBC22D10099977315E5C941F28E2B15B71758295700D2D5A315115749309874B502CDDF6A56'
}
$pageLayoutBasePage = @{
xmlPath = $location + '\SampleBasePages.xml';
url = "/_catalogs/masterpage/CustomPageLayout2.aspx";
ID = '0x010100C568DB52D9D0A14D9B2FDCC96666E9F2007948130EC3DB064584E219954237AF390009FBB3819E595546BF3173275BBC22D10099977315E5C941F28E2B15B717582959'
}
$pageLayouts = @($pageLayoutBlog, $pageLayoutBasePage)
# lookup source list
$sourceListCategoriesName = "Custom Categories"
$sourceListAuthorsName = "Custom Authors"
#======================================== Script =================================
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Sharepoint")
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Sharepoint.Publishing")
# Get publishing web
Start-SPAssignment -Global
$site = get-spsite $siteUrl
$web = $site.rootweb #or OpenWeb()
$web.AllowUnsafeUpdates=$true
$pagesList=$web.Lists["Pages"]
$pWeb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($web)
$sourcelistCategories = $web.Lists[$sourceListCategoriesName] #categories for lookup field
$sourcelistAuthors = $web.Lists[$sourceListAuthorsName]
#===================== Create pages ===========================
foreach ($pageLayout in $pageLayouts)
{
#Load xml file
if(test-path $pageLayout.xmlPath)
{
$xml = [xml](get-content $pageLayout.xmlPath)
}
else
{
write-host $pageLayout.xmlPath " is not valid."
break;
}
foreach($xmlPage in $xml.Pages.Page)
{
if(-not(ListItemExists -sourceList $pagesList -value $xmlPage.Title))
{
Write-Host “Creating " $xmlPage.Title " page..."
# Create blank page
$newPage = $pWeb.AddPublishingPage()
$newPage.Update()
#regex to find illegal characters in fileName
$pattern = "[{0}]" -f ([Regex]::Escape([String][System.IO.Path]::GetInvalidFileNameChars()))
# Update the filename and other fields
if([string]::IsNullOrEmpty($xmlPage.FileName))
{
$pageName=[Regex]::Replace($xmlPage.Title, $pattern, '-') #$pageurl
}
else
{
$pageName=$xmlPage.FileName
}
$newPage.ListItem["BaseName"] = $pageName
$newPage.ListItem["Title"] = $xmlPage.Title
}
else
{
write-host "Updating "$xmlPage.Title " page..."
$newPage = GetPageByTitle -pweb $pweb -title $xmlPage.Title
if($newPage -eq $null)
{
#ToDo: This is dirty, create function to get page name if you have title
$newPage = GetPageByName -pweb $pweb -name $xmlPage.FileName
}
if($newPage -eq $null)
{
write-host "There is no file with name or title: " $xmlPage.Title ", " $xmlPage.FileName
break;
}
}
$newPage.ListItem["PublishingPageContent"] = $xmlPage.PageContent
# Change page layout
$newPage.ListItem["PublishingPageLayout"] = $pageLayout.url
$newPage.ListItem["ContentTypeId"] = $pageLayout.ID
#Add values to field from new page layout - use DisplayName or StaticName
#ArticleDate
if(-not([string]::IsNullOrEmpty($xmlPage.PublishingDate))){
$newPage.ListItem["ArticleDate"]=[datetime]($xmlPage.PublishingDate)
}
#CustomAuthor
if(-not([string]::IsNullOrEmpty($xmlPage.Author))){
$newPage.ListItem["CustomAuthor"]=(DefineLookupValue -sourcelist $sourcelistAuthors -value $xmlPage.Author)
}
#CustomCategory
if(-not([string]::IsNullOrEmpty($xmlPage.Category))){
$newPage.ListItem["CustomCategory"]=(DefineLookupValueMulti -sourcelist $sourcelistCategories -value $xmlPage.Category)
}
#IntroductionText
if(-not([string]::IsNullOrEmpty($xmlPage.IntroText))){
$newPage.ListItem["Introduction Text"] = $xmlPage.IntroText
}
$newPage.ListItem.SystemUpdate()
write-host "Publishing page " $xmlPage.Title " was succesfuly created."
write-host "---------------"
}
}