简体   繁体   English

在 GraphAPI 中创建团队总是返回 null

[英]Create team in GraphAPI returns always null

I am using GraphAPI SDK to create a new Team in Microsoft Teams:我正在使用 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);

The problem is that I get always null.问题是我总是得到 null。 According documentation this method returns a 202 response (teamsAsyncOperation), but the AddAsync method from SDK returns a Team object.根据文档,此方法返回 202 响应 (teamsAsyncOperation),但 SDK 中的 AddAsync 方法返回 Team object。 Is there any way to get the tracking url to check if the team creation has been finished with the SDK?有什么方法可以跟踪 url 以检查团队创建是否已使用 SDK 完成?

Documentation and working SDK works different... As they wrote in microsoft-graph-docs/issues/10840, we can only get the teamsAsyncOperation header values if we use HttpRequestMessage as in contoso-airlines-teams-sample.文档和工作 SDK 的工作方式不同......正如他们在 microsoft-graph-docs/issues/10840 中所写的那样,如果我们在 contoso-airlines-teams-sample 中使用 HttpRequestMessage,我们只能获取 teamAsyncOperation header 值。 They wrote to the people who asks this problem, look to the joined teams:)):)他们写信给提出这个问题的人,看看加入的团队:)):)

 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...

I found a solution from another question ... Tried and saw that it's working...我从另一个问题中找到了解决方案......尝试并看到它正在工作......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM