Can I search for a file through all the teams/groups I belong to on MS Teams using the Microsoft Graph API

Viewed 106
3 Answers

The group_id is required, see here. enter image description here

You could try Powershell to get the groups that you want, then loop to request MS Graph API.

#sign in your azure account
Connect-AzureAD

#get access token
function Get-AzureRMBearerToken
{
    [CmdletBinding()]
    Param
    (
        $TenantID,

        $AppID,

        $ClientSecret
    )

    $Result=Invoke-RestMethod -Uri https://login.microsoftonline.com/$TenantID/oauth2/token?api-version=1.0 -Method Post -Body @{"grant_type" = "client_credentials"; "resource" = "https://graph.microsoft.com/"; "client_id" = "$AppID"; "client_secret" = "$ClientSecret" }
    $Authorization = "{0} {1}" -f ($result.token_type , $result.access_token)
    $Authorization
}
$accessToken = Get-AzureRMBearerToken  -TenantID "{your tenant id}" -AppID "{application/client id}" -ClientSecret "{value in your client secret}"

#get all groups
$groups = Get-AzureADGroup -All

#request MS Graph API in the loop
Foreach($group in $groups){
    $url = "https://graph.microsoft.com/v1.0/groups/"+$group.ObjectId+"/drive/root/search(q='SEARCH_VALUE')"
    Invoke-RestMethod -Method Get -Uri $url -Headers @{ Authorization = $accessToken }
} 

Note: Make sure your application has the required permissions. Refer to here and here.

I found the solution by using Search Drive Items which is currently available in Beta from Graph API. It searches the list of files in detail from all the team drives and Sharepoint drives you have access to under the azure tenant.

O365 Groups are the foundation of many things in Microsoft's cloud world, a team is an add-on to office 365 groups, as is Sharepoint (where the team files are mostly stored)

I don't believe that there is a single graph query that can return all drive files that you have access to. you would have to build 2 queries, one to query the groups you are a member of, and then loop through it to do a search query on each of the groups. to get all group ids of groups you are a member of is /getMemberGroups https://docs.microsoft.com/en-us/graph/api/user-getmembergroups?view=graph-rest-1.0&tabs=http

You can probably do it with powershell or a simple program or really any language that can call graph.

Related