I was asked a few days ago if we could automate Microsoft Teams
creation when a Modern Team Site is created in SharePoint Online. I got to think about it and came up with one way. This is using an Graph Beta endpoint
for the Team creation, so it’s not advisable to use this in production scenarios before it’s in v1.0
We are going to create a Site Script that calls a Flow
The Flow utilizes the Office365 Group(Unified) after the site is created and creates a Team via the Graph.
Since we are calling the Graph via Flow I’m setting up App Only permissions in the Azure AD for the application.
Via PnP PowerShell we will:
Open a new Notepad
Go to the Azure Portal -> select Azure AD -> properties.
Copy the Directory ID
to Notepad
Select App Registrations -> Create new App and name it whatever you want -> then copy the Application ID
to Notepad
Go to Settings -> Keys.
Create a new Key and Save to get the value. Copy the Value
to Notepad
Go to Required Permissions -> Add Permission -> Select API -> Microsoft Graph -> under Application Permissions - Select Read And Write All Groups
Grant permissions
Go to the Microsoft Flow site -> select Create from Blank
Search for Request and select Request - When A HTTP Request is received
Copy the HTTP POST URL to Notepad -> then enter this JSON in the request body
{"type": "object","properties": {"webUrl": {"type": "string"},"webDescription": {"type": "string"},"webTitle": {"type": "string"},"creatorName": {"type": "string"},"creatorEmail": {"type": "string"},"createdTimeUTC": {"type": "string"},"parameters": {"type": "object","properties": {}}}}
Create new action - Search for Initialize and select Initialize Variable. Create three of these, rename them and enter the values from Notepad.
Create new action - Search and select HTTP. Rename it and enter the values:
Method: POST
Uri: https://login.windows.net/@{variables('TenantID')}/oauth2/token
Headers: content-type: application/json;odata=verbose
Body: client_id=@{variables('ClientID')}&client_Secret=@{variables('SecretID')}&resource=https://graph.microsoft.com&grant_type=client_credentials
{"type": "object","properties": {"token_type": {"type": "string"},"expires_in": {"type": "string"},"ext_expires_in": {"type": "string"},"expires_on": {"type": "string"},"not_before": {"type": "string"},"resource": {"type": "string"},"access_token": {"type": "string"}}}
Method: GET
Uri: https://graph.microsoft.com/v1.0/groups?$filter=displayName%20eq%20'@{triggerBody()?['webTitle']}'
Headers: Authorization: Bearer @{body('Parse_and_get_Access_Token')?['access_token']}
{"type": "object","properties": {"@@odata.context": {"type": "string"},"value": {"type": "array","items": {"type": "object","properties": {"id": {"type": "string"}},"required": ["id"]}}}}
Method: PUT
Uri: https://graph.microsoft.com/beta/groups/@{items('Apply_to_each')?['id']}/team
Headers: Authorization: Bearer @{body('Parse_and_get_Access_Token')?['access_token']}
Body:
{"memberSettings": {"allowCreateUpdateChannels": true},"messagingSettings": {"allowUserEditMessages": true,"allowUserDeleteMessages": true},"funSettings": {"allowGiphy": true,"giphyContentRating": "strict"}}
So one of the actions in Site Scripts is triggerFlow
. Since we are associating this Site Script to a Hub Site it will be run everytime a site is associated to the hub, for that site. The Flow will start and create the Team
{"actions": [{"verb": "triggerFlow","url": "https://prod-31.westeurope.logic.azure.com...","name": "Create Team"}]}
As I stated earlier we’re going to use PnP PowerShell Commands to the extent it’s possible. They are doing an awesome job updating these, so it won’t be long until we can do all of this with PnP.
$admin="user.name@tenant.onmicrosoft.com"$cred = Get-Credential -UserName $admin -Message "Admin"Connect-PnPOnline -Url https://tenant-admin.sharepoint.com -Credential $cred
$scriptFromFile = Get-Content 'C:\Users\...createTeam.json' -Raw$siteScript = Add-PnPSiteScript -Title "Auto Create Team" -Description "Creates Team based on teamsite" -Content $scriptFromFile$siteDesign = Add-PnPSiteDesign -Title "Auto Create Team" -WebTemplate "64" -SiteScriptIds $siteScript.Id -Description "Creates Team base on teamsite"
$hub = New-PnPSite -Type CommunicationSite -Title "Project Hub" -Url "https://tenant.sharepoint.com/sites/projecthub"
Register-PnPHubSite -Site $hub
Right now Set-PnPHubSite doesn’t seem to have the ability to set Site Design, so I’m using Set-SpOHubSite instead.
$admin="user.name@tenant.onmicrosoft.com"$cred = Get-Credential -UserName $admin -Message "Admin"Connect-SpOService -Url https://tenant-admin.sharepoint.com -Credential $cred
Set-SpOHubSite -Identity $hub.ID -SiteDesignId $siteDesign.Id
$teamSite = New-PnPSite -Type TeamSite -Title "Project 1" -Alias "project1"
Add-PnPHubSiteAssociation -Site $teamSite -HubSite "https://tenant.sharepoint.com/sites/projecthub"