Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • David F. Hill 122 posts 242 karma points
    Oct 31, 2015 @ 01:10
    David F. Hill
    0

    Create a new Member Type programatically

    Would anyone have an example of how to create a new Member Type using the MemberType service? I've seen this:

        public void CreateMemberType(IContentTypeComposition parent, string memberTypeAlias)
    {
            IMemberType memberType = new Umbraco.Core.Models.MemberType(parent, memberTypeAlias);
            ApplicationContext.Current.Services.MemberTypeService.Save(memberType);
    }
    

    I don't know how to get or assign the "parent".

    I just want to create the new member type in "Member Types".

    Thanks, David

  • Marc Goodson 2149 posts 14377 karma points MVP 9x c-trib
    Nov 01, 2015 @ 12:01
    Marc Goodson
    0

    Hi David

    Yes, a MemberType inherits from ContentTypeCompositionBase and so this means it has the same concept of being able to create a new MemberType inheriting properties from an existing MemberType. Hence the constructor for MemberType (from the base) allowing you to pass in a Parent ContentTypeComposition, to inherit from.

    In the backoffice if you create a new MemberType, then it does not create this type underneath an existing ContentTypeComposition - and there is no GUI to allow you to do this.

    And if you do happen to do this in code then you get a message in the back office when you create your Child MemberType that says:

    Master Content Type enabled This Content Type uses '[masterContentTypeAliashere]' as a Master Content Type. Tabs from Master Content Types are not shown and can only be edited on the Master Content Type itself

    So what is the trick ? - well there is a convention in Umbraco that if you want to create an item at the top of a tree, then you specify the parent as having id -1

    So in your circumstance you should be able to use something like:

      var mts = Services.MemberTypeService;
                var timeStamp = DateTime.Now.Ticks;
                var newMemberType = new MemberType(-1);
                newMemberType.Alias = "MT" + timeStamp;
                newMemberType.Name = "MT" + timeStamp;
                mts.Save(newMemberType);
    

    To create your new MemberType, without any inherited parents, as it would appear if you created it via the backoffice.

  • David F. Hill 122 posts 242 karma points
    Nov 01, 2015 @ 18:28
    David F. Hill
    0

    Thanks, Marc, for that great explanation. It helped me and I'm sure it will help other Umbracians as well.

    For some reason it didn't occur to me to "new up" a MemberType and then assign the alias in a separate operation.

    Currently, there are three overloads for the MemberType constructor:

    public MemberType(IContentTypeComposition parent);
    public MemberType(int parentId);
    public MemberType(IContentTypeComposition parent, string alias);
    

    Seems like a fourth constructor that would be helpful would be this:

    public MemberType(int parentId, string alias);
    

    That would allow us to do in one line.

    Maybe there's a pull request in my future.

    Cheers, David

Please Sign in or register to post replies

Write your reply to:

Draft