public static async Task <string> CreateTeam(Beta.GraphServiceClient client, Beta.Team team, CancellationToken token)
        {
            TeamsAsyncOperation result = await GraphHelperTeams.SubmitTeamCreateRequestAndWait(client, team, token);

            if (result.Status == TeamsAsyncOperationStatus.Succeeded)
            {
                return(result.TargetResourceId);
            }

            string serializedResponse = JsonConvert.SerializeObject(result);

            logger.Error($"Team creation failed\r\n{serializedResponse}");

            throw new ServiceException(new Error()
            {
                Code = result.Error.Code, AdditionalData = result.Error.AdditionalData, Message = result.Error.Message
            });
        }
        /// <summary>
        /// Update the navigation property operations in groups
        /// <param name="body"></param>
        /// <param name="requestConfiguration">Configuration for the request such as headers, query parameters, and middleware options.</param>
        /// </summary>
        public RequestInformation CreatePatchRequestInformation(TeamsAsyncOperation body, Action <TeamsAsyncOperationItemRequestBuilderPatchRequestConfiguration> requestConfiguration = default)
        {
            _ = body ?? throw new ArgumentNullException(nameof(body));
            var requestInfo = new RequestInformation {
                HttpMethod     = Method.PATCH,
                UrlTemplate    = UrlTemplate,
                PathParameters = PathParameters,
            };

            requestInfo.SetContentFromParsable(RequestAdapter, "application/json", body);
            if (requestConfiguration != null)
            {
                var requestConfig = new TeamsAsyncOperationItemRequestBuilderPatchRequestConfiguration();
                requestConfiguration.Invoke(requestConfig);
                requestInfo.AddRequestOptions(requestConfig.Options);
                requestInfo.AddHeaders(requestConfig.Headers);
            }
            return(requestInfo);
        }
Пример #3
0
        public static async Task <Team> CreateNewTeam(string name)
        {
            string teamId = null;
            var    team   = new Team
            {
                DisplayName = name,
                Description = "My Sample Team’s Description",
                Visibility  = TeamVisibilityType.Public,
                Channels    = new TeamChannelsCollectionPage()
                {
                    new Channel
                    {
                        DisplayName         = "Announcements 📢",
                        IsFavoriteByDefault = true,
                        Description         = "This is a sample announcements channel that is favorited by default. Use this channel to make important team, product, and service announcements."
                    }
                },
                MemberSettings = new TeamMemberSettings
                {
                    AllowCreateUpdateChannels         = true,
                    AllowDeleteChannels               = true,
                    AllowAddRemoveApps                = true,
                    AllowCreateUpdateRemoveTabs       = true,
                    AllowCreateUpdateRemoveConnectors = true
                },
                GuestSettings = new TeamGuestSettings
                {
                    AllowCreateUpdateChannels = false,
                    AllowDeleteChannels       = false
                },
                FunSettings = new TeamFunSettings
                {
                    AllowGiphy            = true,
                    GiphyContentRating    = GiphyRatingType.Moderate,
                    AllowStickersAndMemes = true,
                    AllowCustomMemes      = true
                },
                MessagingSettings = new TeamMessagingSettings
                {
                    AllowUserEditMessages    = true,
                    AllowUserDeleteMessages  = true,
                    AllowOwnerDeleteMessages = true,
                    AllowTeamMentions        = true,
                    AllowChannelMentions     = true
                },
                AdditionalData = new Dictionary <string, object>()
                {
                    { "*****@*****.**", "https://graph.microsoft.com/v1.0/teamsTemplates('standard')" }
                },
                Members = new TeamMembersCollectionPage()
                {
                    new AadUserConversationMember
                    {
                        Roles = new List <String>()
                        {
                            "owner"
                        },
                        AdditionalData = new Dictionary <string, object>()
                        {
                            { "*****@*****.**", "https://graph.microsoft.com/v1.0/users('6ae941d0-1625-4182-8a61-30d58f83a092')" }
                        }
                    }
                },
                InstalledApps = new TeamInstalledAppsCollectionPage()
                {
                    new TeamsAppInstallation
                    {
                        AdditionalData = new Dictionary <string, object>()
                        {
                            { "*****@*****.**", "https://graph.microsoft.com/v1.0/appCatalogs/teamsApps('com.microsoft.teamspace.tab.vsts')" }
                        }
                    },
                    new TeamsAppInstallation
                    {
                        AdditionalData = new Dictionary <string, object>()
                        {
                            { "*****@*****.**", "https://graph.microsoft.com/v1.0/appCatalogs/teamsApps('1542629c-01b3-4a6d-8f76-1938b779e48d')" }
                        }
                    }
                }
            };

            try
            {
                //Unfortunately, this always returns 'null', so we have to do it more complicated >:(
                //newTeam = await graphClient.Teams.Request().AddAsync(team);

                //StackOverflow: 64667655
                BaseRequest request = (BaseRequest)graphClient.Teams.Request();
                request.ContentType = "application/json";
                request.Method      = "POST";

                string location;
                using (HttpResponseMessage response = await request.SendRequestAsync(team, 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);
                    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 graphClient.Teams[teamId].Operations[operationId].Request().GetAsync();
                        if (operation.Status == TeamsAsyncOperationStatus.Succeeded)
                        {
                            break;
                        }

                        if (operation.Status == TeamsAsyncOperationStatus.Failed)
                        {
                            throw new Exception($"Failed to create team '{team.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;
                    }

                    //we can now work with the Id
                    team.Id = teamId;
                }
            }
            catch (ServiceException ex)
            {
                Console.WriteLine($"Error creating team: {ex.Message}");
            }

            return(team);
        }