How to find Teams group with microsoft graph API

Highlighted
Deleted
Not applicable

Hello,

 

I would like to only listed Teams group . How it is possible to filter it ?

 

Thanks for your help.

3 Replies
Highlighted
Not yet, afaik.
Highlighted

Please use the below script. To run this script you need to install SPO PnP Powershell module from https://github.com/SharePoint/PnP-PowerShell/releases.

 

Connect-PnPMicrosoftGraph -Scopes “Group.ReadWrite.All”,"Group.Read.All","Directory.Read.All","Directory.AccessAsUser.All","User.ReadBasic.All"
$accesstoken =Get-PnPAccessToken
$group = Invoke-RestMethod -Headers @{Authorization = "Bearer $accesstoken"} -Uri  https://graph.microsoft.com/beta/Groups -Method Get
$Values=$group.value
$TeamsList = @()

foreach($value in $values)
{
    if($value.groupTypes -eq "Unified")
    {
         $id= $value.id

         $team = Invoke-RestMethod -Headers @{Authorization = "Bearer $accesstoken"} -Uri  https://graph.microsoft.com/beta/Groups/$id/channels -Method Get

         If($team.value.id.count -ge 1)
         {
             $Owner = Invoke-RestMethod -Headers @{Authorization = "Bearer $accesstoken"} -Uri  https://graph.microsoft.com/beta/Groups/$id/owners -Method Get
             $Teams = "" | Select "TeamsName","TeamType","Channelcount","ChannelName","Owners"
             $Teams.TeamsName = $value.displayname
             $Teams.TeamType = $value.visibility 
             $Teams.Channelcount = $team.value.id.count
             $Teams.ChannelName = $team.value.displayName
             $Teams.Owners = $Owner.value.userPrincipalName
             $TeamsList+= $Teams
             $Teams=$null

         }
     }
}
$TeamsList  
Highlighted

Using the Graph API from C#

 

private bool IsTeam(UnifiedGroupEntity group)

{

  try {

    var task = GetChannelsAsync(group);

    if (!task.Wait(5000)) {

      throw new Exception("GetChannelsAsync attempt timed out");

      }

    return true;

   } catch (Exception exc) {

    Console.WriteLine("Team call failed: " + exc.Message);

    return false;

   }

}

 

private async Task GetChannelsAsync(UnifiedGroupEntity group) {

  using (HttpClient httpClient = new HttpClient()) {

   httpClient.DefaultRequestHeaders.Add("Authorization", "Bearer "+ AccessToken);

   var url = string.Format("https://graph.microsoft.com/beta/Groups/{0}/channels", group.GroupId);

     HttpResponseMessage hrm = httpClient.GetAsync(url).Result;

   if (hrm.IsSuccessStatusCode) {

     Stream data = await hrm.Content.ReadAsStreamAsync();

     StreamReader reader = new StreamReader(data);

     string text = reader.ReadToEnd();

     //There are better ways to read the json result, hackish code below

     var vstart = text.IndexOf("\"value\":[")+1;

     var vend = text.IndexOf("]", vstart);

     var split = SplitJsonArray(text.Substring(vstart, vend - vstart));

     DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(ChannelResponse));

     foreach (string cjson in split)

        {

      var channel = (ChannelResponse)serializer.ReadObject(new MemoryStream(Encoding.Unicode.GetBytes(cjson))); 

        }

     }

  }

}

public class ChannelResponse {

  public string id { get; set; }

  public string displayname { get; set; }

  public string description { get; set; }

}