|
| 1 | +using namespace System.Net |
| 2 | + |
| 3 | +function Invoke-DeleteSharepointSite { |
| 4 | + <# |
| 5 | + .FUNCTIONALITY |
| 6 | + Entrypoint |
| 7 | + .ROLE |
| 8 | + Sharepoint.Site.ReadWrite |
| 9 | + #> |
| 10 | + [CmdletBinding()] |
| 11 | + param($Request, $TriggerMetadata) |
| 12 | + |
| 13 | + $APIName = $Request.Params.CIPPEndpoint |
| 14 | + $Headers = $Request.Headers |
| 15 | + Write-LogMessage -Headers $Headers -API $APIName -message 'Accessed this API' -Sev 'Debug' |
| 16 | + |
| 17 | + # Interact with query parameters or the body of the request. |
| 18 | + $TenantFilter = $Request.Body.tenantFilter |
| 19 | + $SiteId = $Request.Body.SiteId |
| 20 | + |
| 21 | + try { |
| 22 | + # Validate required parameters |
| 23 | + if (-not $SiteId) { |
| 24 | + throw "SiteId is required" |
| 25 | + } |
| 26 | + if (-not $TenantFilter) { |
| 27 | + throw "TenantFilter is required" |
| 28 | + } |
| 29 | + |
| 30 | + # Validate SiteId format (GUID) |
| 31 | + if ($SiteId -notmatch '^(\{)?[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}(\})?$') { |
| 32 | + throw "SiteId must be a valid GUID" |
| 33 | + } |
| 34 | + |
| 35 | + $SharePointInfo = Get-SharePointAdminLink -Public $false -tenantFilter $TenantFilter |
| 36 | + |
| 37 | + # Get site information using SharePoint admin API |
| 38 | + $SiteInfoUri = "$($SharePointInfo.AdminUrl)/_api/SPO.Tenant/sites('$SiteId')" |
| 39 | + |
| 40 | + # Add the headers that SharePoint REST API expects |
| 41 | + $ExtraHeaders = @{ |
| 42 | + 'accept' = 'application/json' |
| 43 | + 'content-type' = 'application/json' |
| 44 | + 'odata-version' = '4.0' |
| 45 | + } |
| 46 | + |
| 47 | + $SiteInfo = New-GraphGETRequest -scope "$($SharePointInfo.AdminUrl)/.default" -uri $SiteInfoUri -tenantid $TenantFilter -extraHeaders $ExtraHeaders |
| 48 | + |
| 49 | + if (-not $SiteInfo) { |
| 50 | + throw "Could not retrieve site information from SharePoint Admin API" |
| 51 | + } |
| 52 | + |
| 53 | + # Determine if site is group-connected based on GroupId |
| 54 | + $IsGroupConnected = $SiteInfo.GroupId -and $SiteInfo.GroupId -ne "00000000-0000-0000-0000-000000000000" |
| 55 | + |
| 56 | + if ($IsGroupConnected) { |
| 57 | + # Use GroupSiteManager/Delete for group-connected sites |
| 58 | + $body = @{ |
| 59 | + siteUrl = $SiteInfo.Url |
| 60 | + } |
| 61 | + $DeleteUri = "$($SharePointInfo.AdminUrl)/_api/GroupSiteManager/Delete" |
| 62 | + } else { |
| 63 | + # Use SPSiteManager/delete for regular sites |
| 64 | + $body = @{ |
| 65 | + siteId = $SiteId |
| 66 | + } |
| 67 | + $DeleteUri = "$($SharePointInfo.AdminUrl)/_api/SPSiteManager/delete" |
| 68 | + } |
| 69 | + |
| 70 | + # Execute the deletion |
| 71 | + $DeleteResult = New-GraphPOSTRequest -scope "$($SharePointInfo.AdminUrl)/.default" -uri $DeleteUri -body (ConvertTo-Json -Depth 10 -InputObject $body) -tenantid $TenantFilter -extraHeaders $ExtraHeaders |
| 72 | + |
| 73 | + $SiteTypeMsg = if ($IsGroupConnected) { "group-connected" } else { "regular" } |
| 74 | + $Results = "Successfully initiated deletion of $SiteTypeMsg SharePoint site with ID $SiteId, this process can take some time to complete in the background" |
| 75 | + |
| 76 | + Write-LogMessage -headers $Headers -API $APIName -tenant $TenantFilter -message $Results -sev Info |
| 77 | + $StatusCode = [HttpStatusCode]::OK |
| 78 | + |
| 79 | + } catch { |
| 80 | + $ErrorMessage = Get-CippException -Exception $_ |
| 81 | + $Results = "Failed to delete SharePoint site with ID $SiteId. Error: $($ErrorMessage.NormalizedError)" |
| 82 | + Write-LogMessage -headers $Headers -API $APIName -tenant $TenantFilter -message $Results -sev Error -LogData $ErrorMessage |
| 83 | + $StatusCode = [HttpStatusCode]::InternalServerError |
| 84 | + } |
| 85 | + |
| 86 | + # Associate values to output bindings |
| 87 | + Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{ |
| 88 | + StatusCode = $StatusCode |
| 89 | + Body = @{ 'Results' = $Results } |
| 90 | + }) |
| 91 | +} |
0 commit comments