Files
DynamicPoll/deployment/Initialize-DynamicPollLists.ps1
T

93 lines
4.4 KiB
PowerShell

[CmdletBinding()]
param(
[Parameter(Mandatory = $true)]
[string]$SiteUrl,
[string]$PollListTitle = 'Polls',
[string]$AnswerListTitle = 'Poll Answers'
)
$ErrorActionPreference = 'Stop'
function Ensure-Field {
param(
[Microsoft.SharePoint.SPList]$List,
[string]$InternalName,
[string]$SchemaXml
)
if (-not $List.Fields.ContainsField($InternalName)) {
[void]$List.Fields.AddFieldAsXml($SchemaXml, $true, [Microsoft.SharePoint.SPAddFieldOptions]::AddFieldInternalNameHint)
$List.Update()
}
}
$web = Get-SPWeb -Identity $SiteUrl
try {
$polls = $web.Lists.TryGetList($PollListTitle)
if (-not $polls) {
$listId = $web.Lists.Add($PollListTitle, 'Questions and options for the Dynamic Poll web part.', [Microsoft.SharePoint.SPListTemplateType]::GenericList)
$polls = $web.Lists[$listId]
$polls.OnQuickLaunch = $true
$polls.Update()
}
Ensure-Field -List $polls -InternalName 'Question' -SchemaXml '<Field Type="Text" DisplayName="Question" Name="Question" StaticName="Question" Required="TRUE" />'
$legacyOptionsField = $null
try { $legacyOptionsField = $polls.Fields.GetFieldByInternalName('Options') } catch { }
if ($legacyOptionsField) {
$legacyOptionsField.Title = 'Options (legacy)'
$legacyOptionsField.Required = $false
$legacyOptionsField.ShowInNewForm = $false
$legacyOptionsField.ShowInEditForm = $false
$legacyOptionsField.ShowInDisplayForm = $false
$legacyOptionsField.Update()
}
Ensure-Field -List $polls -InternalName 'OptionsText' -SchemaXml '<Field Type="Note" DisplayName="Options" Name="OptionsText" StaticName="OptionsText" Required="FALSE" NumLines="8" RichText="FALSE" AppendOnly="FALSE" />'
if ($legacyOptionsField) {
foreach ($item in $polls.Items) {
if (-not [string]::IsNullOrWhiteSpace([string]$item['OptionsText']) -or $null -eq $item[$legacyOptionsField.Id]) { continue }
$legacyValue = $legacyOptionsField.GetFieldValue([string]$item[$legacyOptionsField.Id])
$optionLines = New-Object System.Collections.Generic.List[string]
if ($legacyValue -is [Microsoft.SharePoint.SPFieldMultiChoiceValue]) {
for ($choiceIndex = 0; $choiceIndex -lt $legacyValue.Count; $choiceIndex++) {
if (-not [string]::IsNullOrWhiteSpace($legacyValue[$choiceIndex])) { $optionLines.Add($legacyValue[$choiceIndex]) }
}
}
elseif (-not [string]::IsNullOrWhiteSpace([string]$legacyValue)) {
$optionLines.Add([string]$legacyValue)
}
if ($optionLines.Count -gt 0) {
$item['OptionsText'] = ($optionLines -join [Environment]::NewLine)
$item.SystemUpdate($false)
}
}
}
Ensure-Field -List $polls -InternalName 'StartDate' -SchemaXml '<Field Type="DateTime" DisplayName="Start date" Name="StartDate" StaticName="StartDate" Format="DateTime" />'
Ensure-Field -List $polls -InternalName 'EndDate' -SchemaXml '<Field Type="DateTime" DisplayName="End date" Name="EndDate" StaticName="EndDate" Format="DateTime" />'
Ensure-Field -List $polls -InternalName 'IsActive' -SchemaXml '<Field Type="Boolean" DisplayName="Active" Name="IsActive" StaticName="IsActive"><Default>0</Default></Field>'
$answers = $web.Lists.TryGetList($AnswerListTitle)
if (-not $answers) {
$listId = $web.Lists.Add($AnswerListTitle, 'Votes submitted through the Dynamic Poll web part.', [Microsoft.SharePoint.SPListTemplateType]::GenericList)
$answers = $web.Lists[$listId]
$answers.OnQuickLaunch = $false
$answers.Update()
}
$pollListId = $polls.ID.ToString('B')
Ensure-Field -List $answers -InternalName 'Poll' -SchemaXml (('<Field Type="Lookup" DisplayName="Poll" Name="Poll" StaticName="Poll" List="{0}" ShowField="Title" Required="TRUE" />' -f $pollListId))
Ensure-Field -List $answers -InternalName 'Answer' -SchemaXml '<Field Type="Text" DisplayName="Answer" Name="Answer" StaticName="Answer" Required="TRUE" MaxLength="255" />'
Write-Host ('Dynamic Poll lists are ready at {0}' -f $web.Url) -ForegroundColor Green
Write-Host ('Poll list: {0} ({1})' -f $polls.Title, $polls.ID)
Write-Host ('Answer list: {0} ({1})' -f $answers.Title, $answers.ID)
}
finally {
if ($web) { $web.Dispose() }
}