- Bump version in package.json to 1.3.0 - Add new properties for field mapping in DynamicPollWebPart.manifest.json - Enhance DynamicPollWebPart.ts to support dynamic field mapping for polls and answers - Implement field loading and default application logic in PollDataService.ts - Update PollTypes.ts to include new interfaces for field mapping - Modify DynamicPollCollection.tsx to display configurable web part title - Update styles in DynamicPoll.module.scss for improved UI - Add localization strings for new properties in en-us.js and de-de.js - Update tests to reflect changes in data structure and functionality - Validate project structure and ensure all new features are accounted for
132 lines
6.5 KiB
PowerShell
132 lines
6.5 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" />'
|
|
Ensure-Field -List $polls -InternalName 'PollKey' -SchemaXml '<Field Type="Text" DisplayName="Poll key" Name="PollKey" StaticName="PollKey" Required="FALSE" MaxLength="255" EnforceUniqueValues="TRUE" Indexed="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>'
|
|
|
|
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 '<Field Type="Text" DisplayName="Poll key" Name="PollKey" StaticName="PollKey" Required="FALSE" MaxLength="255" Indexed="TRUE" />'
|
|
Ensure-Field -List $answers -InternalName 'UserKey' -SchemaXml '<Field Type="Text" DisplayName="User key" Name="UserKey" StaticName="UserKey" Required="FALSE" MaxLength="255" Indexed="TRUE" />'
|
|
Ensure-Field -List $answers -InternalName 'Answer' -SchemaXml '<Field Type="Text" DisplayName="Answer" Name="Answer" StaticName="Answer" Required="TRUE" MaxLength="255" />'
|
|
|
|
# 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() }
|
|
}
|