在 C# 中创建 MS Teams 团队 - AddAsync returns null
Create MS Teams team in C# - AddAsync returns null
上下文:
我正在使用 C# 中的 MS Graph API 创建新的 MS 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()
}
};
var team = await graph.Teams.Request().AddAsync(newTeam);
问题:
团队创建得很好,但我无法获取它的 ID。 Return 类型的 AddAsync
方法是 Task<Team>
但它总是 returns 空。
我使用 Fiddler 检查了服务器响应,发现创建的团队的 ID 在响应中返回 headers。
Content-Location: /teams('cbf27e30-658b-4021-a8c6-4002b9adaf41')
很遗憾,我不知道如何访问这些信息。
您将通过调用 GET joined Teams 获取您的团队 ID,它将获取团队 ID、名称和描述
幸运的是,这很容易通过使用请求基础 class BaseRequest
并自己发送来实现。使用收到的 HttpResponseMessage
,您将获得 headers 记录 here,其中包含您的新团队的 ID。
该代码还包括如何使用忙等待等待团队创建完成——这不被视为最佳实践,但使示例更容易。更好的方法是存储团队 ID 并定期查询创建状态。
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...
上下文:
我正在使用 C# 中的 MS Graph API 创建新的 MS 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()
}
};
var team = await graph.Teams.Request().AddAsync(newTeam);
问题:
团队创建得很好,但我无法获取它的 ID。 Return 类型的 AddAsync
方法是 Task<Team>
但它总是 returns 空。
我使用 Fiddler 检查了服务器响应,发现创建的团队的 ID 在响应中返回 headers。
Content-Location: /teams('cbf27e30-658b-4021-a8c6-4002b9adaf41')
很遗憾,我不知道如何访问这些信息。
您将通过调用 GET joined Teams 获取您的团队 ID,它将获取团队 ID、名称和描述
幸运的是,这很容易通过使用请求基础 class BaseRequest
并自己发送来实现。使用收到的 HttpResponseMessage
,您将获得 headers 记录 here,其中包含您的新团队的 ID。
该代码还包括如何使用忙等待等待团队创建完成——这不被视为最佳实践,但使示例更容易。更好的方法是存储团队 ID 并定期查询创建状态。
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...