[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 ''
Ensure-Field -List $polls -InternalName 'PollKey' -SchemaXml ''
$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 ''
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 ''
Ensure-Field -List $polls -InternalName 'EndDate' -SchemaXml ''
Ensure-Field -List $polls -InternalName 'IsActive' -SchemaXml '0'
foreach ($item in $polls.Items) {
if ([string]::IsNullOrWhiteSpace([string]$item['PollKey'])) {
$item['PollKey'] = ('Poll-{0}' -f $item.ID)
$item.SystemUpdate($false)
}
}
$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()
}
Ensure-Field -List $answers -InternalName 'PollKey' -SchemaXml ''
Ensure-Field -List $answers -InternalName 'UserKey' -SchemaXml ''
Ensure-Field -List $answers -InternalName 'Answer' -SchemaXml ''
# A lookup cannot be distributed safely through the Content Type Hub because its target list ID
# belongs to one site collection. Keep an existing legacy lookup readable, but stop requiring it.
$legacyPollField = $null
try { $legacyPollField = $answers.Fields.GetFieldByInternalName('Poll') } catch { }
if ($legacyPollField -and $legacyPollField.Type -eq [Microsoft.SharePoint.SPFieldType]::Lookup) {
$legacyPollField.Required = $false
$legacyPollField.ShowInNewForm = $false
$legacyPollField.ShowInEditForm = $false
$legacyPollField.ShowInDisplayForm = $false
$legacyPollField.Update()
}
foreach ($answerItem in $answers.Items) {
$changed = $false
if ($legacyPollField -and $legacyPollField.Type -eq [Microsoft.SharePoint.SPFieldType]::Lookup) {
if ([string]::IsNullOrWhiteSpace([string]$answerItem['PollKey']) -and $null -ne $answerItem[$legacyPollField.Id]) {
$lookup = New-Object Microsoft.SharePoint.SPFieldLookupValue([string]$answerItem[$legacyPollField.Id])
$pollItem = $polls.GetItemById($lookup.LookupId)
$answerItem['PollKey'] = [string]$pollItem['PollKey']
$changed = $true
}
}
if ([string]::IsNullOrWhiteSpace([string]$answerItem['UserKey']) -and
-not [string]::IsNullOrWhiteSpace([string]$answerItem['Title'])) {
$answerItem['UserKey'] = [string]$answerItem['Title']
$changed = $true
}
if ($changed) { $answerItem.SystemUpdate($false) }
}
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() }
}