( Spinning off from this thread: http://sharepointsolutions.zendesk.com/entries/131228 )
I want all new users to be created consistently with their UserName = [First Initial][Last Name]. Knowing we can't trust users to be consistent, I've added a "FirstName" and "LastName" field to my profile & registration page.
My plan was to set the UserName in the OnRegistration() event. First attempt produced an error:
c:\Program Files\Common Files\microsoft shared\Web Server Extensions\12\TEMPLATE\LAYOUTS\WaTransit\Register.aspx(22): error CS0200: Property or indexer 'SPSolutions.SharePoint.Delegation.RegistrationEventProperties.UserName' cannot be assigned to -- it is read only at System.Web.Compilation.AssemblyBuilder.Compile()
OK - So, the UserName property needs to be set prior to the RegistrationEvent being created. However, by the time the OnRegister() event is called, the RegistrationEvent has (obviously) already been created.
Next attempt: Change the finish button to call a script that 1) sets the UserNameTextBox.Text to the appropriate value and 2)calls the FinishButton_Click. I think I'm close, but need help with the parameters with which to call the FinishButton_Click event.
Something like this (obviously the parameters are not correct, just fumbling around trying to figure out what it is looking for)
protected void OnFinish()
UserNameTextBox.Text = "mutton";
c:\Program Files\Common Files\microsoft shared\Web Server Extensions\12\TEMPLATE\LAYOUTS\WaTransit\Register.aspx(25): error CS1502: The best overloaded method match for 'SPSolutions.SharePoint.Delegation.ApplicationPages.RegisterPage.FinishButton_Click(object, System.EventArgs)' has some invalid arguments at System.Web.Compilation.AssemblyBuilder.Compile()
Am I close, or totally off base?
You should not override the OnRegister method of ExCM. This is core registration code and should not be altered. ExCM supports two registration events: OnRegistering, OnRegistered.
OnRegistering gets called before the registration is persisted. This is an ideal place to perform any registration validation. In this method you may set the Cancel and ErrorMessage properties of the RegistrationEventProperties instance to cancel the registration and provide an error message back to your registering user.
OnRegistered occurs after the registration is persisted. This is an ideal place to perform an post-registration activities like updating the SharePoint User Info object or saving information back to AD. The Cancel and ErrorMessage properties are ignored in this method as the registration has already been saved.
Maybe I didn't ask the question correctly. I'm not trying to override the OnRegister() event. I am trying to set the UserName value programatically prior to the user being created.
I am unable to do this in the OnRegistering() event, because at that point the UserName is read-only. It appears to me that I need to set the property prior to the OnRegistering() event being called.
My approach was to try to set the property after the "onClick" event and prior to the Registration event and then sending the updated info off to the registration code. (having the finish button calling an interim bit of code) Maybe that isn't the correct approach?
Bottom line is I don't want the user to type in the userName value - I want it to be a value derived from the FirstName / LastName profile properties for the sake of consistency.
The Register page has a property named UserInfo of type ExtranetUserInfo. The ExtranetUserInfo class has a property named AccountName of type string which is used to create the new extranet user. Set the UserInfo.AccountName to the desired username in the OnRegistering event.
Woot!! - Thanks Jeremy, that did the trick.
Here is my (grossly inelegant) OnRegistering() event if anyone should be interested. Tested & working, but needs error checking yet, etc.
protected override void OnRegistering(RegistrationEventProperties eventProperties)
// Derive user name from First Initial & Last Name
mName = (string) UserProfileInfo.Properties["FirstName"];
mName = mName.Substring(0,1);
mName = mName + (string) UserProfileInfo.Properties["LastName"];
UserInfo.AccountName = mName;