Get site collection URL after creating a Group using Microsoft Graph
When creating groups or teams using Microsoft Graph and PowerShell the SharePoint site collection URL can be randomly generated if a site collection with the name of the group already exists.
If you need to get the URL of the site collection right after the creation of the group, you can use Microsoft Graph to retrieve it.
This can be extremely helpfully for scenarios where you have the need to deploy artifacts to the site automatically, like applying a PnP Template.
Right after the group creation use do the request bellow using the group ID.
$response = Invoke-RestMethod -Uri "https://graph.microsoft.com/v1.0/groups/$groupId/sites/root/weburl" -Method Get -Headers $headers -UseBasicParsing $webURL = $response.value Write-Host $webURL
Below you have a complete example that you can use to retrieve the site collection URL after the group creation. More information on how to automate the deployment of Groups and Teams is available here.
# Connect to AAD to consume the Microsoft Graph Connect-PnPOnline -Scopes "Group.ReadWrite.All" $accessToken = Get-PnPAccessToken #Prepare generic OAuth Bearer token header $headers = @{ "Content-Type" = "application/json" Authorization = "Bearer $accessToken" } #Create Office 365 Group $createGroupRequest = @{ displayName = "HANDS ON SharePoint" description = "Group Provisioned with Microsoft Graph" groupTypes = @("Unified") mailEnabled = $true mailNickName = "HOs Bloggers" securityEnabled = $false visibility = "Private" } $createGroupBody = ConvertTo-Json -InputObject $createGroupRequest $response = Invoke-RestMethod -Uri "https://graph.microsoft.com/v1.0/groups" -body $createGroupBody -Method Post -Headers $headers -UseBasicParsing $groupId = $response.id Write-Host ("Group ID {0}" -f $groupId) #Wait for the group creation Start-Sleep -s 15 # Get SharePoint Site URL $response = Invoke-RestMethod -Uri "https://graph.microsoft.com/v1.0/groups/$groupId/sites/root/weburl" -Method Get -Headers $headers -UseBasicParsing $webURL = $response.value Write-Host ("SharePoint Site Collection URL - {0}" -f $webURL)
No comments yet