|
| 1 | +function Invoke-CIPPSharePointTemplateDeploy { |
| 2 | + <# |
| 3 | + .SYNOPSIS |
| 4 | + Deploy a SharePoint provisioning template to a single tenant |
| 5 | +
|
| 6 | + .DESCRIPTION |
| 7 | + Provisions every site template in a SharePoint provisioning template against one tenant. |
| 8 | + When the template is marked createAsTeams the container is created as a full Microsoft |
| 9 | + Team first (via the Teams API, so channels and Teams functionality stay intact) and the |
| 10 | + document libraries are added to the backing SharePoint site afterwards. Otherwise a plain |
| 11 | + SharePoint site is created. Root-level and per-library permissions are applied by group |
| 12 | + display name, optionally creating missing groups as security groups. |
| 13 | +
|
| 14 | + .PARAMETER TemplateData |
| 15 | + The deserialized template object (templateName, createAsTeams, createMissingGroups, siteTemplates) |
| 16 | +
|
| 17 | + .PARAMETER SiteOwner |
| 18 | + UPN set as the owner of every site or Team the template creates |
| 19 | +
|
| 20 | + .PARAMETER TenantFilter |
| 21 | + The tenant to deploy to |
| 22 | + #> |
| 23 | + [CmdletBinding()] |
| 24 | + param( |
| 25 | + [Parameter(Mandatory = $true)] |
| 26 | + $TemplateData, |
| 27 | + |
| 28 | + [Parameter(Mandatory = $true)] |
| 29 | + [string]$SiteOwner, |
| 30 | + |
| 31 | + [Parameter(Mandatory = $true)] |
| 32 | + [string]$TenantFilter, |
| 33 | + |
| 34 | + $APIName = 'Deploy SharePoint Template', |
| 35 | + $Headers |
| 36 | + ) |
| 37 | + |
| 38 | + # Extracts the group display name from a stored permission entry: the frontend saves plain |
| 39 | + # strings, but older entries may be autocomplete objects ({label,value}). |
| 40 | + $GetPrincipalName = { param($Principal) $Principal.value ?? $Principal } |
| 41 | + $CreateMissingGroups = $TemplateData.createMissingGroups -eq $true |
| 42 | + |
| 43 | + $Results = [System.Collections.Generic.List[string]]::new() |
| 44 | + foreach ($SiteTemplate in $TemplateData.siteTemplates) { |
| 45 | + try { |
| 46 | + # Create the container first: a full Team (Teams API) so all Teams functionality |
| 47 | + # stays intact, or a plain SharePoint site otherwise. |
| 48 | + if ($TemplateData.createAsTeams -eq $true) { |
| 49 | + $Team = New-CIPPTeam -DisplayName $SiteTemplate.displayName -Description ($SiteTemplate.description ?? '') -Owner $SiteOwner -TenantFilter $TenantFilter -Headers $Headers -APIName $APIName |
| 50 | + $SiteUrl = $Team.SiteUrl |
| 51 | + $Results.Add("[$TenantFilter] Created Team '$($SiteTemplate.displayName)' with site $SiteUrl") |
| 52 | + } else { |
| 53 | + $null = New-CIPPSharepointSite -SiteName $SiteTemplate.displayName -SiteDescription ($SiteTemplate.description ?? $SiteTemplate.displayName) -SiteOwner $SiteOwner -TemplateName 'Team' -TenantFilter $TenantFilter -Headers $Headers -APIName $APIName |
| 54 | + $SharePointInfo = Get-SharePointAdminLink -Public $false -tenantFilter $TenantFilter |
| 55 | + $SitePath = $SiteTemplate.displayName -replace ' ' -replace '[^A-Za-z0-9-]' |
| 56 | + $SiteUrl = "https://$($SharePointInfo.TenantName).sharepoint.com/sites/$SitePath" |
| 57 | + $Results.Add("[$TenantFilter] Created site '$($SiteTemplate.displayName)' at $SiteUrl") |
| 58 | + } |
| 59 | + |
| 60 | + # Root-level permissions, grouped per permission level. |
| 61 | + $RootPermGroups = @($SiteTemplate.permissions) | Group-Object -Property permissionLevel |
| 62 | + foreach ($PermGroup in $RootPermGroups) { |
| 63 | + $GroupNames = @($PermGroup.Group | ForEach-Object { & $GetPrincipalName $_.principal }) | Where-Object { $_ } |
| 64 | + try { |
| 65 | + $PermResult = Set-CIPPSharePointObjectPermission -SiteUrl $SiteUrl -PermissionLevel $PermGroup.Name -GroupNames $GroupNames -CreateMissingGroups:$CreateMissingGroups -TenantFilter $TenantFilter -Headers $Headers -APIName $APIName |
| 66 | + $Results.Add("[$TenantFilter] $($SiteTemplate.displayName): $PermResult") |
| 67 | + } catch { |
| 68 | + $Results.Add("[$TenantFilter] $($SiteTemplate.displayName): root permissions failed - $($_.Exception.Message)") |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + # Then the document libraries via the SharePoint module. |
| 73 | + foreach ($Library in $SiteTemplate.libraries) { |
| 74 | + try { |
| 75 | + $NewLibrary = New-CIPPSharePointLibrary -SiteUrl $SiteUrl -LibraryName $Library.name -Description ($Library.description ?? '') -TenantFilter $TenantFilter -Headers $Headers -APIName $APIName |
| 76 | + $Results.Add("[$TenantFilter] $($SiteTemplate.displayName): library '$($Library.name)' $($NewLibrary.Created ? 'created' : 'already existed')") |
| 77 | + |
| 78 | + $LibPermGroups = @($Library.permissions) | Group-Object -Property permissionLevel |
| 79 | + foreach ($PermGroup in $LibPermGroups) { |
| 80 | + $GroupNames = @($PermGroup.Group | ForEach-Object { & $GetPrincipalName $_.principal }) | Where-Object { $_ } |
| 81 | + try { |
| 82 | + $PermResult = Set-CIPPSharePointObjectPermission -SiteUrl $SiteUrl -ListId $NewLibrary.ListId -PermissionLevel $PermGroup.Name -GroupNames $GroupNames -CreateMissingGroups:$CreateMissingGroups -TenantFilter $TenantFilter -Headers $Headers -APIName $APIName |
| 83 | + $Results.Add("[$TenantFilter] $($SiteTemplate.displayName)/$($Library.name): $PermResult") |
| 84 | + } catch { |
| 85 | + $Results.Add("[$TenantFilter] $($SiteTemplate.displayName)/$($Library.name): permissions failed - $($_.Exception.Message)") |
| 86 | + } |
| 87 | + } |
| 88 | + } catch { |
| 89 | + $Results.Add("[$TenantFilter] $($SiteTemplate.displayName): library '$($Library.name)' failed - $($_.Exception.Message)") |
| 90 | + } |
| 91 | + } |
| 92 | + } catch { |
| 93 | + $Results.Add("[$TenantFilter] Failed to deploy '$($SiteTemplate.displayName)': $($_.Exception.Message)") |
| 94 | + } |
| 95 | + } |
| 96 | + return $Results |
| 97 | +} |
0 commit comments