'Report on office groups without owner

Use case: I need to create a report on office groups without owner. This happens when people leave the company and their account is deleted. Their groups live further, but eventually group expiration kicks in and somebody need to take action.

Question: What is the easiest was to create query in graph to filter groups that do not have an owner?

What I currently do is: List all groups Enumerate this list and look for groups where the owner array is empty:

https://graph.microsoft.com/v1.0/groups/{id}/owners?$select=mail

This returns an empty array when there are no owners.

{
  "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#directoryObjects(mail)",
  "value": []
}

This, in theory, works, but takes ages as we have many 10 thousands of groups, so I am looking for a solution that makes this possible with one query. I also tried to filter the exprationDatTime property to limit results but query this doesn't seem to be supported. I mainly need groups that are due to expire.



Solution 1:[1]

There is no way how to get groups without owners with one query.

What you can do is to query all groups, expand owners and select only id of the group and id of the owner. It will minimize the response size.

Then iterate through the all groups and check for empty owners collection.

GET https://graph.microsoft.com/v1.0/groups?$expand=owners($select=id)&$select=id

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 user2250152