Try our conversational search powered by Generative AI!

How to create attributes programatically?


I'm sure there is a way to create Community attributes programatically but it's nowhere to be found - neither on the net nor the SDK. Someone who made this happen can you please share the wizdom. Thanks in advance!

Jun 14, 2013 1:18

I am not sure but i believe there are a way todo it as well. Have not time to investigate at the moment but I was in a project last autum where I think we did that. The base for that project was the MedStore templates from Circuit. You can dowload and check the code from it is a bit old but they might have that when importing users from AD or something like that.

Jun 14, 2013 9:56

Thanks, but I managed to solve it by reflecting and guessing :) It's actually very simple once you know it. But it had saved me some time if this was already mentioned in the docs. Hint Episerver...

Here are the magic lines (for future reference):

var attribute = new EPiServer.Common.Attributes.Attribute("SomeAttribute", typeof(User), typeof(string));

To be honest, I don't think creating attributes manually through admin is a serious approach, since it cannot be reproduced in all environments without manual work. Doing it programatically (through an init module) is the only way to do this right.

Jun 14, 2013 12:23

You may want to use CommunityAttributeBuilder ( that is similar to PageTypeBuilder for CMS. Currently it's supporting CMS6, I'll commit v7 as soon I will finish testing. By decorating your class properties with special attribute you will find those created in target site.

For instance:

[CommunityEntity(TargetType = typeof(IUser))]
public class UserAttributes : IClubUserAttributes
    public virtual int AccessType { get; set; }

    public virtual string Code { get; set; }

    public virtual int EmployeeKey { get; set; }

    public virtual bool IsAdmin { get; set; }


Library will scan all assemblies and look for types decorated with CommunityEntity attribute, if found one then properties will be scanned and those decorated with CommunityEntityMetadata attribute will be automatically created in DB. It also supports strongly-typed interface over IUser type:

var metadata = user.AsAttributeExtendable<UserAttributes>();
metadata.AccessType = info.AccessType;
metadata.Code = info.Code;
metadata.EmployeeKey = info.EmployeeKey;
metadata.IsAdmin = info.IsAdmin;


More info about library could be found -

More info about internals (if interested) could be found here -

Jun 18, 2013 23:32

Package for CMS7 in on the way to Nuget feed ;)

Jun 20, 2013 11:14
Jun 25, 2013 8:14
This thread is locked and should be used for reference only. Please use the Legacy add-ons forum to open new discussions.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.