I'm trying to now update a custom property for a member. I have been looking through the Umbraco documentation, but it appears to be out of date as none of what I try appears to work?
// Get the details of the user currently logged in
var profileModel = Members.GetCurrentMemberProfileModel();
var profile = ProfileBase.Create(profileModel.Email);
profile.SetPropertyValue("city", model.City);
// profile["city"] = model.City;
profile.Save();
I get the error message "The property "city" is not recognised" on any of the above attempts.
Here's how you'd update a custom member property - assuming you have a member and the member is logged in:
// Get the member var member = Member.GetCurrentMember();
// Check if there is a current member
if (member != null)
{ // Update member properties
member.getProperty("city").Value = model.City;
If I try "var member = Member.GetCurrentMember();" I get the error message "The name Member does not exist in the current context"? I have also tried "var member = Umbraco.Core.Services.Member.GetCurrentMember();" to no avail.
Where is this happening (razor view, controller etc)? What assemblies are you referencing? Are you creating a member and updating the properties in one go, or do you already have the member created and logged in?
I did some research and it turns out Umbraco 7 has overhauled the Member Service API, which is why very little of the documentation I can find online is actually working. In case anyone else is looking at how to do this in future, I've copied the working code below:
// Check if the user is logged in
if (!User.Identity.IsAuthenticated)
{
// Send the user to the login page
Redirect("/login");
}
// The model is not valid yet
if (!ModelState.IsValid)
{
// Display the current Umbraco page (do not redirect)
return CurrentUmbracoPage();
}
// Create new instance of "MemberService"
var memberService = Services.MemberService;
// Get the details of the user currently logged in
var profileModel = Members.GetCurrentMemberProfileModel();
// Get the custom properties for the member
var member = memberService.GetByEmail(profileModel.Email);
// Check if the email address is in the database
if (member == null)
{
ModelState.AddModelError("", "Sorry, we could not find the user to update");
return CurrentUmbracoPage();
}
// Update the member properties
member.Properties["city"].Value = model.City;
// Save in Umbraco
memberService.Save(member);
...yielded a null value, it seems MemberService is the only way to go as the Membership helper is broke.
However this did work for me.
// Create new instance of "MemberService"
var memberService = Services.MemberService;
// Expose the custom properties for the member
var member = memberService.GetByEmail(model.EmailAddress);
// Update the member properties
member.Properties["company"].Value = "Set Property";
// Save the object
memberService.Save(member);
The membership helper is only used to get the current email address anyhow which was in my login page model already.
Note the square brackets getting your property references.
As "umbraco.cms.businesslogic.member" is now deprecated its the only way to go without rolling your own.
Update a member property in Umbraco 7
Hi again,
I'm trying to now update a custom property for a member. I have been looking through the Umbraco documentation, but it appears to be out of date as none of what I try appears to work?
I get the error message "The property "city" is not recognised" on any of the above attempts.
Thanks for any help!
Hi Steven,
As I said yesterday I am not a backend developer, but I will try to help, as much as I can.
Have you seen this documentation about MemberShipHelpers:
http://our.umbraco.org/Documentation/Reference/Querying/MemberShipHelper/index
There is a method, called .UpdateMemberProfile(); in the bottom of the, maybe you can use this method to update your profile.
Hope this helps,
/Dennis
Hi Steven,
For Umbraco 7 it would be best to use the new Member Service API - the documentation is up to date: http://our.umbraco.org/documentation/Reference/Management-v6/Services/MemberService.
Here's how you'd update a custom member property - assuming you have a member and the member is logged in:
Hope this helps...
Hi Dan,
If I try "var member = Member.GetCurrentMember();" I get the error message "The name Member does not exist in the current context"? I have also tried "var member = Umbraco.Core.Services.Member.GetCurrentMember();" to no avail.
Thanks, Steven.
Where is this happening (razor view, controller etc)? What assemblies are you referencing? Are you creating a member and updating the properties in one go, or do you already have the member created and logged in?
Hi Dan,
Thanks for replying, appreciate it. This is in a controller and the user is already logged in at this point.
The assembly references I am using are:
Thanks again, Steven.
Try adding these references:
Hi again,
I did some research and it turns out Umbraco 7 has overhauled the Member Service API, which is why very little of the documentation I can find online is actually working. In case anyone else is looking at how to do this in future, I've copied the working code below:
This didn't work for me.
...yielded a null value, it seems MemberService is the only way to go as the Membership helper is broke.
However this did work for me.
The membership helper is only used to get the current email address anyhow which was in my login page model already.
Note the square brackets getting your property references.
As "umbraco.cms.businesslogic.member" is now deprecated its the only way to go without rolling your own.
is working on a reply...