Custom User Fields - CommunityServer v2.1
How to add custom user fields in CommunityServer 2.1
Scenario:
I'm working on a CommunityServer site (v2.1 .Net 2), and need to add custom user fields. This site is for a professional organization where the members have a 'License #', so they want the 'Sign-Up' form to require the user to enter:
- License # (store in cs_Users)
- First Name (store in aspnet_Profile as commonname)
- Last Name (store in aspnet_Profile as commonname)
in addition to the default fields required by CommunityServer at sign up. This will allow them to verify the user's credentials before activating their user account.
CS stores a ‘commonname’ value in the aspnet_Profile table, so I will take advantage of that to store my first and last name values. With the license# field, I have to get a little creative, and I will store that in the cs_Users table. In both cases, there is no need to alter the database as these tables store properties in arrays in a single field, so we can just add to the items in those arrays.
Process:
Part I: Changing the Sign up form.
In the web application:
1)
Change 'communityserver.config' by adding this under "<CommunityServer>":
<
ExtendedUserData
>
<
add
name
=
"LicenseNo" />
</ExtendedUserData>
*You could add more items if you want to store more custom properties in the ‘cs_Users’ table.
2) In the CommunityServerControls20 project, open User\CreateUser.cs
a. In the class variables, add:
TextBox
LicenseNo;
TextBox FirstName;
TextBox
LastName;
b.
In AttachChildControls() add:
LicenseNo = (TextBox)FindControl("LicenseNo");
firstname = (TextBox)FindControl("FirstName");
lastname = (TextBox)FindControl("LastName");
c.
In SaveAdditionalProfileData(User user), add the first and last names to the user.Profile.CommonName before the user is saved (see red mods).
protected
virtual void SaveAdditionalProfileData(User user)
{
if (timezone != null)
user.Profile.Timezone = double.Parse(timezone.SelectedItem.Value);
string commonname = "";
if (firstname != null)
commonname = firstname.Text.Trim() + " ";
if (lastname != null)
commonname += lastname.Text.Trim();
user.Profile.CommonName = commonname.Trim();
if (csContext.SiteSettings.ShowContactCheckboxes)
{
if (allowSiteToContact != null)
user.AllowSiteToContact = allowSiteToContact.SelectedValue;
if (allowSitePartnersToContact != null)
user.AllowSitePartnersToContact = allowSitePartnersToContact.SelectedValue;
}
SetExtendedUserData(user);
Users.UpdateUser(user);
}
*
If
you are wondering why I’m not adding something for ‘LicenseNo’, the reason is that the ‘SetExtendedUserData(user)’ call will handle that without making any further mods to the code.
3) In Themes\default\Skins\ Skin-CreateUser.ascx, I add the following html:
<
tr
>
<
td align="right">
<
CS
:
ResourceControl runat="server" ResourceName="CreateNewAccount_LicenseNo" ID="Resourcecontrol12"/>
</
td
>
<
td align="left">
<
div class="CommonFormField">
<
asp
:
textbox id="LicenseNo" MaxLength="64" runat="server" columns="40" ></asp:textbox>
<
asp
:
RequiredFieldValidator EnableClientScript="false" id="RequiredFieldValidator1" runat="server" ControlToValidate="LicenseNo" Cssclass="validationWarning">*</asp:RequiredFieldValidator>(from your membership card)
</
div
>
</
td
>
</
tr
>
<
tr
>
<
td align="right">
<
CS
:
ResourceControl runat="server" ResourceName="CreateNewAccount_FirstName" ID="Resourcecontrol13"/>
</
td
>
<
td align="left">
<
div class="CommonFormField">
<
asp
:
textbox id="FirstName" MaxLength="64" runat="server" columns="40" ></asp:textbox>
<
asp
:
RequiredFieldValidator EnableClientScript="false" id="RequiredFieldValidator2" runat="server" ControlToValidate="FirstName" Cssclass="validationWarning">*</asp:RequiredFieldValidator>
</
div
>
</
td
>
</
tr
>
<
tr
>
<
td align="right">
<
CS
:
ResourceControl runat="server" ResourceName="CreateNewAccount_LastName" ID="Resourcecontrol14"/>
</
td
>
<
td align="left">
<
div class="CommonFormField">
<
asp
:
textbox id="LastName" MaxLength="64" runat="server" columns="40" ></asp:textbox>
<
asp
:
RequiredFieldValidator EnableClientScript="false" id="RequiredFieldValidator3" runat="server" ControlToValidate="LastName" Cssclass="validationWarning">*</asp:RequiredFieldValidator>
</
div
>
</
td
>
</
tr
>
A note about the above html: The ‘id’ value of each of the textboxes much match the names I put in the config files exactly. These ARE case-sensitive. For the ‘LicenseNo’ textbox the name must match exactly what I put in ‘communityserver.config’. For the ‘FirstName’ and ‘LastName’ textboxes, it must match exactly what was used in the previous step when altering ‘CreateUser.cs’ file.
Also note that I put in RequiredFieldValidators on all 3 of these rows to make sure the user puts in the information requested.
4) Now to set our labels, we need to edit Languages/en-US/Resources.xml in the web application.
Add the following resources in the appropriate sections:
(*Add these to the section where they begin with ‘CreateNewAccount_’)
<
resource
name
=
"CreateNewAccount_LicenseNo">ADHA Member #</resource>
<
resource
name
=
"CreateNewAccount_FirstName">First Name:</resource>
<
resource
name
=
"CreateNewAccount_LastName">Last Name:</resource>
(*Add this to the section where they begin with ‘EditProfile_’)
<
resource
name
=
"EditProfile_LicenseNo">ADHA Member #</resource>
Compile CommunityServerControls20, and put the updated dll CommunityServer.Controls.dll) in your website bin.
Now you are done changing the Sign up form.
Now we want to change the Administrators EditUser page so our new properties show up there and can be edited there by the administrator. This is dependent on some of the changes made in the earlier section.
Part II: Changing the Administrators EditUser page.
*Some of this is dependent on things we’ve done in Part I.
This will be much easier than Part I. What we need to do is change web\Controlpanel\Membership\UserEdit.aspx
.
1) In the html view, I find a location where I want to show my items and insert these 2 rows:
<
tr
>
<
td
class="CommonFormFieldName" width="45%">
<
cp
:
resourcecontrol runat="server" resourcename="EditProfile_CommonName" id="ResourceControl5" name="ResourceControl5" />
</
td
>
<
td class="CommonFormField">
<
asp
:
textbox id="CommonName" size="30" maxlength="50" runat="server" />
</
td
>
</
tr
>
<
tr
>
<
td class="CommonFormFieldName" width="45%">
<
cp
:
resourcecontrol runat="server" resourcename="EditProfile_LicenseNo" id="ResourceControl2" name="ResourceControl2" />
</
td
>
<
td class="CommonFormField">
<
asp
:
textbox id="LicenseNo" size="30" maxlength="50" runat="server" />
</td>
</tr>
*The textbox id’s are again case sensitive.
We don’t need to make any changes to the code behind. Its not magic, but I’ll leave it you to read the code behind if you really want to see why this works without adding more code to the code behind page.
Here’s what the changed pages look like:
[ 12/23/2006 ]