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")); }