MemberProperties returning only properties with memberCanEdit = true
Hi all. I have a question regarding the ProfileModel in Umbraco 7.1.8
I have created a number of custom properties on the default membertype, some are set as editable by the user, some are not. I want to manipulate all of these properties in backend code, but it turns out that the profileModel.MemberProperties only contains properties that have memberCanEdit = true.
How can I access the other properties in backend?
My way of getting the properties:
var membershipHelper = new Umbraco.Web.Security.MembershipHelper(Umbraco.Web.UmbracoContext.Current); var pm = membershipHelper.GetCurrentMemberProfileModel(); List<Umbraco.Web.Models.UmbracoProperty> properties = pm.MemberProperties;
I think you'll need to use the member service for that. This code is just from memory so it'll probably need tweaking, but something like this should do the trick:
// Instantiate member service and membership helper var memberService = ApplicationContext.Current.Services.MemberService;
var membershipHelper = new MembershipHelper(UmbracoContext);
// Get profile model of current member
var profileModel = membershipHelper.GetCurrentMemberProfileModel();
// Get username of current member
var username = profileModel.UserName;
// Use member service to get current member object by username (could do it from email and/or id too I think...)
var currentMember = memberService.GetByUsername(username);
// Update custom member properties
currentMember.SetValue("yourCustomProperty", "Some value");
// Save updated member
memberService.Save(currentMember);
Hmm, my approach above did indeed work, so I guess I can stick with that. I can't get your last approach to work, as I get the following error when I try to assign to the variable as you do above. (I am writing a .net usercontrol, and have included the umbraco libraries).
'Umbraco.Core.Services.MemberService' is a 'type', which is not valid in the given context.
Have you try using MemberType, it has possibilities to iterate the properties?
var mHelper = ApplicationContext.Current.Services.MemberService;
// "Member" is alias of default MemberType
var memberType = mHelper.GetMembersByMemberType("Member").FirstOrDefault();
if (memberType != null)
{
foreach (var mProperty in memberType.Properties)
{
@mProperty.Alias
}
}
Hi all. I got it to work doing the following. Dan let me in the right direction. I still can't figure out if I get the MemberService instance in the right way, but it works.
var memberService = newMemberService(new Umbraco.Core.Persistence.RepositoryFactory(), newMemberGroupService(new Umbraco.Core.Persistence.RepositoryFactory())); var memberShipHelper = newMembershipHelper(Umbraco.Web.UmbracoContext.Current); var currentMember = memberService.GetByUsername(memberShipHelper.GetCurrentMemberProfileModel().UserName); var properties = currentMember.Properties;
This way I can use a LINQ query on the PropertyCollection 'properties' to find whichever custom property I need, using it's value or whatever parameter I like. Modifying values, you must use the method Dan described above, using
// Update custom member properties currentMember.SetValue("yourCustomProperty","Some value");
// Save updated member memberService.Save(currentMember);
MemberProperties returning only properties with memberCanEdit = true
Hi all. I have a question regarding the ProfileModel in Umbraco 7.1.8
I have created a number of custom properties on the default membertype, some are set as editable by the user, some are not. I want to manipulate all of these properties in backend code, but it turns out that the profileModel.MemberProperties only contains properties that have memberCanEdit = true.
How can I access the other properties in backend?
My way of getting the properties:
Hi Martin,
I think you'll need to use the member service for that. This code is just from memory so it'll probably need tweaking, but something like this should do the trick:
Hi Dan. Almost there I think, but as I'm coding a usercontrol, I don't think I have the ApplicationContext available?
So how do I instantiate a MemberService object? Will the following work? (Seems a bit far fetched, and not at my laptop so can't try it out right now)
Thanks
Martin
Hi Martin,
You should be able to instantiate the member service outside of the `ApplicationContext` like this:
There's stuff in the documentation about this actually - http://our.umbraco.org/documentation/Reference/Management-v6/Services/MemberService.
Hmm, my approach above did indeed work, so I guess I can stick with that. I can't get your last approach to work, as I get the following error when I try to assign to the variable as you do above. (I am writing a .net usercontrol, and have included the umbraco libraries).
Hey, Martin.
Have you try using MemberType, it has possibilities to iterate the properties?
Hi all.
I got it to work doing the following. Dan let me in the right direction. I still can't figure out if I get the MemberService instance in the right way, but it works.
This way I can use a LINQ query on the PropertyCollection 'properties' to find whichever custom property I need, using it's value or whatever parameter I like.
Modifying values, you must use the method Dan described above, using
Excellent, glad you got it sorted Martin :)
As a complete aside, this article from yesterday's Umbraco advent calendar is a really nice reference for real-world membership integration: http://24days.in/umbraco/2014/dealing-with-members/.
is working on a reply...