How to import a list of users from a text file
This article will show you how to import a list of users from a text file to automate the user definition. Text file definitionThe comma separated value file used in this article contains 6 fields and each field is separated by a semicolon “;” Example: mmathy;password;Michael Mathy;support@quickelsoft.com;1;Marketing einstein;password;Albert Einstein;einstein@quickelsoft.com;1;HR jdoe;password;John Doe;jdoe@quickelsoft.com;0;Readers rdoe;password;Richard Doe;rdoe@quickelsoft.com;1;Marketing In the sample file above, Richard Doe will be defined as a content contributor and will be added to the Marketing group. Codeusing System; using System.Web; using QuickelSoft.CMS.API; using System.IO;
namespace ImportUsers { class Program { static void Main(string[] args) { Context context = new Context(); String line; String[] token; String userName; String password; String fullName; String eMailAddress; String groupName; bool isContentContributor; User user; Group group; if (args.Length != 1) { Console.WriteLine("Usage: importusers filename"); return; } try { context.Login("MyUserId", "MyPassword"); SiteCollection sites = context.GetSites(); UserCollection users = context.GetUsers(); GroupCollection groups = context.GetGroups(); StreamReader reader = System.IO.File.OpenText(args[0]); while (!reader.EndOfStream) { line = reader.ReadLine(); token = line.Split(';'); userName = token[0]; password = token[1]; fullName = token[2]; eMailAddress = token[3]; isContentContributor = token[4].Equals("1"); groupName = token[5]; user = users.FromUserName(userName); if (user == null) { user = users.Create(); user.UserName = userName; } user.FullName = fullName; user.EMail = eMailAddress; user.Password = password; if (isContentContributor) user.UserType = UserType.ContentContributor; else user.UserType = UserType.SimpleUser; user.Save(); group = groups.FromName(groupName); if (group == null) { group = groups.Create(); group.Name = groupName; }
group.Add(user); group.Save(); Console.WriteLine("Users imported"); } } catch (QuickelSoft.CMS.API.Exceptions.CMSException) { Console.WriteLine(context.LastErrorDescription + " [" + context.LastError + "]"); } catch (QuickelSoft.CMS.API.Exceptions.TooManyUsersException) { Console.WriteLine("Too many content contributors created"); } catch (Exception ex) { Console.WriteLine(ex.Message + "\n" + ex.StackTrace); } } } } Code explanation1. The code creates a Context object and login as an administrator. Context context = new Context(); context.Login(“MyUserId”, “MyPassword”); 2. A UserCollection object and a GroupCollection object are created. These two objects are used to create or find users or groups in the QuickelSoft CMS domain. GroupCollection groups = context.GetGroups(); UserCollection users = context.GetUsers(); 3. The CSV file is opened and each line is split using a semicolon as token separator. 4. The program first tries to find the user in the domain. If it does not exist, it creates one. user = users.FromUserName(userName); if (user == null) { user = users.Create(); user.UserName = userName; } 5. The properties are set and the user is saved. user.FullName = fullName; user.EMail = eMailAddress; user.Password = password; if (isContentContributor) user.UserType = UserType.ContentContributor; else user.UserType = UserType.SimpleUser; 6. Then the group to which the user belongs is created or updated. group = groups.FromName(groupName); if (group == null) { group = groups.Create(); group.Name = groupName; } group.Add(user); group.Save();
|