public async Task AddGroupsAndUsersAsync(GraphService graphService)
        {
            List <string> newUsers = new List <string>();

            #region Create AD Group
            var groupsDict = new Dictionary <string, Graph.Group>();

            XmlNode groupItems = sampleData.SelectSingleNode("//List[@name='AD Groups']");

            foreach (XmlNode item in groupItems.ChildNodes)
            {
                string displayName  = item.Attributes["DisplayName"].Value;
                string description  = item.Attributes["Description"].Value;
                string mailNickname = item.Attributes["MailNickname"].Value;

                var group = await GraphServiceExtension.GetGroupByDisplayNameAsync(graphService, displayName);

                if (group == null)
                {
                    group = await GraphServiceExtension.AddGroupAsync(graphService, mailNickname, displayName, description);
                }
                groupsDict.Add(displayName, group as Graph.Group);
            }
            #endregion

            #region Create AD Users
            XmlNode userItems = sampleData.SelectSingleNode("//List[@name='AD Users']");
            foreach (XmlNode item in userItems)
            {
                string displayName         = item.Attributes["DisplayName"].Value;
                string userPrincipalName   = item.Attributes["PrincipalName"].Value;
                string password            = item.Attributes["Password"].Value;
                string ownGroupDisplayName = item.Attributes["GroupsDisplayName"].Value;

                Graph.IUser user = await GraphServiceExtension.GetFirstUserAsync(graphService, i => i.displayName == displayName);

                if (user == null)
                {
                    user = await GraphServiceExtension.AddUserAsync(graphService, userPrincipalName, displayName, password);

                    await GraphServiceExtension.AssignLicenseAsyncViaHttpClientAsync(graphService, user as Graph.User);
                }

                if (groupsDict.ContainsKey(ownGroupDisplayName) &&
                    (await GraphServiceExtension.GetGroupMembersAsync(graphService, ownGroupDisplayName))
                    .Where(i => i.userPrincipalName == user.userPrincipalName).Count() == 0)
                {
                    groupsDict[ownGroupDisplayName].members.Add(user as Graph.User);
                    await groupsDict[ownGroupDisplayName].SaveChangesAsync();
                }

                newUsers.Add(user.userPrincipalName);
            }
            #endregion

            AddUsersToSPGroup(newUsers);
        }
        public async Task <ActionResult> TestAssignLicenseByHttpClient()
        {
            var service = await AuthenticationHelper.GetGraphServiceAsync();

            Graph.IUser user = await GraphServiceExtension.AddUserAsync(service, "testUser", "Test User", "password2468!");

            await GraphServiceExtension.AssignLicenseAsyncViaHttpClientAsync(service, user as Graph.User);

            return(Content("Success"));
        }
        public async Task AddGroupsAndUsersAsync(GraphServiceClient graphService, string graphAccessToken)
        {
            List <string> newUsers = new List <string>();

            #region Create AD Group

            var groupsDict = new Dictionary <string, Graph.Group>();

            XmlNode groupItems = sampleData.SelectSingleNode("//List[@name='AD Groups']");

            foreach (XmlNode item in groupItems.ChildNodes)
            {
                string displayName  = item.Attributes["DisplayName"].Value;
                string description  = item.Attributes["Description"].Value;
                string mailNickname = item.Attributes["MailNickname"].Value;

                var group = await GraphServiceExtension.GetGroupByDisplayNameAsync(graphService, displayName);

                if (group == null)
                {
                    group = await GraphServiceExtension.AddGroupAsync(graphService, mailNickname, displayName, description);
                }
                groupsDict.Add(displayName, group as Graph.Group);
            }

            #endregion

            #region Create AD Users

            XmlNode userItems = sampleData.SelectSingleNode("//List[@name='AD Users']");
            foreach (XmlNode item in userItems)
            {
                string displayName         = item.Attributes["DisplayName"].Value;
                string userPrincipalName   = item.Attributes["PrincipalName"].Value;
                string password            = item.Attributes["Password"].Value;
                string ownGroupDisplayName = item.Attributes["GroupsDisplayName"].Value;
                string jobTitle            = item.Attributes["JobTitle"].Value;
                string companyName         = item.Attributes["CompanyName"].Value;
                string officeLocation      = item.Attributes["OfficeLocation"].Value;
                string mobilePhone         = item.Attributes["MobilePhone"].Value;
                string businessPhone       = item.Attributes["BusinessPhone"].Value;

                var        queryUser = (await graphService.Users.Request().Filter(string.Format("displayName eq '{0}'", displayName)).GetAsync()).CurrentPage;//await GraphServiceExtension.GetFirstUserAsync(graphService, i => i.DisplayName == displayName);
                Graph.User user      = queryUser.Count > 0 ? queryUser[0] : null;
                if (user == null)
                {
                    List <string> bp = new List <string>();
                    bp.Add(businessPhone);
                    user = new Graph.User
                    {
                        DisplayName       = displayName,
                        UserPrincipalName = userPrincipalName + "@" + AppSettings.DemoSiteCollectionOwner.Split('@')[1],
                        PasswordProfile   = new PasswordProfile
                        {
                            Password = password,
                            ForceChangePasswordNextSignIn = false
                        },
                        UsageLocation  = "US",
                        AccountEnabled = true,
                        MailNickname   = userPrincipalName,
                        JobTitle       = jobTitle,
                        Department     = companyName,
                        OfficeLocation = officeLocation,
                        BusinessPhones = bp
                    };

                    user = await GraphServiceExtension.AddUserAsync(graphService, user);

                    await GraphServiceExtension.AssignLicenseAsyncViaHttpClientAsync(graphService, graphAccessToken, user as Graph.User);
                }

                if (groupsDict.ContainsKey(ownGroupDisplayName) &&
                    (await GraphServiceExtension.GetGroupMembersAsync(graphService, ownGroupDisplayName))
                    .Where(i => i.UserPrincipalName == user.UserPrincipalName).Count() == 0)
                {
                    try
                    {
                        await GraphServiceExtension.AddUserToGroupMembersAsync(graphService, groupsDict[ownGroupDisplayName], user, graphAccessToken);
                    }
                    catch { }
                }

                newUsers.Add(user.UserPrincipalName);
            }

            #endregion

            AddUsersToSPGroup(newUsers);
        }