-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathCreate-DefaultListForms.ps1
211 lines (173 loc) · 9.15 KB
/
Create-DefaultListForms.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
<#
Microsoft provides programming examples for illustration only, without warranty either expressed or
implied, including, but not limited to, the implied warranties of merchantability and/or fitness
for a particular purpose. We grant You a nonexclusive, royalty-free right to use and modify the
Sample Code and to reproduce and distribute the object code form of the Sample Code, provided that
You agree: (i) to not use Our name, logo, or trademarks to market Your software product in which the
Sample Code is embedded; (ii) to include a valid copyright notice on Your software product in which
the Sample Code is embedded; and (iii) to indemnify, hold harmless, and defend Us and Our suppliers
from and against any claims or lawsuits, including attorneys' fees, that arise or result from the
use or distribution of the Sample Code.
This sample assumes that you are familiar with the programming language being demonstrated and the
tools used to create and debug procedures. Microsoft support professionals can help explain the
functionality of a particular procedure, but they will not modify these examples to provide added
functionality or construct procedures to meet your specific needs. if you have limited programming
experience, you may want to contact a Microsoft Certified Partner or the Microsoft fee-based consulting
line at (800) 936-5200.
#>
<#
.SYNOPSIS
Script to create default list forms (New, Edit, Display) if they are missing.
Will also re-add missing Form Webpart to existing list forms if they are missing.
Expects a CSV file called TargetListForms.csv to be in the same directory as the script.
CSV should have a siteUrl and listTitle columns.
.DESCRIPTION
Script to create default list forms (New, Edit, Display) if they are missing.
Will also re-add missing Form Webpart to existing list forms if they are missing.
Expects a CSV file called TargetListForms.csv to be in the same directory as the script.
CSV should have a siteUrl and listTitle columns.
Tested with List Templates: 100, 101, 102, 103, 104, 105, 106, 107, 108
NOTE: This script requires the PowerShell module 'SharePointPnPPowerShellOnline' to be installed. If it is missing, the script will attempt to install it.
RECOMMENDATION: Add administrator username and password for your tenant to Windows Credential Manager before running script.
https://github.com/SharePoint/PnP-PowerShell/wiki/How-to-use-the-Windows-Credential-Manager-to-ease-authentication-with-PnP-PowerShell
.PARAMETER CSVPath
Specify the path to a CSV file containing siteUrl and listTitle fields.
.PARAMETER UseWebLogin
Login via web browser
.EXAMPLE
.\Create-DefaultListForms.ps1
Creates list forms for specified siteUrl and listTitle rows in a CSV file in the script directory called Create-DefaultListForms-Sample.csv
.EXAMPLE
.\Create-DefaultListForms.ps1 -CSVPath C:\temp\targetlists.csv
Creates list forms for specified siteUrl and listTitle rows in a CSV file located at C:\temp\targetlists.csv
.EXAMPLE
.\Create-DefaultListForms.ps1 -CSVPath C:\temp\targetlists.csv -UseWebLogin
Creates list forms for specified siteUrl and listTitle rows in a CSV file located at C:\temp\targetlists.csv, using the browser for login
#>
param(
$CSVPath = "$(Split-Path -Parent -Path $MyInvocation.MyCommand.Definition)\Create-DefaultListForms-Sample.csv",
[switch]$UseWebLogin
)
#############################################
$module = Get-Module SharePointPnPPowerShellOnline -ListAvailable
if ($null -eq $module) {
Write-Output "Installing PowerShell Module: SharePointPnPPowerShellOnline"
Install-Module SharePointPnPPowerShellOnline -Force -AllowClobber -Confirm:$false
}
#############################################
$webpartTemplate = @"
<WebPart xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.microsoft.com/WebPart/v2">
<Assembly>Microsoft.SharePoint, Version=16.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c</Assembly>
<TypeName>Microsoft.SharePoint.WebPartPages.ListFormWebPart</TypeName>
<ListName xmlns="http://schemas.microsoft.com/WebPart/v2/ListForm">{{{LIST_ID}}}</ListName>
<ListId xmlns="http://schemas.microsoft.com/WebPart/v2/ListForm">{{LIST_ID}}</ListId>
<PageType xmlns="http://schemas.microsoft.com/WebPart/v2/ListForm">{{PAGE_TYPE}}</PageType>
<FormType xmlns="http://schemas.microsoft.com/WebPart/v2/ListForm">{{FORM_TYPE}}</FormType>
<ControlMode xmlns="http://schemas.microsoft.com/WebPart/v2/ListForm">{{CONTROL_MODE}}</ControlMode>
<ViewFlag xmlns="http://schemas.microsoft.com/WebPart/v2/ListForm">1048576</ViewFlag>
<ViewFlags xmlns="http://schemas.microsoft.com/WebPart/v2/ListForm">Default</ViewFlags>
<ListItemId xmlns="http://schemas.microsoft.com/WebPart/v2/ListForm">0</ListItemId>
</WebPart>
"@
Function Create-DefaultListForm
{
param(
[parameter(Mandatory=$true)]$List,
[parameter(Mandatory=$true)][string]$FormUrl,
[parameter(Mandatory=$true)][ValidateSet("Display", "Edit", "New")]$FormType
)
begin { }
process
{
$webpartXml = $webpartTemplate -replace "{{LIST_ID}}", $List.Id.ToString()
switch ($FormType)
{
"Display" {
$webpartXml = $webpartXml -replace "{{PAGE_TYPE}}", "PAGE_DISPLAYFORM"
$webpartXml = $webpartXml -replace "{{FORM_TYPE}}", "4"
$webpartXml = $webpartXml -replace "{{CONTROL_MODE}}", "Display"
break;
}
"Edit" {
$webpartXml = $webpartXml -replace "{{PAGE_TYPE}}", "PAGE_EDITFORM"
$webpartXml = $webpartXml -replace "{{FORM_TYPE}}", "6"
$webpartXml = $webpartXml -replace "{{CONTROL_MODE}}", "Edit"
break;
}
"New" {
$webpartXml = $webpartXml -replace "{{PAGE_TYPE}}", "PAGE_NEWFORM"
$webpartXml = $webpartXml -replace "{{FORM_TYPE}}", "8"
$webpartXml = $webpartXml -replace "{{CONTROL_MODE}}", "New"
break;
}
}
try
{
#Check if form page already exists
$listPages = Get-PnPProperty -ClientObject $List.RootFolder -Property Files
$formPage = $listPages | Where-Object { $_.ServerRelativeUrl.ToLower() -eq $FormUrl.ToLower() }
if ($null -eq $formPage) {
Write-Output " [Creating Form Page] $FormUrl"
#Create Form
$formPage = $List.RootFolder.Files.AddTemplateFile($FormUrl, [Microsoft.SharePoint.Client.TemplateFileType]::FormPage)
}
else {
#Form page exists, check if form is recognized by list (i.e. form page has a form webpart on it)
$listForms = Get-PnPProperty -ClientObject $List -Property Forms
if ($null -ne $listForms -and $listForms.Count -gt 0) {
$existingForm = $list.Forms | Where-Object { $_.ServerRelativeUrl.ToLower() -eq $FormUrl.ToLower() }
if ($null -ne $existingForm) {
Write-Warning " [Form Already Exists] $FormUrl"
return;
}
}
}
Write-Output " [Adding Form Webpart] $FormUrl"
#Get Webpart Manager for Form
$wpm = $formPage.GetLimitedWebPartManager([Microsoft.SharePoint.Client.WebParts.PersonalizationScope]::Shared)
#Import Webpart on page
$wp = $wpm.ImportWebPart($webpartXml)
#Add webpart to Form
$wpm.AddWebPart($wp.WebPart, "Main", 1) | Out-Null
#Execute changes
$List.Context.ExecuteQuery()
}
catch
{
Write-Error "Error creating form $FormType at $FormUrl. Error: $($_.Exception)"
}
}
end { }
}
#############################################
$csvRows = ConvertFrom-Csv (Get-Content $CSVPath)
if ($null -eq $csvRows) {
Write-Error "Unable to find CSV at path $CSVPath"
break
}
foreach ($row in $csvRows)
{
Write-Output "[$($row.siteUrl)] [$($row.listTitle)]"
if($UseWebLogin) {
Connect-PnPOnline -Url $row.siteUrl -UseWebLogin
}
else {
Connect-PnPOnline -Url $row.siteUrl
}
$list = Get-PnPList $row.listTitle
$listUrl = $list.RootFolder.ServerRelativeUrl
# Handle Document Library Types
if ($list.BaseType -eq [Microsoft.SharePoint.Client.BaseType]::DocumentLibrary) {
Write-Host " > Processing Library: $listUrl"
Create-DefaultListForm -List $list -FormUrl "$listUrl/Forms/Upload.aspx" -FormType New
Create-DefaultListForm -List $list -FormUrl "$listUrl/Forms/DispForm.aspx" -FormType Display
Create-DefaultListForm -List $list -FormUrl "$listUrl/Forms/EditForm.aspx" -FormType Edit
}
# Handle Generic List Types
else {
Write-Host " > Processing List: $listUrl"
Create-DefaultListForm -List $list -FormUrl "$listUrl/NewForm.aspx" -FormType New
Create-DefaultListForm -List $list -FormUrl "$listUrl/DispForm.aspx" -FormType Display
Create-DefaultListForm -List $list -FormUrl "$listUrl/EditForm.aspx" -FormType Edit
}
}