Monday, July 13, 2009

Attributes in EPiServer Relate+

Attributes in EPiServer Relate+ is an easy way of extending classes without having to do it programmatically. In order to assign an attribute to an object, the object has to implement the IAttributeExtendableEntity interface and to everyone’s joy most objects in Relate+ do.

In this example each user of type EPiServer.Common.Security.IUser will be able to register their favorite sport of type System.String. The first thing to do is to add the attribute in the Community tab of the edit interface:



The attribute is now defined in the database, so we can set its value:



Getting the attribute value is just as simple:




Now what if we want to find all the users who have rugby as their favorite sport? Most of the EPiServer Community classes allow querying based on a set of criteria, and that’s the way to go:



First a new UserQuery is created and a StringCriterion is added. Multiple criterions can be added if that’s needed. The GetQueryResult method fills up the cache with the query result. Use the RemoveQueryResultCache method to clear the cache:



Piece of cake!