How to Create an O365 Group in Teams Integrated with MS Graph API

Introduction 

 
Hi guys, as we all know, MS Graph is Microsoft’s RESTful API that allows you to interact directly with Azure AD, Office 365, Intune, SharePoint, Teams, OneNote, and a whole lot more. Today, let’s explore a good way to create a Teams Group using PnP PowerShell programming, with MS Graphs Integration.
 
Steps: 

First, log on to the https://dev.office.com/app-registration to sign in for app registration. You can finish your app registration along with the certificates and a secret for confidentiality purposes.

Next, open your Windows PowerShell, running it as an administrator.

Run the below commands with the mentioned choices

Get-ExecutionPolicy RemoteSigned

Get-ExecutionPolicy -list

Set-ExecutionPolicy -ExecutionPolicy unrestricted

>>Enter Yes to All option by Entering A

Set-ExecutionPolicy -ExecutionPolicy ByPass

>>Enter Yes to All option by Entering A

Just save the Below PnP PowerShell code with the name, “CreategroupandTeamsusingPnPandGraphscript.ps1” on your Local Desktop D Drive. I mostly run all my scripts from the D: Drive. Make your respective changes & tunings as per your process by following the comments marked as ‘#’.

param

(

[Parameter(Mandatory=$true)]

[string]$displayName,

[Parameter(Mandatory=$true)]

[string]$teamDescription,

[Parameter(Mandatory=$true)]

[string]$teamOwner

)

#Enter your App ID, App Secret details from the App Registration process

#Enter your Own Tenant Name

Connect-PnPOnline -AppId "99******-****-***-***-******" -AppSecret "BB_***************" -AADDomain "<TenantName>.onmicrosoft.com"

$accessToken = Get-PnPAccessToken

$headers = @{

"Content-Type" = "application/json"

Authorization = "Bearer $accessToken"

}

# List all Teams

$apiUrl = "https://graph.microsoft.com/beta/groups?$filter=resourceProvisioningOptions/Any(x:x eq 'Team')"

$Data = Invoke-RestMethod -Uri "https://graph.microsoft.com/beta/groups?$filter=resourceProvisioningOptions/Any(x:x eq 'Team')" -Method Get -Headers $headers -UseBasicParsing

$Teams = ($Data | select-object Value).Value | Select-Object displayName, id, description

$Teams

Write-Host "wait until PnP creates an Office 365 group with" $displayName "............" -f Yellow

$createGroupRequest = @{

displayName = $displayName

description = $teamDescription

groupTypes = @("Unified")

mailEnabled = $true

mailNickName = $teamOwner

securityEnabled = $false

visibility = "Private"

}

$createGroupBody = ConvertTo-Json -InputObject $createGroupRequest | % { [System.Text.RegularExpressions.Regex]::Unescape($_) }

$response = Invoke-RestMethod -Uri "https://graph.microsoft.com/beta/groups" -Body $createGroupBody -Method Post -Headers $headers -UseBasicParsing

Start-Sleep -s 15

$groupId = $response.id

Write-Host $displayName" Group created Successfully" -f Green

Write-Host "wait until PnP creates a Team with " $displayName "............" -f Yellow

$userResponse = Invoke-RestMethod -Uri "https://graph.microsoft.com/v1.0/users/$teamOwner" -Method Get -Headers $headers

$teamOwnerID = $userResponse.id

$owners = @("https://graph.microsoft.com/beta/users/$teamOwnerID")

$teamRequest = @{

"[email protected]" = "https://graph.microsoft.com/beta/teamsTemplates('standard')"

displayName = $displayName

description = $teamDescription

"[email protected]" = $owners

}

$teamRequestBody = ConvertTo-Json -InputObject $teamRequest | % { [System.Text.RegularExpressions.Regex]::Unescape($_) }

$response = Invoke-RestMethod -Uri "https://graph.microsoft.com/beta/teams" -Body $teamRequestBody -Method Post -Headers $headers -UseBasicParsing

Write-Host $displayName"MS Team created Successfully" -f Green

Start-Sleep -s 20

Write-Host "wait until PnP gets a Team Id with " $displayName " Name............" -f Yellow

# Grab Team via Display Name (not secure)

$queryUrl = 'https://graph.microsoft.com/beta/groups?$filter=resourceProvisioningOptions'

$queryUrl += "/Any(x:x eq 'Team') and displayName eq '$displayName'"

$teamResponse = Invoke-RestMethod -Uri $queryUrl -Method Get -Headers $headers

$orderedTeams = $teamResponse.value | Sort-Object -Property createdDateTime -Descending

$TeamID = $orderedTeams[0].id

return $TeamID

*********************

Now, run D:\CreategroupandTeamsusingPnPandGraphscript.ps1

Just enter the details prompted, like DisplayName, TeamDescription, TeamOwner etc. This is from the end that best suits your operational information. 

Now log in to https://admin.microsoft.com/AdminPortal/Home#/groups to see if your O365 Group was created or not. Similarly, check from your MS Teams also to verify if the Group was created as per your overall process. Finally, when you Open MS teams you can see the created Team and when you open Groups you can see the created Group.

Open https://developer.microsoft.com/en-us/graph/graph-explorer and run this simple query: https://graph.microsoft.com/v1.0/me/memberOf

Good luck working with MS Graph Interface. For further information on working with MS Graphs, please go through these C# Blogs:

https://www.c-sharpcorner.com/article/introduction-to-ms-graph-api/

https://www.c-sharpcorner.com/article/office-365-microsoft-graph-beginning-part-one/