public static Handle ImportUsers(string userSheetItemId, string database, bool downloadFile, bool ignoreDate) { UserImportStatus importStatus = new UserImportStatus(); UserImportOptions options = new UserImportOptions() { CsvSheetItemId = userSheetItemId, Database = database, DownloadLatestFile = downloadFile, IgnoreFileDate = ignoreDate }; JobOptions options1 = new JobOptions("ImportCsvUsers", "UserImportManager", "shell", (object)new UserImportManager(), "DoImport", new object[2] { (object) options, (object) importStatus }); options1.ContextUser = Context.User; options1.ClientLanguage = Context.Language; options1.AfterLife = TimeSpan.FromMilliseconds(1000); options1.Priority = ThreadPriority.Normal; options1.ExecuteInManagedThreadPool = true; importStatus.State = JobState.Queued; Job job = JobManager.Start(options1); job.Options.CustomData = (object)importStatus; return job.Handle; }
public void DoImport(UserImportOptions options, UserImportStatus status) { status.State = JobState.Initializing; try { UserCSVSheetItem userCsvSheetItem = Factory.GetDatabase(options.Database).GetItem(options.CsvSheetItemId); status.State = JobState.Running; if (options.DownloadLatestFile) { userCsvSheetItem.DownloadLatestCsv(options.IgnoreFileDate); } FileInfo file = new FileInfo(userCsvSheetItem.CsvFileFullPath); if (!file.Exists) { status.ErrorMessage = userCsvSheetItem.FileName + ": File cannot be found."; } status.CsvFileName = file.FullName; status.CsvFileDate = file.LastWriteTime; userCsvSheetItem.ImportUsersFromCsv(options.IgnoreFileDate, status); } catch (Exception e) { status.ErrorMessage = e.Message; Log.Error("Error importing users", e, this); } status.State = JobState.Finished; }
/// <summary> /// /// </summary> /// <param name="role">The role name to add to users processed.</param> /// <param name="profile">The Custom Profile item from the Core DB to use for the user's profile.</param> public UserManager(string role, Item profile, UserImportStatus status) { if (string.IsNullOrEmpty(role)) { throw new ArgumentException("Role must be specified."); } if (profile == null) { throw new ArgumentException("Custom profile Item must be provided."); } _roleName = AddDomain(role); _customProfileItem = profile; Status = status; }
public void ImportUsersFromCsv(bool ignoreDate, UserImportStatus statusObject) { // Check parameters if (string.IsNullOrEmpty(CsvFileFullPath)) { throw new ArgumentNullException("FileName", "File Name must be specified."); } var loader = new CsvFileLoader(CsvFileFullPath); if (!loader.CsvFile.Exists) { throw new FileNotFoundException("Csv file could not be loaded.", CsvFileFullPath); } if (LastUpdated.DateTime != DateTime.MinValue && LastUpdated.DateTime >= loader.CsvFile.LastWriteTime) { Log.Info(string.Format("File {0} not modified since the last import was run. Skipping UserCsvSheet item {1}.", FileName.Raw, this.Name), this); if (!ignoreDate) { return; } } // Load the File var mapped = loader.GetUsersFromCsv(); // Parse the mapped users var users = LoadMappedUsers(mapped); // Take the timestamp DateTime editStartTime = loader.CsvFile.LastWriteTime; // Load the users var um = new UserManager(Role.Raw, CustomProfileItem, statusObject); um.CreateUsers(users.ToList()); // Update the File Date using (new SecurityDisabler()) { InnerItem.Editing.BeginEdit(); LastUpdated.Field.InnerField.SetValue(DateUtil.ToIsoDate(editStartTime, false), false); InnerItem.Editing.EndEdit(); } }