erik.engstrand@precio.se
Aug 20, 2009
  7975
(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
Opti ID overview

Opti ID allows you to log in once and switch between Optimizely products using Okta, Entra ID, or a local account. You can also manage all your use...

K Khan | Jul 26, 2024

Getting Started with Optimizely SaaS using Next.js Starter App - Extend a component - Part 3

This is the final part of our Optimizely SaaS CMS proof-of-concept (POC) blog series. In this post, we'll dive into extending a component within th...

Raghavendra Murthy | Jul 23, 2024 | Syndicated blog

Optimizely Graph – Faceting with Geta Categories

Overview As Optimizely Graph (and Content Cloud SaaS) makes its global debut, it is known that there are going to be some bugs and quirks. One of t...

Eric Markson | Jul 22, 2024 | Syndicated blog

Integration Bynder (DAM) with Optimizely

Bynder is a comprehensive digital asset management (DAM) platform that enables businesses to efficiently manage, store, organize, and share their...

Sanjay Kumar | Jul 22, 2024