How to create attributes programatically?

Vote:
 

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!

#72362
Jun 14, 2013 1:18
Vote:
 

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 http://relateintranet.codeplex.com/ it is a bit old but they might have that when importing users from AD or something like that.

#72367
Jun 14, 2013 9:56
Vote:
 

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));
AttributeHandler.Instance.AddAttribute(attribute);

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.

#72374
Jun 14, 2013 12:23
Vote:
 

You may want to use CommunityAttributeBuilder (https://github.com/Geta/Community.EntityAttributeBuilder) 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
{
    [CommunityEntityMetadata]
    public virtual int AccessType { get; set; }

    [CommunityEntityMetadata]
    public virtual string Code { get; set; }

    [CommunityEntityMetadata]
    public virtual int EmployeeKey { get; set; }

    [CommunityEntityMetadata]
    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 - http://world.episerver.com/Blogs/Valdis-Iljuconoks/Dates/2012/6/Community-Attribute-Builder-final/

More info about internals (if interested) could be found here - http://www.tech-fellow.lv/2012/06/when-you-need-something-stronger/

#72497
Jun 18, 2013 23:32
Vote:
 

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

#72547
Jun 20, 2013 11:14
Vote:
 
#72631
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.