skip to Main Content

I’m searching a way to add multiple owners to a group inside Azure Entra ID. I’m using the msgraph API with Python.
The documentation available here show how to add one owner, but I want to add multiple owners in one call, and I can’t find anything on that.

For now, my function work but only for one user :

async def add_team_as_owners(graph_client, group_name, group_id):
"""
Add multiple users as owners of a group
Args:
    graph_client (GraphServiceClient): The Graph client
    group_id (str): The ID of the group to update
Returns:
    str: Return code (204 if successful)
"""
request_body = ReferenceCreate(
    odata_id = "https://graph.microsoft.com/v1.0/users/<azure_user_id>"
)

try:
    result = await graph_client.groups.by_group_id(group_id).owners.ref.post(request_body)
    logging.info(f"Group updated successfully: {group_name} - {group_id}")
    return result
except Exception as e:
    logging.error(f"Error updating the group: {group_name} - {group_id}")
    logging.error(f"Error detail: {e}")

Is someone has an idea or an example to achieve that ?

Thanks for your time,
Regards,

2

Answers


  1. Chosen as BEST ANSWER

    Based on the answer of Sridevi, which I thank for this, I've updated my code to create a loop on all users I wanted to add as owners. It works perfectly, but I find sad that we can't do a one call to add multiple owners.

    Anyway, thanks again for your time, and here's my code after the correction :

    async def add_team_as_owners(graph_client, group_name, group_id, user_id):
    """
    Add Team as owner of the group
    Args:
        graph_client (GraphServiceClient): The Graph client
        group_id (str): The ID of the group to update
        user_id (str): The ID of the user to add as owner
    Returns:
        str: Return code (204 if successful)
    
    """
    request_body = ReferenceCreate(odata_id = "https://graph.microsoft.com/v1.0/users/"+user_id)
    
    try:
        result = await graph_client.groups.by_group_id(group_id).owners.ref.post(request_body)
        logging.info(f"Group updated successfully: {group_name} - {group_id}")
        return result
    except Exception as e:
        logging.error(f"Error updating the group: {group_name} - {group_id}")
        logging.error(f"Error detail: {e}")
    

    The call of this function is done by these lines:

    for user_id in ["<user_id_1>", "<user_id_2>", "<user_id_3>"]:
      runner.run(add_team_as_owners(client, source_group_name, source_group_id, user_id))
    

  2. You can update the group and a maximum of 20 owners and members can be added as part of group update

    request_body = Group(
        additional_data = {
                "owners@odata_bind" : [
                    "https://graph.microsoft.com/v1.0/users/{user_id_1}",
                    "https://graph.microsoft.com/v1.0/users/{user_id_2}"
                ]
        }
    )
    
    result = await graph_client.groups.patch(request_body)
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search