[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 '' $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' $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 (('' -f $pollListId)) Ensure-Field -List $answers -InternalName 'Answer' -SchemaXml '' 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() } }