Aug 20, 2009
(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:


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.


It is also possible to create custom attribute types. You can read about it here:

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;
   7:     currentUserClone.SetAttributeValue<string>("ServiceIdentifier", guid.ToString());
   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


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
Preview multiple Visitor Groups directly while browsing your Optimizely site

Visitor groups are great - it's an easy way to add personalization towards market segments to your site. But it does come with it's own set of...

Allan Thraen | Sep 26, 2022 | Syndicated blog

The Report Center is finally back in Optimizely CMS 12

With Episerver.CMS.UI 12.12.0 the Report Center is finally re-introduced in the core product.

Tomas Hensrud Gulla | Sep 26, 2022 | Syndicated blog

Dynamic Route in ASP.NET Core When MapDynamicControllerRoute Does Not Work

Background Creating one of the add-on for Optimizely I had to deal with challenge to register dynamically route for the API controller. Dynamic rou...

valdis | Sep 25, 2022 | Syndicated blog

404 Error on Static Assets Within an Optimizely plugin

Background With the move to CMS 12 and .NET 5/6, developers are now able to build Plugins and Extensions using Razor Class Libraries (RCL).  These...

Mark Stott | Sep 23, 2022