protected override void ExecuteCmdlet()
        {
            bool forceCreation;

            if (!Force)
            {
                var existingGroup = UnifiedGroupsUtility.ListUnifiedGroups(AccessToken,
                                                                           mailNickname: MailNickname,
                                                                           endIndex: 1).Any();

                forceCreation = !existingGroup || ShouldContinue(string.Format(Resources.ForceCreationOfExistingGroup0, MailNickname), Resources.Confirm);
            }
            else
            {
                forceCreation = true;
            }

            if (forceCreation)
            {
                var group = UnifiedGroupsUtility.CreateUnifiedGroup(
                    DisplayName,
                    Description,
                    MailNickname,
                    AccessToken,
                    Owners,
                    Members,
                    GroupLogoPath,
                    IsPrivate);

                WriteObject(group);
            }
        }
        public void Initialize()
        {
            _accessToken = TestCommon.AcquireTokenAsync("https://graph.microsoft.com");

            TestCommon.FixAssemblyResolving("Newtonsoft.Json");

            var random = new Random();

            _groupId = UnifiedGroupsUtility.CreateUnifiedGroup("PnPDeletedUnifiedGroup test", "PnPDeletedUnifiedGroup test", $"pnp-unit-test-{random.Next(1, 1000)}", _accessToken, groupLogo: null).GroupId;

            UnifiedGroupsUtility.DeleteUnifiedGroup(_groupId, _accessToken);
        }
        protected override void ExecuteCmdlet()
        {
            if (MailNickname.Contains(" "))
            {
                throw new ArgumentException("MailNickname cannot contain spaces.");
            }
            bool forceCreation;

            if (!Force)
            {
                var candidates = UnifiedGroupsUtility.GetUnifiedGroups(AccessToken,
                                                                       mailNickname: MailNickname,
                                                                       endIndex: 1);
                // ListUnifiedGroups retrieves groups with starts-with, so need another check
                var existingGroup = candidates.Any(g => g.MailNickname.Equals(MailNickname, StringComparison.CurrentCultureIgnoreCase));

                forceCreation = !existingGroup || ShouldContinue(string.Format(Resources.ForceCreationOfExistingGroup0, MailNickname), Resources.Confirm);
            }
            else
            {
                forceCreation = true;
            }

            if (forceCreation)
            {
                var group = UnifiedGroupsUtility.CreateUnifiedGroup(
                    displayName: DisplayName,
                    description: Description,
                    mailNickname: MailNickname,
                    accessToken: AccessToken,
                    owners: Owners,
                    members: Members,
                    groupLogoPath: GroupLogoPath,
                    isPrivate: IsPrivate,
                    createTeam: CreateTeam);

                WriteObject(group);
            }
        }
        public static async Task <HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequestMessage req, TraceWriter log)
        {
            //Set up the results to return to caller
            CreateGroupResponse result = new CreateGroupResponse()
            {
                CreatedChannels = new List <CreateChannelResponse>()
            };

            try
            {
                var content = await req.Content.ReadAsStringAsync();

                log.Info($"Function was triggered with the following payload { content }");

                try
                {
                    var request = JsonConvert.DeserializeObject <CreateGroupRequest>(content);
                    log.Info("Successfully deserialized Json data");

                    //Get Azure OAuth access token
                    string accessToken = await(new GraphApiUtility(log)).GetGraphApiAuthenticationToken();

                    List <string> owners = new List <string>();
                    owners.Add(request.OwnerUpn);
                    // Graph API create team only supports delegated access permission.  Must add service principal to owner of the Group
                    string teamServicePrincipal = KeyVaultUtility.GetSecret(ConfigurationManager.AppSettings["kv:ServicePrincipalNameSecretName"], log).Result;
                    if (teamServicePrincipal.ToLower() != request.OwnerUpn.ToLower())
                    {
                        owners.Add(teamServicePrincipal);
                    }

                    //Use Pnp to create a new Group
                    var newGroup = UnifiedGroupsUtility.CreateUnifiedGroup(displayName: request.Displayname,
                                                                           description: request.Description,
                                                                           mailNickname: request.EmailAlias,
                                                                           accessToken: accessToken,
                                                                           owners: owners.ToArray(),
                                                                           members: owners.ToArray(),
                                                                           isPrivate: request.IsPrivate,
                                                                           retryCount: 10,
                                                                           delay: 500);

                    //Set up the results to return to caller
                    result.GroupCreated = true;
                    result.GroupId      = new Guid(newGroup.GroupId);
                    result.GroupSiteUrl = newGroup.SiteUrl;

                    if (request.CreateTeam)
                    {
                        //Add apps such as Team (& Channel), Planner, and OneNote to Group
                        await AddAppsToTeam(request, result.GroupId, result, log);
                    }

                    return(new HttpResponseMessage()
                    {
                        StatusCode = HttpStatusCode.OK,
                        Content = new StringContent(JsonConvert.SerializeObject(result), System.Text.Encoding.UTF8, "application/json")
                    });
                }
                catch (JsonReaderException jsonError)
                {
                    log.Error($"Json parsing error in incoming request: {jsonError.Message}");

                    result.GroupCreated = false;
                    result.Message      = $"Invalid Json request: { jsonError.Message }";

                    return(new HttpResponseMessage()
                    {
                        StatusCode = HttpStatusCode.InternalServerError,
                        Content = new StringContent(JsonConvert.SerializeObject(result), System.Text.Encoding.UTF8, "application/json")
                    });
                }
            }
            catch (Exception ex)
            {
                log.Error(ex.ToDetailedString());
                result.GroupCreated = false;
                result.Message      = $"Error: { ex.ToDetailedString() }";
                return(new HttpResponseMessage()
                {
                    StatusCode = HttpStatusCode.InternalServerError,
                    Content = new StringContent(JsonConvert.SerializeObject(result), System.Text.Encoding.UTF8, "application/json")
                });
            }
        }