Impossible de créer une équipe à partir de zéro via l'API Microsoft Graph

Oct 20 2020

Je suis ce document et j'ai essayé de créer une équipe dans le code https://docs.microsoft.com/en-us/graph/api/team-post?view=graph-rest-1.0&tabs=csharp%2Chttp. voici mes extraits de code:

var scopes = new string[] { "https://graph.microsoft.com/.default" };

            // Configure the MSAL client as a confidential client
            var confidentialClient = ConfidentialClientApplicationBuilder
                .Create(clientId)
                .WithTenantId(tenantId)
                .WithClientSecret(clientSecret)
                .Build();
 GraphServiceClient graphServiceClient =
                new GraphServiceClient(new DelegateAuthenticationProvider(async (requestMessage) =>
                {

                    // Retrieve an access token for Microsoft Graph (gets a fresh token if needed).
                    var authResult = await confidentialClient
                        .AcquireTokenForClient(scopes)
                        .ExecuteAsync();

                    // Add the access token in the Authorization header of the API request.
                    requestMessage.Headers.Authorization =
                        new AuthenticationHeaderValue("Bearer", authResult.AccessToken);
                })
                );

            // Make a Microsoft Graph API call
            var team = new Team
            {
                DisplayName = "My Sample Team",
                Description = "My Sample Team’s Description",
                AdditionalData = new Dictionary<string, object>()
    {
        {"[email protected]", "https://graph.microsoft.com/v1.0/teamsTemplates('standard')"},
        {"[email protected]", "[{\"@odata.type\":\"#microsoft.graph.aadUserConversationMember\",\"roles\":[\"owner\"],\"userId\":\"57d4fc1c-f0a3-1111-b41e-22229f05911c\"}]"}
    }
            };
 GraphServiceClient graphServiceClient =
                new GraphServiceClient(new DelegateAuthenticationProvider(async (requestMessage) =>
                {

                    // Retrieve an access token for Microsoft Graph (gets a fresh token if needed).
                    var authResult = await confidentialClient
                        .AcquireTokenForClient(scopes)
                        .ExecuteAsync();

                    // Add the access token in the Authorization header of the API request.
                    requestMessage.Headers.Authorization =
                        new AuthenticationHeaderValue("Bearer", authResult.AccessToken);
                })
                );

            // Make a Microsoft Graph API call
            var team = new Team
            {
                DisplayName = "My Sample Team",
                Description = "My Sample Team’s Description",
                AdditionalData = new Dictionary<string, object>()
    {
        {"[email protected]", "https://graph.microsoft.com/v1.0/teamsTemplates('standard')"},
        {"[email protected]", "[{\"@odata.type\":\"#microsoft.graph.aadUserConversationMember\",\"roles\":[\"owner\"],\"userId\":\"57d4fc1c-f0a3-4105-b41e-1ba89f05911c\"}]"}
    }
            };

mais obtenez cette erreur:

 "message": "Bind requests not supported for containment navigation property.",\r\n   

J'utilise la dernière Microsoft.Graphbibliothèque et la version estV3.1.8

Quelqu'un a-t-il des idées sur ce problème ou sur l'erreur de format odata?

Réponses

2 AllenWu Oct 20 2020 at 16:01

Il semble que le [email protected]est toujours en changement. Cela ne fonctionne pas actuellement.

Vous devez utiliser la memberspropriété.

POST https://graph.microsoft.com/v1.0/teams

{
  "[email protected]":"https://graph.microsoft.com/v1.0/teamsTemplates('standard')",
  "displayName":"My Sample Team555",
  "description":"My Sample Team’s Description555",
  "members":[
      {
         "@odata.type":"#microsoft.graph.aadUserConversationMember",
         "roles":[
            "owner"
         ],
         "userId":"9xxxxxc9-f062-48e2-8ced-22xxxxx6dfce"
      }
   ]
}

Le code C # correspondant doit être:

var team = new Team
            {
                DisplayName = "My Sample Team557",
                Description = "My Sample Team’s Description557",
                Members = (ITeamMembersCollectionPage)new List<ConversationMember>()
                {
                    new AadUserConversationMember
                    {
                        Roles = new List<String>()
                        {
                            "owner"
                        },
                        UserId = "9xxxxxc9-f062-48e2-8ced-22xxxxx6dfce"
                    }
                },
                AdditionalData = new Dictionary<string, object>()
                {
                    {"[email protected]", "https://graph.microsoft.com/v1.0/teamsTemplates('standard')"}
                }
            };

Malheureusement, lorsque j'exécute le code, cela montre:

System.InvalidCastException: 'Unable to cast object of type 'System.Collections.Generic.List`1[Microsoft.Graph.ConversationMember]' to type 'Microsoft.Graph.ITeamMembersCollectionPage'.'

Je ne peux pas le faire fonctionner. La solution de contournement consiste à utiliser httpClient pour envoyer la demande dans votre code.

Voir une question similaire ici .

METTRE À JOUR:

Je l'ai compris.

Vous pouvez essayer le code suivant:

        var team = new Team
        {
            DisplayName = "My Sample Team558",
            Description = "My Sample Team’s Description558",
            Members = new TeamMembersCollectionPage() {
                new AadUserConversationMember
                {
                    Roles = new List<String>()
                    {
                        "owner"
                    },
                    UserId = "9xxxxxc9-f062-48e2-8ced-22xxxxx6dfce"
                }
            },
            AdditionalData = new Dictionary<string, object>()
            {
                {"[email protected]", "https://graph.microsoft.com/v1.0/teamsTemplates('standard')"}
            }
        };

Si vous préférez la méthode httpClient, reportez-vous à ceci:

        string str = "{\"[email protected]\":\"https://graph.microsoft.com/v1.0/teamsTemplates('standard')\",\"displayName\":\"My Sample Team999\",\"description\":\"My Sample Team’s Description555\",\"members\":[{\"@odata.type\":\"#microsoft.graph.aadUserConversationMember\",\"roles\":[\"owner\"],\"userId\":\"9xxxxxc9-f062-48e2-8ced-22xxxxx6dfce\"}]}";

        var content = new StringContent(str, Encoding.UTF8, "application/json");

        HttpClient client = new HttpClient();
        
        client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
        var response = client.PostAsync("https://graph.microsoft.com/v1.0/teams", content).Result;

MISE À JOUR 2:

Si vous devez l'appeler dans Postman, utilisez ce format:

{
  "[email protected]":"https://graph.microsoft.com/v1.0/teamsTemplates('standard')",
  "displayName":"My Sample Team555",
  "description":"My Sample Team’s Description555",
  "members":[
      {
         "@odata.type":"#microsoft.graph.aadUserConversationMember",
         "roles":[
            "owner"
         ],
         "userId":"9xxxxxc9-f062-48e2-8ced-22xxxxx6dfce"
      }
   ]
}