ExtendedAttributes in Community Server

In this, the first of what I hope is a long line of articles based on customizing Community Server, I want to talk about a little know but very powerful property found in many places inside the CS API.

One thing people often need to do in Community Server, especially when customizing it, is to save custom data. Of course, if you’re feeling brave, you can always edit the database schema and related Data Providers as well as extending the relevant class (Weblog, Gallery, User etc.). Now this method is not without its merits, such as easier searching based on these new values, plus better performance where the values are to regularly accessed, and as ever, such factors should always be taken into account when extending Community Server.

What I want to talk about however, is the case where you want to simply store some extra information with an object. For example, one common request is to save extra contact information against a user. Currently, Community Server allows you to add IM addresses for MSN, AOL, Yahoo and ICQ contact details to a user’s profile, but you might want more! Another example might be that you wish a user to accept some specific terms/rules before you allow them to use your site (this came up on a recent client installation) so you need to record the date/time when they accepted.

By far the easiest way to achieve this is by using ExtendedAttributes. Most of the major classes in CS inherit from ExtendedAttributes such as User, Post & Section (in turn Weblog, Gallery, Forum etc inherit from Section as WeblogPost, GalleryPost etc. inherit from Post). All of these classes have two methods that you can use to set and retrieve this data and these are GetExtendedAttribute() & SetExtendedAttribute(). Under the hood, the ExtendedAttributes class uses a NameValueCollection object to store the data so naturally, SetExtendedAttribute()requires just two parameters; name and value.

So, to assign a users GoogleTalk address:

User user = CSContext.Current.User; // Get the logged on user.

user.SetExtendedAttribute("GoogleTalkAccount", "usersaccount"); // Set the new details.

Users.UpdateUser(user); // Save the changes.

And to retrieve that data:

string googleTalkAccount = user.GetExtendedAttribute("GoogleTalkAccount");

Or, to set the date that a user accepted the terms to your site;

User user = CSContext.Current.User; // Get the logged on user.

user.SetExtendedAttribute("TermsAccepted", DateTime.Now.ToString()); // Set the date.

Users.UpdateUser(user); // Save the changes.

And again, to retrieve this:

DateTime termsAccepted = Convert.ToDateTime(user.GetExtendedAttribute("TermsAccepted"));

Of course, normally, you’d need to check the above string before trying to cast it as a DateTime, but we’ll skip that for the purposes of this article. As a side note, if the ExtendedAttribute doesn’t exisit in the colletion, GetExtendedAttribute() will return an empty string.

That’s it for this article, and I’d just like to thank Microsoft for adding these great new blogging features to Word 2007 to make this article so easy to write.

6 comment(s) so far

admin avatar

Great article man,


I'm going to update my today's Dev Guide to use this great article as a reference ;-)

admin avatar

Thanks.  I read your post and was short of ideas for an article so the bit about ExtendedAttribute in there gave me the idea to do something slightly more in depth on them (seeing as they're so useful).

admin avatar
David wrote on January 23, 2008

I'm just beginning to research how to extend CS without having to make a great deal of modifications. I like your article and it leads me to a question I've been asking through the forums lately: Groups, CS needs grouping options and it should be user selectable based on role. The only real grouping option I've been able come up with in CS is right now is to manually place member galleries in groups.



I'm a database programmer and grouping is one of the most basic, default ideas I work with day after day. It would only require two tables with a total of 4 fields to implement "infinite" grouping in CS. Groups need to be searchable using the default searching schemes.



The reason I'm writing this is because the discussion is about extened attributes. Could we use this method to solve the ability to define members in groups?



admin avatar
Jacob Portnoy wrote on January 23, 2008

Is this possible to implement for extending attributes of an uploaded file? For example, if I want to add the field "abstract" to an uploaded file (presumably a paper), could I use extended attributes to do this? If so, how would I go about beginning this process? Please e-mail me at jportnoy [at] cfinst [dot] org. Thanks in advance.

admin avatar
sfdhftftg wrote on January 23, 2008

fgthjftgh

 avatar
Dave wrote on January 23, 2008

You should use a host that support CS so if you do run into any issues they can be resolved quickly. We are currently using Server Intellect to host our CS page for this very reason. If CS stops working or we cant implement a change they will fix it for us.

Post your comment