Search code examples
c#microsoft-graph-apimicrosoft-graph-sdksmicrosoft-graph-teamsmicrosoft-graph-edu

How to create a team with template educationClass via Microsoft Graph with C#


I am trying without results to create a team in a educational tenant with educationClass template, dinamically with Microsoft Graph SDK for C#. This is the code with the BETA api that is recommended from the official website (https://learn.microsoft.com/es-es/graph/api/team-post?view=graph-rest-beta&tabs=csharp).

GraphServiceClient graphClient = new GraphServiceClient( authProvider );

var team = new Team
{
    DisplayName = "My Sample Team",
    Description = "My Sample Team’s Description",
    AdditionalData = new Dictionary<string, object>()
    {
        {"template@odata.bind", "https://graph.microsoft.com/beta/teamsTemplates('educationClass')"},
        {"owners@odata.bind", "[\"https://graph.microsoft.com/beta/users('userId')\"]"}
    }
};

await graphClient.Teams
    .Request()
    .AddAsync(team);

When I execute this code it throws the following error: Microsoft.Graph.ServiceException: 'Code: BadRequest Message: Invalid URL format specified in @odata.bind for owners

Thank you!


Solution

  • I found a solution to the problem. Instead of creating and array in a string as is recommended in the official page I used an external array of strings:

    GraphServiceClient graphClient = new GraphServiceClient( authProvider );
    string[] owners = new string[1];
    owners[0] = "https://graph.microsoft.com/beta/users/userId";
    var team = new Team
    {
        DisplayName = "My Sample Team",
        Description = "My Sample Team’s Description",
        AdditionalData = new Dictionary<string, object>()
        {
            {"template@odata.bind", "https://graph.microsoft.com/beta/teamsTemplates('educationClass')"},
            {"owners@odata.bind", owners}
        }
    };
    
    await graphClient.Teams
        .Request()
        .AddAsync(team);