[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 'Options' -SchemaXml 'Option 1Option 2'
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() }
}