Writing a User Creation Wizard Data Source

We're going to look at the code to the Manual Data Entry data source to demonstrate how to write a data source plugin for User Creation Wizard.  For this, you'll need:

  • A copy of Microsoft Visual Studio 2010 (Express will do)
  • Some familiarity with C#
  • User Creation Wizard

Start a new .NET 4 (not Client Profile) Class Library in Visual Studio, then bring up the Add References window for your project and choose Trogsoft.DsTools.Ucw.dll from the path C:\Program Files (x86)\Trogsoft Ltd\ADStudio\User Creation Wizard (or wherever you installed User Creation Wizard).

Add Reference

Next, rename the default class from Class1 to something meaningful, and add Trogsoft.DsTools.Ucw to your using statements at the top of the code, as shown below.  Make sure your class inherits from UcwDataSource, which is an abstract class providing some default functionality.  You can right click on 'UcwDataSource' and have Visual Studio fill in some code for you.

There are a number of methods and properties you can implement, and some you must implement:

  • Name (property, must override) - provide the name of the data source to User Creation Wizard.
  • GetSelectionInterface() (method, must override) - returns a UserControl object to User Creation Wizard which the program will embed into a wizard page.
  • GetRecordCount() (method, must override) - returns the number of user records found in whatever data source you are using.  For our manual data source, it will always be one.
  • GetRecords() (method, must override) - returns the user data found in whatever data source you are using. 
  • PerformDataMapping (property, optional, default: true) - Tells User Creation Wizard whether the user needs to map data found in your data source to user properties.  In our case, it does not.
  • ReadyToAdvance (property, optional, default: true) - Tells User Creation Wizard if it can move to the next page of the wizard.

But before we do that, we'll create a User Control to contain the interface the user will enter data into, and it looks something like this:

In the Manual Data Source which ships with User Creation Wizard, we have created a private field in our ManualDataSource class which holds a reference to an instance of this user control using code something like this

 private ManualDataEntry theInterface = new ManualDataEntry();

'ManualDataEntry' is the name of the control we created.  Having this field enables us to quickly implement the 'GetSelectionInterface()' method, like so:

public override UserControl GetSelectionInterface() {
    return theInterface;
}

While we're coding, we might as well also fill in the other easy ones.

public override string Name {
    get { return "Manual Data Entry"; }
} public override int GetRecordCount() {
    return 1;
}
  public override bool PerformDataMapping {
    get {
        return false;
    }
}

Here we've told User Creation Wizard the name of our data source ('Manual Data Entry'), and how many records it returns (always one) as well as the fact we don't need to perform any mapping of values to user properties.  Next we need to do a little work on our data entry interface.

First, we'll define a public method called 'ReadyToAdvance()' which will determine if all conditions have been met to advance to the next page in User Creation Wizard.  You can call this anything you like, as User Creation Wizard will not directly look at this.  We are making it public so that our MyDataSource class can call it.

public bool ReadyToAdvance() {
    if (String.IsNullOrWhiteSpace(foreName.Text){
        MessageBox.Show("You must enter a forename.");
        return false;
    }     if (String.IsNullOrWhiteSpace(surname.Text)) {
        MessageBox.Show("You must enter a surname.");
        return false;
    }     if (String.IsNullOrWhiteSpace(fullName.Text)) {
        MessageBox.Show("You must enter a full name.");
        return false;
    }     if (!generateUsername.Checked && String.IsNullOrWhiteSpace(logonName.Text)) {
        MessageBox.Show("You must enter a logon name, or choose to automatically generate one.");
        return false;
    }     if (!generatePassword.Checked && String.IsNullOrWhiteSpace(password1.Text)) {
        MessageBox.Show("You must enter a password, or choose to automatically generate one.");
        return false;
    }     if (!generatePassword.Checked && password1.Text != password2.Text) {
        MessageBox.Show("The two passwords entered do not match.");
        return false;
    }     return true; }

Here, we check to make sure that a first name, last name and full name have been entered and that a username and password have been entered if the options to generate them automatically are turned off.  We also check that the two passwords entered match.  If all these conditions are met, we return true.  Going back to our MyDataSource class for a moment, we can now override another of the UcwDataSource methods - ReadyToAdvance.

public override bool ReadyToAdvance {
    get {
        return theInterface.ReadyToAdvance();
    }
}

User Creation Wizard will call this method, and it in turn calls the method we just defined in the ManualDataEntry class. 

User Creation Wizard needs to know what data is being returned to it, and there are a number of properties which take care of this.  They are all defined as virtual properties in UcwDataSource and do not need overriding, however we are going to override the HasUsername and HasPassword properties because our data source allows these properties to be specified if the user wants to specify them.  We'll do the same thing we did for the ReadyToAdvance property - define a couple of helper properties in the ManualDataEntry class first.

 public bool HasPassword {
    get {
        return !generatePassword.Checked;
    }
}
public bool HasUsername {
    get {
        return !generateUsername.Checked;
    }
}

This just returns the opposite of the value of the relevant checkbox.  If the 'Generate Username' checkbox is checked, then there is no username in our data source and User Creation Wizard must generate one itself.  The same rules apply for passwords.

In our MyDataSource class, we now override some more properties.

 public override bool HasPassword {
    get {
        return theInterface.HasPassword;
    }
}
public override bool HasUsername {
    get {
        return theInterface.HasUsername;
    }
}

Again, these properties just reference the properties we just defined in the ManualDataEntry UserControl class and tell User Creation Wizard whether there is data it can use for the Username and Password property of the user objects it will create, or whether it must generate them for itself in some way.

We only need to write one more method in the ManualDataEntry UserControl class before we're done (you can make the input logic work for yourself):

public Ucw.UcwSourceRecord GetRecord() {
    Ucw.UcwSourceRecord r = new Ucw.UcwSourceRecord();
    r.AdLocation = null;
    r.Forename = foreName.Text;
    r.Initial = initials.Text;
    r.Surname = surname.Text;
    r.Fullname = fullName.Text;
    r.HomeDirectory = null;
    r.HomeDrive = null;
    r.Mail = mail.Text != "" ? mail.Text : null;
    r.Password = (!generatePassword.Checked ? password1.Text : null);
    r.ProfilePath = null;
    r.ScriptPath = null;
    r.Title = title.Text != "" ? title.Text : null;
    r.Username = (!generateUsername.Checked ? logonName.Text : null);
    return r; }

This method will create a UcwSourceRecord class and fill it with information specified in the user interface of the ManualDataEntry control.  We set properties we're not using to null just to be explicit about what we're doing, and set the Forename, Initial, Surname, FullName, Mail, Password and Username properties to the appropriate values.

Finally, in our MyDataSource class, we can implement the GetRecords() method, and it's very simple to do.

public override UcwSourceRecord[] GetRecords(Ucw.UcwSourceRecord FieldMap) {
    return new UcwSourceRecord[] { theInterface.GetRecord() };
}

User Creation Wizard expects an array of UcwSourceRecord objects, and so we provide it with one containing the record generated by our GetRecord() method in the ManualDataEntry UserControl. 

All that remains is to build your class library, and drop the dll file into C:\Program Files (x86)\Trogsoft Ltd\ADStudio\User Creation Wizard\DataSources, then run User Creation Wizard.

Add a Comment