Microsoft Teams | Provisioning private channels

Header TeamsPrivateChannels
Reading Time: 4 minutes

So it is a most common known problem that when you create automated a private channel, there will be no Sharepoint site provisioned until you click Files in the teams app. I Found a solution.

Situation

We provision a new team and add some private channels. To these private channels, we would like to migrate some data. If the private teams site is not yet provisioned, you will not get a WebUrl for this site. No WebUrl, is no destination for a file share migration. Microsoft Sharepoint Migration Tool but also Sharegate needs this url to know the destination.

Solution

The provisioning of teams and the private channel, we need Powershell. Powershell, Graph API and an app registration with application permissions will do the trick. After the creation of a Team, the drive will not be ready. This will be when you click on “Files” in the General tab or when you create something on the drive. If the drive in General is ready, the drive in the Private Channel will also be ready instantly. So after creating a new team, we create temporary a folder with Graph API and delete that folder also. After that we create a private channel.

App registration

Create an app registration in Azure Active Directory with the permissions:

  • Channel.Create
  • Files.ReadWrite.All
  • Sites.Read.All
  • Team.Create
  • User.Read

These permissions are enough to do the trick. Provide it with an Admin Consent and you are all set.

Custom PowerShell functions

I Wrote some custom PowerShell functions to help me in my scripts. This time I will share them with you.

Get-TokenForGraphApi

A PowerShell function to get the token from Graph API. It needs the input of:

  • AppId
  • Clientsecret
  • TenantId
function Get-TokenForGraphAPI {
    param(
        [Parameter(Mandatory)]
        [string]$appid,
        [Parameter(Mandatory)]
        [string]$clientsecret,
        [Parameter(Mandatory)]
        [string]$tenantid
    )
    $Body = @{
        Grant_Type    = "client_credentials"
        Scope         = "https://graph.microsoft.com/.default"
        Client_Id     = $appid
        Client_Secret = $ClientSecret
    }
    $Connection = Invoke-RestMethod `
        -Uri https://login.microsoftonline.com/$($TenantID)/oauth2/v2.0/token `
        -Method POST `
        -Body $body
    #Get the Access Token
    return $connection
}
Create-Teams

A PowerShell function to create a new Team using Microsoft Graph API. It needs input:

  • TeamsName (sort of DisplayName)
  • Owner (using the UPN is enough)
  • Description (just write what it is about the team)
function Create-Teams {
    # #Team.Create
    param(
        [Parameter(Mandatory)]
        [string]$Teamsname,
        [Parameter(Mandatory)]
        [string]$Owner,
        [Parameter(Mandatory)]
        [string]$Description
    )
    $userDatabind = "https://graph.microsoft.com/v1.0/users('$($owner)')"
    $params = @{
        "template@odata.bind" = "https://graph.microsoft.com/v1.0/teamsTemplates('standard')"
        displayName           = $Teamsname
        description           = $description
        members               = @(
            @{
                "@odata.type"     = "#microsoft.graph.aadUserConversationMember"
                "user@odata.bind" = $userDatabind
                roles             = @(
                    "owner"
                )
            }
        )
    }
    $body = $params | ConvertTo-Json -Depth 5
    $uri = "https://graph.microsoft.com/v1.0/teams"
    $header = @{
        'Authorization' = "BEARER $($Token.access_token)"
        'Content-type'  = "application/json"
    }
    try {  
        $createTeams = Invoke-Restmethod -Method POST -Uri $uri -headers $header -Body $body
        return $createTeams
    }
    catch {
        $webError = $_
        $webError = ($weberror | convertFrom-json).error.innererror.code
        return $weberror
    }
}
Create-Channel

A PowerShell function to create a new Channel within a team using Microsoft Graph API. It needs input:

  • TeamsId (you will need to retrieve this after creation of the team)
  • ChannelName (sort of DisplayName for the naming of the Channel)
  • Description (a description about the Channel)
  • Owner (in case of a private channel)
  • Type (input can only be: Standard, Private, Shared)
function Create-Channel {
    param(
        [Parameter(Mandatory)]
        [string]$TeamsId,
        [Parameter(Mandatory)]
        [string]$ChannelName,
        [Parameter(Mandatory)]
        [string]$Description,
        [Parameter(Mandatory)]
        [string]$owner,
        [Parameter(Mandatory)]
        [string]$Type
    )
    $userDatabind = "https://graph.microsoft.com/v1.0/users('$($owner)')"
    $params = @{
        "@odata.type"  = "#Microsoft.Graph.channel"
        membershipType = $Type
        displayName    = $ChannelName
        description    = $description
        members        = @(
            @{
                "@odata.type"     = "#microsoft.graph.aadUserConversationMember"
                "user@odata.bind" = $userDatabind
                roles             = @(
                    "owner"
                )
            }
        )
    }
    $body = $params | ConvertTo-Json -Depth 5
    $apiUri = "https://graph.microsoft.com/v1.0/Teams/$teamsId/channels"
    $header = @{
        'Authorization' = "BEARER $($Token.access_token)"
        'Content-type'  = "application/json"
    }
    try {  
        $createChannel = Invoke-Restmethod -Method POST -Uri $apiUri -headers $header -Body $body
    }
    catch {
        $webError = $_
        $createChannel = ($weberror | convertFrom-json).error.innererror.code
    }
    return $createChannel
}
Variables

For input we have variables. We need to define them:

$appid = "YOUR APP ID"
$clientsecret = "YOUR CLIENT SECRET" 
$tenantid = "YOUR TENANT ID"
$teamsName = "THE NAME OF THE TEAM"
$channel1 = "Standard Channel"
$channel2 = "Private Channel"
$Description = "THE DESCRIPTION OF THE TEAM"
$owner = "THE UPN OF THE OWNER OF TEAM"
$folderToCreate = "FOLDER TO CREATE" 
Getting a token
$Token = Get-TokenForGraphAPI -appid $appid -clientsecret $clientsecret -Tenantid $tenantid
Creating a new team
$team = Create-Teams -Teamsname $teamsName -Owner $owner -Description $Description 
Checking if team exists
do {
    $Apiuri = "https://graph.microsoft.com/v1.0/teams" + '?$filter=startsWith(displayName, ' + "'$teamsname')"
    $Teams = (Invoke-RestMethod -Headers @{Authorization = "Bearer $($Token.access_token)" } -Uri $apiUri -Method Get).value
    start-sleep -seconds 2
} while ($Teams.count -eq 0)
Getting the “GENERAL” channelId
$apiUri = "https://graph.microsoft.com/v1.0/Teams/$($teams.id)/channels"
$channels = (Invoke-RestMethod -Headers @{Authorization = "Bearer $($Token.access_token)" } -Uri $apiUri -Method Get).value | Where-Object { $_.DisplayName -eq "General" }
Getting information about the “Drive” in Sharepoint
$apiUri = "https://graph.microsoft.com/v1.0/teams/$($teams.id)/channels/$($Channels.id)/filesFolder"
$drive = Invoke-Restmethod -method Get -headers @{Authorization = "Bearer $($Token.access_token)" } -Uri $apiUri 
$driveId = $drive.id
$driveitemId = $drive.parentReference.DriveId
Setting Header for Graph API Post call
$header = @{
    'Authorization' = "BEARER $($Token.access_token)"
    'Content-type'  = "application/json"
}
Building parameters for folder creation
    $params = @{
        name                                = $FolderToCreate
        folder                              = @{
        }
        "@microsoft.graph.conflictBehavior" = "rename"
    }
    $params = $params | ConvertTo-Json
Creating a folder
$ApiUri = "https://graph.microsoft.com/v1.0/drives/$driveitemid/items/$DriveId/children"
$createFolder = Invoke-Restmethod -Method POST -Uri $apiUri -Headers $header -body $params
Deleting the folder
$apiUri = "https://graph.microsoft.com/v1.0/drives/$driveItemId/items/$($createFolder.id)"
$DeleteFolder = Invoke-RestMethod -Method DELETE -uri $apiUri -Headers $header
Creating Channels
$FirstChannel = Create-Channel -TeamsId $Teams.id -ChannelName $Channel1 -Description $Description -owner $owner -Type "Standard"
$privateChannel = Create-Channel -TeamsId $Teams.id -ChannelName $Channel2 -Description $Description -owner $owner -Type "Private"
Getting WebUrl from Private channel

do {
    try {  
        $folderlocation = Invoke-Restmethod -method Get -headers @{Authorization = "Bearer $($Token.access_token)" } -Uri https://graph.microsoft.com/v1.0/teams/$($teams.id)/channels/$($privateChannel.id)/filesFolder
        $mustRetry = 0
    }
    catch {
        $weberror = $_
        $mustRetry = 1
        Start-Sleep -seconds 5
    }
} while (
    $mustRetry -eq 1
)
$Folderlocation

Results

So now you can provision automated through Graph API a teams and a private channel without the worry that the storage location will not be available. You can use it in your scripts migrating fileshares to Microsoft Teams.

Share and Enjoy !

Shares
Designer (23)

Stay close to the action—follow GetToThe.Cloud across social!
Deep dives and hands‑on how‑tos on Azure Local, hybrid cloud, automation, PowerShell/Bicep, AVD + FSLogix, image pipelines, monitoring, networking, and resilient design when the internet/Azure is down.

🔗 Our channels
▶️ YouTube: https://www.youtube.com/channel/UCa33PgGdXt-Dr4w3Ub9hrdQ
💼 LinkedIn Group: https://www.linkedin.com/groups/9181126/
✖️ X (Twitter): https://x.com/Gettothecloud
🎵 TikTok: https://www.tiktok.com/@gettothecloud
🐙 GitHub: https://github.com/GetToThe-Cloud/Website
💬 Slack: DM us for an invite
📲 WhatsApp: DM for the community link

We use cookies to personalise content and ads, to provide social media features and to analyse our traffic. We also share information about your use of our site with our social media, advertising and analytics partners. View more
Cookies settings
Accept
Privacy & Cookie policy
Privacy & Cookies policy
Cookie name Active

Who we are

Our website address is: https://www.gettothe.cloud

Comments

When visitors leave comments on the site we collect the data shown in the comments form, and also the visitor’s IP address and browser user agent string to help spam detection.An anonymized string created from your email address (also called a hash) may be provided to the Gravatar service to see if you are using it. The Gravatar service privacy policy is available here: https://automattic.com/privacy/. After approval of your comment, your profile picture is visible to the public in the context of your comment.

Media

If you upload images to the website, you should avoid uploading images with embedded location data (EXIF GPS) included. Visitors to the website can download and extract any location data from images on the website.

Cookies

If you leave a comment on our site you may opt-in to saving your name, email address and website in cookies. These are for your convenience so that you do not have to fill in your details again when you leave another comment. These cookies will last for one year.If you visit our login page, we will set a temporary cookie to determine if your browser accepts cookies. This cookie contains no personal data and is discarded when you close your browser.When you log in, we will also set up several cookies to save your login information and your screen display choices. Login cookies last for two days, and screen options cookies last for a year. If you select "Remember Me", your login will persist for two weeks. If you log out of your account, the login cookies will be removed.If you edit or publish an article, an additional cookie will be saved in your browser. This cookie includes no personal data and simply indicates the post ID of the article you just edited. It expires after 1 day.

Embedded content from other websites

Articles on this site may include embedded content (e.g. videos, images, articles, etc.). Embedded content from other websites behaves in the exact same way as if the visitor has visited the other website.These websites may collect data about you, use cookies, embed additional third-party tracking, and monitor your interaction with that embedded content, including tracking your interaction with the embedded content if you have an account and are logged in to that website.

Who we share your data with

If you request a password reset, your IP address will be included in the reset email.

How long we retain your data

If you leave a comment, the comment and its metadata are retained indefinitely. This is so we can recognize and approve any follow-up comments automatically instead of holding them in a moderation queue.For users that register on our website (if any), we also store the personal information they provide in their user profile. All users can see, edit, or delete their personal information at any time (except they cannot change their username). Website administrators can also see and edit that information.

What rights you have over your data

If you have an account on this site, or have left comments, you can request to receive an exported file of the personal data we hold about you, including any data you have provided to us. You can also request that we erase any personal data we hold about you. This does not include any data we are obliged to keep for administrative, legal, or security purposes.

Where we send your data

Visitor comments may be checked through an automated spam detection service.
Save settings
Cookies settings