|
| 1 | +function New-CIPPSharepointSite { |
| 2 | + <# |
| 3 | + .SYNOPSIS |
| 4 | + Create a new SharePoint site |
| 5 | +
|
| 6 | + .DESCRIPTION |
| 7 | + Create a new SharePoint site using the Modern REST API |
| 8 | +
|
| 9 | + .PARAMETER SiteName |
| 10 | + The name of the site |
| 11 | +
|
| 12 | + .PARAMETER SiteDescription |
| 13 | + The description of the site |
| 14 | +
|
| 15 | + .PARAMETER SiteOwner |
| 16 | + The username of the site owner |
| 17 | +
|
| 18 | + .PARAMETER TemplateName |
| 19 | + The template to use for the site. Default is Communication |
| 20 | +
|
| 21 | + .PARAMETER SiteDesign |
| 22 | + The design to use for the site. Default is Topic |
| 23 | +
|
| 24 | + .PARAMETER WebTemplateExtensionId |
| 25 | + The web template extension ID to use |
| 26 | +
|
| 27 | + .PARAMETER SensitivityLabel |
| 28 | + The Purview sensitivity label to apply to the site |
| 29 | +
|
| 30 | + .PARAMETER TenantFilter |
| 31 | + The tenant associated with the site |
| 32 | +
|
| 33 | + #> |
| 34 | + [CmdletBinding(SupportsShouldProcess = $true)] |
| 35 | + Param( |
| 36 | + [Parameter(Mandatory = $true)] |
| 37 | + [string]$SiteName, |
| 38 | + |
| 39 | + [Parameter(Mandatory = $true)] |
| 40 | + [string]$SiteDescription, |
| 41 | + |
| 42 | + [Parameter(Mandatory = $true)] |
| 43 | + [string]$SiteOwner, |
| 44 | + |
| 45 | + [Parameter(Mandatory = $false)] |
| 46 | + [ValidateSet('Communication', 'Team')] |
| 47 | + [string]$TemplateName = 'Communication', |
| 48 | + |
| 49 | + [Parameter(Mandatory = $false)] |
| 50 | + [ValidateSet('Topic', 'Showcase', 'Blank', 'Custom')] |
| 51 | + [string]$SiteDesign = 'Showcase', |
| 52 | + |
| 53 | + [Parameter(Mandatory = $false)] |
| 54 | + [ValidatePattern('(\{|\()?[A-Za-z0-9]{4}([A-Za-z0-9]{4}\-?){4}[A-Za-z0-9]{12}(\}|\()?')] |
| 55 | + [string]$WebTemplateExtensionId, |
| 56 | + |
| 57 | + [Parameter(Mandatory = $false)] |
| 58 | + [ValidatePattern('(\{|\()?[A-Za-z0-9]{4}([A-Za-z0-9]{4}\-?){4}[A-Za-z0-9]{12}(\}|\()?')] |
| 59 | + [string]$SensitivityLabel, |
| 60 | + |
| 61 | + [string]$Classification, |
| 62 | + |
| 63 | + [Parameter(Mandatory = $true)] |
| 64 | + [string]$TenantFilter |
| 65 | + ) |
| 66 | + $tenantName = (New-GraphGetRequest -uri 'https://graph.microsoft.com/beta/sites/root' -asApp $true -tenantid $TenantFilter).id.Split('.')[0] |
| 67 | + $AdminUrl = "https://$($tenantName)-admin.sharepoint.com" |
| 68 | + $SitePath = $SiteName -replace ' ' -replace '[^A-Za-z0-9-]' |
| 69 | + $SiteUrl = "https://$tenantName.sharepoint.com/sites/$SitePath" |
| 70 | + |
| 71 | + |
| 72 | + |
| 73 | + |
| 74 | + switch ($TemplateName) { |
| 75 | + 'Communication' { |
| 76 | + $WebTemplate = 'SITEPAGEPUBLISHING#0' |
| 77 | + } |
| 78 | + 'Team' { |
| 79 | + $WebTemplate = 'STS#0' |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + $WebTemplateExtensionId = '00000000-0000-0000-0000-000000000000' |
| 84 | + $DefaultSiteDesignIds = @( '96c933ac-3698-44c7-9f4a-5fd17d71af9e', '6142d2a0-63a5-4ba0-aede-d9fefca2c767', 'f6cc5403-0d63-442e-96c0-285923709ffc') |
| 85 | + |
| 86 | + switch ($SiteDesign) { |
| 87 | + 'Topic' { |
| 88 | + $SiteDesignId = '96c933ac-3698-44c7-9f4a-5fd17d71af9e' |
| 89 | + } |
| 90 | + 'Showcase' { |
| 91 | + $SiteDesignId = '6142d2a0-63a5-4ba0-aede-d9fefca2c767' |
| 92 | + } |
| 93 | + 'Blank' { |
| 94 | + $SiteDesignId = 'f6cc5403-0d63-442e-96c0-285923709ffc' |
| 95 | + } |
| 96 | + 'Custom' { |
| 97 | + if ($WebTemplateExtensionId -match '^[0-9a-f]{8}-([0-9a-f]{4}-){3}[0-9a-f]{12}$') { |
| 98 | + if ($WebTemplateExtensionId -notin $DefaultSiteDesignIds) { |
| 99 | + $WebTemplateExtensionId = $SiteDesign |
| 100 | + $SiteDesignId = '00000000-0000-0000-0000-000000000000' |
| 101 | + } else { |
| 102 | + $SiteDesignId = $WebTemplateExtensionId |
| 103 | + } |
| 104 | + } else { |
| 105 | + $SiteDesignId = '96c933ac-3698-44c7-9f4a-5fd17d71af9e' |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + # Create the request body |
| 111 | + $Request = @{ |
| 112 | + Title = $SiteName |
| 113 | + Url = $SiteUrl |
| 114 | + Lcid = 1033 |
| 115 | + ShareByEmailEnabled = $false |
| 116 | + Description = $SiteDescription |
| 117 | + WebTemplate = $WebTemplate |
| 118 | + SiteDesignId = $SiteDesignId |
| 119 | + Owner = $SiteOwner |
| 120 | + WebTemplateExtensionId = $WebTemplateExtensionId |
| 121 | + } |
| 122 | + |
| 123 | + # Set the sensitivity label if provided |
| 124 | + if ($SensitivityLabel) { |
| 125 | + $Request.SensitivityLabel = $SensitivityLabel |
| 126 | + } |
| 127 | + if ($Classification) { |
| 128 | + $Request.Classification = $Classification |
| 129 | + } |
| 130 | + |
| 131 | + Write-Verbose ($Request | ConvertTo-Json -Compress -Depth 10) |
| 132 | + |
| 133 | + $body = @{ |
| 134 | + request = $Request |
| 135 | + } |
| 136 | + |
| 137 | + # Create the site |
| 138 | + if ($PSCmdlet.ShouldProcess($SiteName, 'Create new SharePoint site')) { |
| 139 | + $AddedHeaders = @{ |
| 140 | + 'accept' = 'application/json;odata.metadata=none' |
| 141 | + 'odata-version' = '4.0' |
| 142 | + } |
| 143 | + New-GraphPostRequest -scope "$AdminUrl/.default" -uri "$AdminUrl/_api/SPSiteManager/create" -Body ($body | ConvertTo-Json -Compress -Depth 10) -tenantid $TenantFilter -ContentType 'application/json' -AddedHeaders $AddedHeaders |
| 144 | + } |
| 145 | +} |
0 commit comments