When you have a custom member type defined in Umbraco with some extra properties (eg. a property named customMemberInformation), the following code can be used (for example in your own SurfaceController):
...
MembershipCreateStatus status;
var member = Members.RegisterMember(model, out status, model.LoginOnSuccess);
// Handle custom fields
var memberService = Services.MemberService;
var customMember = memberService.GetByEmail(member.Email);
customMember.SetValue("customMemberInformation", "My custom member information");
memberService.Save(customMember);
...
In this simplified example I store the text "My custom member information" in the custom field and save the data using the MemberService.
To create a custom property for a member you have to add it to the member type, by creating a new property type based on an existing data type. This makes sense if you think of how it works in the backoffice.
See below, get the 'Label (string)' data type, and create a new PropertyType from it (there are a few overloads when creating, but you need to make sure Name and Alias are set, which none of them do). Then you assign it to the member type and save. Saving the member type has the knock of effect of creating the property type.
var memberType = _memberTypeService.Get("Member");
if (!memberType.PropertyTypeExists(propertyAlias)) {
var dataType = _dataTypeService.GetDataType("Label (string)");
var newProp = new PropertyType(dataType); //object needs to be saved
newProp.Name = propertyName;
newProp.Alias = propertyAlias;
bool success = memberType.AddPropertyType(newProp, propertyGroupName);
_memberTypeService.Save(memberType);
}
Then you need to re-get the member before setting the new property type.
See below a full example:
//get the member and member type
var memberType = _memberTypeService.Get("Member");
var currentMember = _memberService.GetByEmail("[email protected]");
//setup some properties
string propertyGroupName = "Test Group";
string propertyName = "Test Property";
string propertyAlias = "testProperty";
//create the property group if it doesn't exist
if (!memberType.PropertyGroups.Any(pg => pg.Name == propertyGroupName))
{
bool success = memberType.AddPropertyGroup(propertyGroupName);
_memberTypeService.Save(memberType);
}
//create the property if it doesn't exist
if (!memberType.PropertyTypeExists(propertyAlias))
{
var dataType = _dataTypeService.GetDataType("Label (string)");
var newProp = new PropertyType(dataType); //object needs to be saved
newProp.Name = propertyName;
newProp.Alias = propertyAlias;
bool success = memberType.AddPropertyType(newProp, propertyGroupName);
_memberTypeService.Save(memberType);
}
currentMember = _memberService.GetByEmail(currentMember.Email);
currentMember.SetValue(propertyAlias, "456");
_memberService.Save(currentMember);
I used dependency injection to get the _memberTypeService and _dataTypeService.
Add custom property to Membership member
Hi
I need to programmatically add a custom property an umbraco member using the standard membership provider. I'm using Umbraco 7.3.
So I create the member below (is this correct?) then I need to add a custom property that will store simply a list of integers.
What approach do I take?
Hi Euge,
Try to see our documentation about the member service, there you have the create method and others.
https://our.umbraco.org/documentation/reference/management/services/memberservice
Or in this video on Umbraco TV
https://umbraco.tv/videos/umbraco-v7/developer/fundamentals/member-api/
Hope this helps,
/Dennis
Great thanks Dennis
There doesn't appear to be anything on adding a custom property but I am thinking along the lines of
Hi Euge,
This video should show you have to set custom property values on a member using the Member API
http://umbraco.tv/videos/umbraco-v7/developer/fundamentals/member-api/populating-property-values/
Hope this helps,
/Dennis
Hi Euge,
When you have a custom member type defined in Umbraco with some extra properties (eg. a property named customMemberInformation), the following code can be used (for example in your own SurfaceController):
...
...
In this simplified example I store the text "My custom member information" in the custom field and save the data using the MemberService.
Does this help you move ahead?
Just to note that in the above example, customMemberInformation needs to be created in the back office.
I stumbled on an answer for this here https://our.umbraco.com/forum/using-umbraco-and-getting-started/106096-add-property-to-media-type-programatically
To create a custom property for a member you have to add it to the member type, by creating a new property type based on an existing data type. This makes sense if you think of how it works in the backoffice.
See below, get the 'Label (string)' data type, and create a new
PropertyType
from it (there are a few overloads when creating, but you need to make sureName
andAlias
are set, which none of them do). Then you assign it to the member type and save. Saving the member type has the knock of effect of creating the property type.Then you need to re-get the member before setting the new property type.
See below a full example:
I used dependency injection to get the _memberTypeService and _dataTypeService.
The above works in 8.15.1
is working on a reply...