erik.engstrand@precio.se
Aug 20, 2009
  8012
(0 votes)

EPiServer Community and user attributes

This article will give some examples of how to work with attributes on users in the EPiServer Community. Attributes can also be used on other objects in the commnuity, such as blogs, but this will not be discussed here.

Adding attributes

To get it possible to work with an attribute, you must first add it. Click on the Attributes link in the Community tab. You will get this dialog:

image

I will here create an attribute “ServiceIdentifier” of type string for the IUser class. It will now be possible to set this attribute on all users.

image

It is also possible to create custom attribute types. You can read about it here: http://world.episerver.com/en/Articles/Items/Creating-a-Custom-EPiServer-Community-Module---Attribute-Data-Types/

Set attribute value on the user

I will here set a guid to this newly created attribute. CurrentUser is a property of type IUser.

   1: Guid guid = Guid.NewGuid();
   2: if (CurrentUser != null && String.IsNullOrEmpty(CurrentUser.GetAttributeValue<string>("ServiceIdentifier")))
   3: {
   4:     // Create a clone of the user to enable edit
   5:     IUser currentUserClone = CurrentUser.Clone() as IUser;
   6:  
   7:     currentUserClone.SetAttributeValue<string>("ServiceIdentifier", guid.ToString());
   8:  
   9:     // Save everything to the user
  10:     CommunitySystem.CurrentContext.DefaultSecurity.UpdateUser(currentUserClone);
  11: }

Get attribute value from the user

   1: if (CurrentUser != null)
   2: {
   3:     return CurrentUser.GetAttributeValue<string>("ServiceIdentifier");
   4: }
   5: return String.Empty;

Search, find, query users given an attribute

You can create very advanced queries to search for users. The method IsServiceUser checks if there exists a user with the attribute “ServiceIdentifier” set to the value of guid. I don’t know know fast this kind of queries are on a huge number of users. Does anyone know?

   1: public bool IsServiceUser(string guid)
   2: {
   3:     //Create a new BlogQuery
   4:     UserQuery uq = new UserQuery();
   5:     StringCriterion strCriterion = new StringCriterion();
   6:     strCriterion.Value = guid;
   7:     uq["ServiceIdentifier"] = strCriterion;
   8:     return CommunitySystem.CurrentContext.DefaultSecurity.GetQueryResult(uq).Count > 0;
   9: }
Aug 20, 2009

Comments

Sep 21, 2010 10:32 AM

Nice and informative post, thanks!

Is there any way to programatically add such an attribute? Without going though the EPiServer administration interface?
/ Glenn F. Henriksen

Please login to comment.
Latest blogs
Creating an Optimizely CMS Addon - Adding an Editor Interface Gadget

In   Part One   of this series, I covered getting started with creating your own AddOn for Optimizely CMS 12. This covered what I consider to be an...

Mark Stott | Aug 30, 2024

Configure your own Search & Navigation timeouts

The main blog Configure your own Search & Navigation timeouts was posted for years but you need copy the code to your application. We now bring tho...

Manh Nguyen | Aug 30, 2024

Joining the Optimizely MVP Program

Andy Blyth has been honoured as an Optimizely MVP, recognising his contributions to the Optimizely community. Learn how this achievement will enhan...

Andy Blyth | Aug 29, 2024 | Syndicated blog

Welcome 2024 Summer OMVPs

Hello, Optimizely community! We are thrilled to announce and welcome the newest members to the Optimizely Most Valuable Professionals (OMVP) progra...

Patrick Lam | Aug 29, 2024