質問

This may be a very simple question but I'm very new to EPiServer, so pls help.

I'm working on the EPiServer Relate demo site. I want to progrmatically create a new attribute on Episerver.Common.Security.IUser type. I have created attributes using CMS edit mode Admin options. But I want to know how to do this in code.

役に立ちましたか?

解決

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/

他のヒント

You need to use the AttributeHandler class.

Joel has written a great guide with example code here

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top