-
Notifications
You must be signed in to change notification settings - Fork 0
/
Provision-PublishingPage.ps1
134 lines (112 loc) · 5.12 KB
/
Provision-PublishingPage.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#create a new publishing page named home.aspx and apply custom template (BannerHome2) (The templates should be in an accessible place so the script can call it)
#The below code comes from "https://blog.mastykarz.nl/provisioning-publishing-pages-powershell/"
param (
$SiteUrl = "<NAME OF SITE COLLECTION>"
)
function Import-PublishingPage {
param (
$SiteUrl = $(throw "Required parameter -SiteUrl missing"),
[xml]$PageXml = $(throw "Required parameter -PageXml missing")
)
$site = New-Object Microsoft.SharePoint.SPSite($SiteUrl)
$psite = New-Object Microsoft.SharePoint.Publishing.PublishingSite($site)
$web = Get-SPWeb $SiteUrl
$pweb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($web)
$pagesListName = $pweb.PagesListName
# get prerequisites
$pageName = $PageXml.Module.File.Url
if (-not($pageName)) {
throw "Page name missing in <File Url='...'/>"
}
$plDefinition = $PageXml.Module.File.Property | Where { $_.Name -eq "PublishingPageLayout" }
if (-not($plDefinition)) {
throw "Page Layout reference missing in <File><Property Name='PublishingPageLayout' Value='...'/></File>"
}
$plUrl = New-Object Microsoft.SharePoint.SPFieldUrlValue($plDefinition.Value)
$plName = $plUrl.Url.Substring($plUrl.Url.LastIndexOf('/') + 1)
$pl = $psite.GetPageLayouts($false) | Where { $_.Name -eq $plName }
if (-not($pl)) {
throw "Page Layout '$plName' not found"
}
[Microsoft.SharePoint.Publishing.PublishingPage]$page = $null
$file = $web.GetFile("$pagesListName/$pageName")
if (-not($file.Exists)) {
Write-Host "Page $pageName not found. Creating..." -NoNewline
$page = $pweb.AddPublishingPage($pageName, $pl)
Write-Host "DONE" -ForegroundColor Green
}
else {
Write-Host "Configuring '$($file.ServerRelativeUrl)'..."
$item = $file.Item
$page = [Microsoft.SharePoint.Publishing.PublishingPage]::GetPublishingPage($item)
if ($page.ListItem.File.CheckOutStatus -eq [Microsoft.SharePoint.SPFile+SPCheckOutStatus]::None) {
$page.CheckOut()
}
}
if ($PageXml.Module.File.AllUsersWebPart) {
Write-Host "`tImporting Web Parts..." -NoNewline
# fake context
[System.Web.HttpRequest] $request = New-Object System.Web.HttpRequest("", $web.Url, "")
$sw = New-Object System.IO.StringWriter
$hr = New-Object System.Web.HttpResponse($sw)
[System.Web.HttpContext]::Current = New-Object System.Web.HttpContext($request, $hr)
[Microsoft.SharePoint.WebControls.SPControl]::SetContextWeb([System.Web.HttpContext]::Current, $web)
$wpMgr = $web.GetLimitedWebPartManager("$pagesListName/$pageName", [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)
foreach ($webPartDefinition in $PageXml.Module.File.AllUsersWebPart) {
$err = $null
$sr = New-Object System.IO.StringReader($webPartDefinition.InnerText)
$xtr = New-Object System.Xml.XmlTextReader($sr);
$wp = $wpMgr.ImportWebPart($xtr, [ref] $err)
$oldWebPartId = $webPartDefinition.ID.Trim("{", "}")
$wp.ID = "g_" + $oldWebPartId.Replace("-", "_")
$wpMgr.AddWebPart($wp, $webPartDefinition.WebPartZoneID, $webPartDefinition.WebPartOrder)
Write-Host "." -NoNewline
}
[System.Web.HttpContext]::Current = $null
Write-Host "`n`tWeb Parts successfully imported"
}
else {
Write-Host "`tNo Web Parts found"
}
Write-Host "`tImporting content..."
$li = $page.ListItem
foreach ($property in $PageXml.Module.File.Property) {
Write-Host "`t$($property.Name)..." -NoNewline
$field = $li.Fields.GetField($property.Name)
if (-not($field.IsReadOnlyField)) {
try {
$value = $field.GetValidatedString($property.Value.Replace("~SiteCollection/", $site.ServerRelativeUrl).Replace("~Site/", $web.ServerRelativeUrl))
if ($value) {
$li[$property.Name] = $value
Write-Host "DONE" -ForegroundColor Green
}
else {
Write-Host "SKIPPED (Invalid value)" -ForegroundColor Red
}
}
catch {
Write-Host "SKIPPED (Invalid value)" -ForegroundColor Red
}
}
else {
Write-Host "SKIPPED (ReadOnly)" -ForegroundColor Red
}
}
$li.Update()
Write-Host "`tContent import completed" -ForegroundColor Green
$page.CheckIn("")
$file = $page.ListItem.File
$file.Publish("")
#$file.Approve("")
Write-Host "Page successfully imported" -ForegroundColor Green
}
$pages = @{
"home_default.aspx.xml" = "/";
"aboutus_default.aspx.xml" = "/about-us";
}
$pages.GetEnumerator() | % {
[xml]$pageXml = Get-Content "Pages\$($_.Name)"
Import-PublishingPage "$SiteUrl$($_.Value)" $pageXml
#Add the Content Editor Web parts
#Add html files to each content editor web part (names of files will match the zone sections on the page)
#Make new page the homepage and publish