CS Spotlight: Extended User Data
Introduction
For this week's spotlight, I figured I'd talk about another often overlooked feature that exists in Community Server, ExtendedUserData. This small feature grew out of numerous client requests to include additional profile information during both registration and profile editing while not touching core code. While it's not the end-all solution it's quite simple to setup and meets the demands of the basic scenarios. You could add controls to the correct skins, configure the fields in communityserver.config, and then the magic would happen. Whenever a user registers, their additional input would be captured and stored in ExtendedAttributes for display on profile editing. Using additional non-evasive techniques, this data can be surfaced on most any additional skin.
At this time, the skins that participate in the feature are Themes\default\Skins\Skin-CreateUser.ascx, Themes\default\Skins\Skin-EditProfile.ascx, and ControlPanel\Membership\UserEdit.aspx. Since these skins are not read-only, there is some needed code-behind that exists for capturing the data. For any other skin files, if you need to display this data, you would just pull it out of the user object.
Example
Assume your site is planning on contacting folks via postal mail. (I know, "That's so 80's!", but work with me here). You've decided to request the following pieces of information:
-
Address Line 1
-
Address Line 2
-
City
-
State
-
Zip
So, the first step would be to open up the registration skin (Themes\default\Skins\Skin-CreateUser.ascx) and add markup and server controls to capture the data. For example, in order to add input for the first address line, you could add the following:
...
<tr>
<td align="right">
Address Line 1
</td>
<td align="left">
<div class="CommonFormField">
<asp:TextBox id="addressLine1" runat="server" />
</div>
</td>
</tr>
...
Once you're done adding the controls to the skins, you need to open communityserver.config. While the location doesn't matter, I recommend adding the following near the bottom of the file since it's extra.
...
<ExtendedUserData>
<add name="addressLine1" />
<add name="addressLine2" />
<add name="city" />
<add name="state" />
<add name="zip" />
</ExtendedUserData>
....
That's really all you need to do! CS will loop thru the list of extended user data and bind existing data to controls it finds. This means the server controls must be named the same as the configuration information. At this time, you can use any controls that derive from TextBox, CheckBox, and ListControl.
Next Steps
Now, some folks will want to display this data on the user's public profile page. Since that would be a read-only scenario, there's no core code needed to support it. You could just open up the user profile skin (Themes\default\Skins\Skin-UserProfile.ascx) and add the following:
<%= CSContext.Current.User.GetExtendedAttribute("addressLine1") %>
But doing so would display the logged on user's value for the data. Since you'll most likely want to view information for other users, especially on their profile, I recommend you add the following to your user profile skin:
<script language="cs" runat="server">
User GetUserToView()
{
User user = null;
CSContext csContext = CSContext.Current;
int userID = csContext.UserID;
if (userID <= 0)
{
string username = Context.Request.QueryString["UserName"];
if (username == string.Empty)
throw new CSException(CSExceptionType.UserNotFound);
else
user = Users.GetUser(0, username, false, true);
}
else
{
user = Users.GetUser(csContext.UserID, false, true);
}
return user;
}
</script>
Then, somewhere within your markup, you could include the following:
<%= GetUserToView().GetExtendedAttribute("addressLine1") %>
As with many other configuration based updates, you'll need to "touch" the web.config file in order to flush the existing configuration and reload the updated changes.
Summary
Community Server offers many ways to extend the platform. Today we've peeked at yet another non-invasive method to extend a user's profile information. For a peek at this type of implementation in the wild, be sure to check out The Hive and specifically the registration form.