Files
CustomBranding/deployment/add-custombranding.ps1
Torsten Brendgen 0e32b831bd Initial commit
2026-04-13 10:24:29 +02:00

104 lines
3.0 KiB
PowerShell

param(
[Parameter(Mandatory = $true)]
[Alias('WebUrl')]
[string]$SiteUrl,
[string]$CssPath = '',
[string]$Description = 'MSFT-Custom-Solution:CustomBranding'
)
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue | Out-Null
$componentId = '035ba968-6488-4d42-86b3-0470ffcc95b9'
$location = 'ClientSideExtension.ApplicationCustomizer'
$name = 'CustomBranding'
$title = 'Custom Branding'
function Get-CustomBrandingPropertiesJson {
param(
[string]$Path
)
$config = @{
cssfiles = @()
placeholdertop = @{ elements = @() }
placeholderbottom = @{ elements = @() }
}
if ($Path -and $Path.Trim().Length -gt 0) {
$config.cssfiles += @{ path = $Path.Trim() }
}
return ($config | ConvertTo-Json -Depth 10 -Compress)
}
function Remove-WebScopedComponentActions {
param(
[Microsoft.SharePoint.SPSite]$CurrentSite,
[string]$CurrentComponentId,
[string]$CurrentLocation
)
$removedCount = 0
foreach ($web in $CurrentSite.AllWebs) {
try {
$webActions = @($web.UserCustomActions | Where-Object {
$_.Location -eq $CurrentLocation -and
$_.ClientSideComponentId -and
$_.ClientSideComponentId.ToString().ToLower() -eq $CurrentComponentId
})
foreach ($webAction in $webActions) {
$web.UserCustomActions.Delete($webAction.Id)
$removedCount++
}
if ($webActions.Count -gt 0) {
$web.Update()
}
}
finally {
$web.Dispose()
}
}
return $removedCount
}
$site = Get-SPSite -Identity $SiteUrl
try {
$removedWebScopedActions = Remove-WebScopedComponentActions -CurrentSite $site -CurrentComponentId $componentId -CurrentLocation $location
$existingAction = $site.UserCustomActions | Where-Object {
$_.Location -eq $location -and $_.ClientSideComponentId -and $_.ClientSideComponentId.ToString().ToLower() -eq $componentId
} | Select-Object -First 1
if ($existingAction) {
$action = $existingAction
Write-Host 'Aktualisiere vorhandene site-scoped Custom Branding UserCustomAction...' -ForegroundColor Yellow
}
else {
$action = $site.UserCustomActions.Add()
Write-Host 'Erstelle neue site-scoped Custom Branding UserCustomAction...' -ForegroundColor Yellow
}
$action.Name = $name
$action.Title = $title
$action.Description = $Description
$action.Location = $location
$action.ClientSideComponentId = [Guid]$componentId
$action.ClientSideComponentProperties = Get-CustomBrandingPropertiesJson -Path $CssPath
$action.Update()
Write-Host 'Custom Branding wurde site-scoped registriert.' -ForegroundColor Green
Write-Host ('Entfernte web-scoped Eintraege: ' + $removedWebScopedActions)
Write-Host ('Beschreibung: ' + $Description)
Write-Host ('Properties: ' + $action.ClientSideComponentProperties)
}
finally {
if ($site) {
$site.Dispose()
}
}