在 GraphAPI returns 中创建团队始终为 null

Create team in GraphAPI returns always null

我正在使用 GraphAPI SDK 在 Microsoft Teams 中创建一个新团队:

        var newTeam = new Team()
        {
            DisplayName = teamName,
            Description = teamName,
            AdditionalData = new Dictionary<string, object>()
            {
                {"template@odata.bind", "https://graph.microsoft.com/v1.0/teamsTemplates('standard')"}
            },
            Members = new TeamMembersCollectionPage()
            {
                new AadUserConversationMember
                {
                    Roles = new List<String>()
                    {
                        "owner"
                    },
                    AdditionalData = new Dictionary<string, object>()
                    {
                        {"user@odata.bind", $"https://graph.microsoft.com/v1.0/users/{userId}"}
                    }
                }
            }
        };

        var team = await this.graphStableClient.Teams
            .Request()
            .AddAsync(newTeam);

问题是我得到的总是空值。根据 documentation 此方法 returns 一个 202 响应 (teamsAsyncOperation),但来自 SDK returns 的 AddAsync 方法是一个 Team 对象。有什么方法可以获取跟踪 url 以检查是否已使用 SDK 完成团队创建?

文档和工作 SDK 的工作方式不同...正如他们在 microsoft-graph-docs/issues/10840 中所写,如果我们使用 contoso-airlines-teams-sample 中的 HttpRequestMessage,我们只能获得 teamsAsyncOperation header 值。他们写信给问这个问题的人,看看加入的团队:)) :)

 var newTeam = new Team()
{
    DisplayName = model.DisplayName,
    Description = model.Description,
    AdditionalData = new Dictionary<string, object>
    {
        ["template@odata.bind"] = $"{graph.BaseUrl}/teamsTemplates('standard')",
        ["members"] = owners.ToArray()
    }
};

// we cannot use 'await client.Teams.Request().AddAsync(newTeam)'
// as we do NOT get the team ID back (object is always null) :(
BaseRequest request = (BaseRequest)graph.Teams.Request();
request.ContentType = "application/json";
request.Method = "POST";

string location;
using (HttpResponseMessage response = await request.SendRequestAsync(newTeam, CancellationToken.None))
    location = response.Headers.Location.ToString();

// looks like: /teams('7070b1fd-1f14-4a06-8617-254724d63cde')/operations('c7c34e52-7ebf-4038-b306-f5af2d9891ac')
// but is documented as: /teams/7070b1fd-1f14-4a06-8617-254724d63cde/operations/c7c34e52-7ebf-4038-b306-f5af2d9891ac
// -> this split supports both of them
string[] locationParts = location.Split(new[] { '\'', '/', '(', ')' }, StringSplitOptions.RemoveEmptyEntries);
string teamId = locationParts[1];
string operationId = locationParts[3];

// before querying the first time we must wait some secs, else we get a 404
int delayInMilliseconds = 5_000;
while (true)
{
    await Task.Delay(delayInMilliseconds);

    // lets see how far the teams creation process is
    TeamsAsyncOperation operation = await graph.Teams[teamId].Operations[operationId].Request().GetAsync();
    if (operation.Status == TeamsAsyncOperationStatus.Succeeded)
        break;

    if (operation.Status == TeamsAsyncOperationStatus.Failed)
        throw new Exception($"Failed to create team '{newTeam.DisplayName}': {operation.Error.Message} ({operation.Error.Code})");

    // according to the docs, we should wait > 30 secs between calls
    // https://docs.microsoft.com/en-us/graph/api/resources/teamsasyncoperation?view=graph-rest-1.0
    delayInMilliseconds = 30_000;
}

// finally, do something with your team...

我从 another question 中找到了解决方案...尝试并发现它有效...