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!

5 comments:

  1. Nice post!
    An option to clearing the cache manually is to use the overload of GetQueryResult(...) where you can specify a cache timeout for the query.

    //Tom Stenius

    ReplyDelete
  2. Great post! Just what I needed! Thanks!

    /Jens

    ReplyDelete
  3. Hello Karoline

    Thanks for posting this!

    I wonder if you have any idea whether this would be a better choice to implement custom user fields in a bigger web application OR the standard asp.net profile (System.Web.Profile.ProfileBase)?

    I'm thinking in terms of performance / scalability - e.g. if you have 1000 or 10 000 users?

    Miklos

    ReplyDelete
  4. Hi Miklos,

    In a recent project (http://www.fk-world.com) I've used the attributes in Relate+ rather than standard asp.net profile without noticing any performance issues. The site has approximately 5000 users, and it works like a charm!

    The reason I chose attributes instead of asp.net profile was mainly development time and searchability. Not having to extend the User class programatically saved me a lot of time compared to what extending the ProfileBase class would. The reason for this is the Relate+ query system which I would have to 'recreate' myself if I extended ProfileBase.

    But then on the other side, the attributes are not very customizable. So if you need to do a lot of data processing before displaying the data, ProfileBase might have an advantage.

    But all in all, you're asking a person who thinks that attributes are the core of Relate+, so I'd recommend trying them out :)

    Karoline

    ReplyDelete
  5. Thank you very much. That's perfect, we're also expecting a user number around <=5000 so it's great to hear it works well!

    The searchability is a good point too but so far it's not a requirement. Maybe I could promote the idea now :) But you're right, I should give them a chance and I will!

    Thank you again
    Miklos

    ReplyDelete