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.
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:
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
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:
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:
To create your new MemberType, without any inherited parents, as it would appear if you created it via the backoffice.
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:
Seems like a fourth constructor that would be helpful would be this:
That would allow us to do in one line.
Maybe there's a pull request in my future.
Cheers, David
is working on a reply...