Try our conversational search powered by Generative AI!

erik.engstrand@precio.se
Aug 20, 2009
  7909
(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
Azure AI Language – Extractive Summarisation in Optimizely CMS

In this article, I demonstrate how extractive summarisation, provided by the Azure AI Language platform, can be leveraged to produce a set of summa...

Anil Patel | Apr 26, 2024 | Syndicated blog

Optimizely Unit Testing Using CmsContentScaffolding Package

Introduction Unit tests shouldn't be created just for business logic, but also for the content and rules defined for content creation (available...

MilosR | Apr 26, 2024

Solving the mystery of high memory usage

Sometimes, my work is easy, the problem could be resolved with one look (when I’m lucky enough to look at where it needs to be looked, just like th...

Quan Mai | Apr 22, 2024 | Syndicated blog

Search & Navigation reporting improvements

From version 16.1.0 there are some updates on the statistics pages: Add pagination to search phrase list Allows choosing a custom date range to get...

Phong | Apr 22, 2024