[Client bug]: Creating a new Team (Teams.Request().AddAsync) always returns null
Describe the bug The "AddAsync" method of Teams always returns NULL value, I was trying to get the ID of the new team after creating it.
My code was like:
string ownerID = ConfigurationManager.AppSettings["DefaultChannelOwnerID"];
var members = new TeamMembersCollectionPage();
members.Add(new AadUserConversationMember()
{
Roles = new List<String>()
{
"owner"
},
AdditionalData = new Dictionary<string, object>()
{
{"[email protected]", "https://graph.microsoft.com/v1.0/users('"+ ownerID +"')"}
}
});
var team = new Team()
{
DisplayName = teamName,
Members = members,
AdditionalData = new Dictionary<string, object>()
{
{"[email protected]", "https://graph.microsoft.com/v1.0/teamsTemplates('standard')"}
}
};
var result = await graphClient.Teams.Request().AddAsync(team);
return result.Id;
I even tried this:
var team = new Team()
{
DisplayName = teamName,
Members = members,
AdditionalData = new Dictionary<string, object>()
{
{"[email protected]", "https://graph.microsoft.com/v1.0/teamsTemplates('standard')"}
}
};
await graphClient.Teams.Request().AddAsync(team);
return team.Id;
In both cases, I could not retrieve the team id.
This POST API doesn't return the created Team information in the body so there is no team to deserialize from the response. And since we don't have an object to return, we haven't provided access to the information in the response headers which does have the team identifier in it.
@andrueastman If you haven't already, as part of the 2.0 work, we should consider returning a GraphResponse
@gunner13 As a workaround, you'll need to do something like this:
#var result = await graphClient.Teams.Request().AddAsync(team);
var httpRequestMessage = client.Teams.Request().GetHttpRequestMessage();
httpRequestMessage.Content = client.HttpProvider.Serializer.SerializeAsJsonContent(team);
var httpResponseMessage = await client.HttpProvider.SendAsync(httpRequestMessage);
if (httpResponseMessage.IsSuccessStatusCode)
{
IEnumerable<string> headerValues;
if (httpResponseMessage.Headers.TryGetValues("Content-Location", out headerValues))
{
string teamid = headerValues.First().Split('/', 2, StringSplitOptions.RemoveEmptyEntries)[1];
}
}
@MIchaelMainer This has already been done https://github.com/microsoftgraph/msgraph-sdk-dotnet-core/blob/eb8ce364eb9c95b027a77ec83166bd402b80d50f/src/Microsoft.Graph.Core/Requests/GraphResponse.cs#L42
@MIchaelMainer Facing the same problem as gunner13 I tried to replace my AddAsync(team) call with your workaround code but unfortunately "await client.HttpProvider.SendAsync(httpRequestMessage)" never returns even when trying to create a basic name-only Team.
The following code works for me, I added the POST method and evaluate the returned location (with different format) header because the "Content-Location" header field is missing in the response.
var httpRequestMessage = graphClient.Teams.Request().GetHttpRequestMessage();
httpRequestMessage.Method = HttpMethod.Post;
httpRequestMessage.Content = graphClient.HttpProvider.Serializer.SerializeAsJsonContent(team);
var httpResponseMessage = await graphClient.HttpProvider.SendAsync(httpRequestMessage);
if (httpResponseMessage.IsSuccessStatusCode)
{
if (httpResponseMessage.Headers.TryGetValues("Location", out var headerValues))
{
var teamId = headerValues?.First().Split('\'', StringSplitOptions.RemoveEmptyEntries)[1];
}
}
Any update as to the status of this bug? I'm still experiencing this exact issue on the current version of the product.
Closing this as the new v4 previews provide direct access to the response headers and status codes. More info in the guide here and https://github.com/microsoftgraph/msgraph-sdk-dotnet/issues/935.
Ideally, in this scenario, SDK users can now make a call that looks something like this.
var team = new Team()
{
DisplayName = "teamName",
Members = new TeamMembersCollectionPage(),
AdditionalData = new Dictionary<string, object>()
{
{"[email protected]", "https://graph.microsoft.com/v1.0/teamsTemplates('standard')"}
}
};
var teamResponse = await graphClient.Teams.Request().AddResponseAsync(team);
if (teamResponse.HttpHeaders.TryGetValues("Location", out var headerValues))
{
var teamId = headerValues?.First().Split('\'', StringSplitOptions.RemoveEmptyEntries)[1];
}
Closing this as the new v4 previews provide direct access to the response headers and status codes. More info in the guide here and #935.
Ideally, in this scenario, SDK users can now make a call that looks something like this.
var team = new Team() { DisplayName = "teamName", Members = new TeamMembersCollectionPage(), AdditionalData = new Dictionary<string, object>() { {"[email protected]", "https://graph.microsoft.com/v1.0/teamsTemplates('standard')"} } }; var teamResponse = await graphClient.Teams.Request().AddResponseAsync(team); if (teamResponse.HttpHeaders.TryGetValues("Location", out var headerValues)) { var teamId = headerValues?.First().Split('\'', StringSplitOptions.RemoveEmptyEntries)[1]; }
I guess, but it seems counter productive to return a team object that doesn't have the ID of the newly created team, then have to make a completely separate call to grab the id out of a header value which eliminates the whole usefulness of an SDK. If you're creating the team in the first call, and already have the logic to return the team, why not just return the team with the ID present?
Creating a Team actually returns a teamsAsyncOperation.
You then use the information in that response to poll for completion. https://docs.microsoft.com/en-us/graph/api/resources/teamsasyncoperation?view=graph-rest-1.0
The fact that the Id in the Location header matches the eventually-created Team is not part of the API and shouldn't necessarily be assumed to always match.
The SDK does not really support the async operation pattern (at least in V3).