public static async Task BulkCreateFromCsv(AppSettings config, GraphServiceClient graphClient, List <SpreadsheetModelUserCreated> responses) { // Get the users to import // string appDirectoryPath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location); // string dataFilePath = Path.Combine(appDirectoryPath, config.UsersFileName); // // Verify and notify on file existence // if (!System.IO.File.Exists(dataFilePath)) // { // Console.ForegroundColor = ConsoleColor.Red; // Console.WriteLine($"File '{dataFilePath}' not found."); // Console.ResetColor(); // Console.ReadLine(); // return; // } Console.WriteLine("Starting bulk create operation..."); // Read the data file and convert to object // UsersModel users = UsersModel.Parse(System.IO.File.ReadAllText(dataFilePath)); var users = SpreadsheetService.GetUserModelListFromCSV(); foreach (var user in users) { user.SetB2CProfile(config.TenantId); try { // Create the user account in the directory User user1 = await graphClient.Users .Request() .AddAsync(user); var result = new SpreadsheetModelUserCreated { FirstName = user.GivenName, SurName = user.Surname, Email = user.Identities.FirstOrDefault().IssuerAssignedId, ObjectId = user1.Id }; Console.WriteLine($"User '{user.DisplayName}' successfully created."); responses.Add(result); Console.Write(string.Empty); } catch (Exception ex) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine(ex.Message); Console.ResetColor(); } } }
static async Task Main(string[] args) { // Read application settings from appsettings.json (tenant ID, app ID, client secret, etc.) AppSettings config = AppSettingsFile.ReadFromJsonFile(); // Initialize the client credential auth provider IConfidentialClientApplication confidentialClientApplication = ConfidentialClientApplicationBuilder .Create(config.AppId) .WithTenantId(config.TenantId) .WithClientSecret(config.ClientSecret) .Build(); ClientCredentialProvider authProvider = new ClientCredentialProvider(confidentialClientApplication); // Set up the Microsoft Graph service client with client credentials GraphServiceClient graphClient = new GraphServiceClient(authProvider); // Create object to store create user responses List <SpreadsheetModelUserCreated> responses = new List <SpreadsheetModelUserCreated>(); PrintCommands(); try { while (true) { Console.Write("Enter command, then press ENTER: "); string decision = Console.ReadLine(); switch (decision.ToLower()) { case "1": await UserService.ListUsers(graphClient); break; case "2": await UserService.GetUserById(graphClient); break; case "3": await UserService.GetUserBySignInName(config, graphClient); break; case "4": await UserService.DeleteUserById(graphClient); break; case "5": await UserService.SetPasswordByUserId(graphClient); break; case "6": await UserService.BulkCreate(config, graphClient); break; case "7": await UserService.CreateUserWithCustomAttribute(graphClient, config.B2cExtensionAppClientId, config.TenantId); break; case "8": await UserService.ListUsersWithCustomAttribute(graphClient, config.B2cExtensionAppClientId); break; case "9": await UserService.BulkCreateFromCsv(config, graphClient, responses); SpreadsheetService.SaveResponseDataAsCSV(responses); break; case "help": Program.PrintCommands(); break; case "exit": return; default: Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("Invalid command. Enter 'help' to show a list of commands."); Console.ResetColor(); break; } Console.ResetColor(); } } catch (Exception ex) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine($"An error occurred: {ex}"); Console.ResetColor(); } Console.ReadLine(); }