Try our conversational search powered by Generative AI!

erik.engstrand@precio.se
Aug 20, 2009
  7905
(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
Optimizely and the never-ending story of the missing globe!

I've worked with Optimizely CMS for 14 years, and there are two things I'm obsessed with: Link validation and the globe that keeps disappearing on...

Tomas Hensrud Gulla | Apr 18, 2024 | Syndicated blog

Visitor Groups Usage Report For Optimizely CMS 12

This add-on offers detailed information on how visitor groups are used and how effective they are within Optimizely CMS. Editors can monitor and...

Adnan Zameer | Apr 18, 2024 | Syndicated blog

Azure AI Language – Abstractive Summarisation in Optimizely CMS

In this article, I show how the abstraction summarisation feature provided by the Azure AI Language platform, can be used within Optimizely CMS to...

Anil Patel | Apr 18, 2024 | Syndicated blog

Fix your Search & Navigation (Find) indexing job, please

Once upon a time, a colleague asked me to look into a customer database with weird spikes in database log usage. (You might start to wonder why I a...

Quan Mai | Apr 17, 2024 | Syndicated blog