1
votes

I'm running a forum with Umbraco 7, and I wish to update a property on a specific member by hes Id.

This is what I tried:

var authorId = Model.Content.GetPropertyValue<int>("postAuthor", 0);
var author = Members.GetById(authorId);


umbraco.cms.businesslogic.member.Member member = umbraco.cms.businesslogic.member.Member.GetMemberFromEmail(author.GetPropertyValue("email").ToString());
member.getProperty("postCounter").Value = Convert.ToInt32(member.getProperty("postCounter")) + 1;
member.Save();

But this dont work and the line below throws this error:

umbraco.cms.businesslogic.member.Member member = umbraco.cms.businesslogic.member.Member.GetMemberFromEmail(author.GetPropertyValue("email").ToString());

It says: Warning: umbraco.cms.businesslogic.member.Member is obsolete: "Use the MemberService and the Umbraco.Core.Models.Member models instead"

Can someone help me solve this?

2

2 Answers

4
votes
var memberService = ApplicationContext.Current.Services.MemberService
var member = memberService.GetById(authorId)
member.SetValue("postCounter", newValue);
memberService.Save(member);

Never, ever do this though!!

You need to store counts like this that update really frequently in your own separate table as each time you save a piece of content (and yes, the member object is basically a piece of content as well) you will save a new version in the versions table. All of your custom properties will also be saved again with the new version. Also this is a fairly database-intense operation which is completely unnecessary, just have a table with two columns: the memberId and the count and you're done and it's all very lean and performant.

1
votes

If you are in razor script you want to do something like:

var authorId = Model.Content.GetPropertyValue<int>("postAuthor", 0);
var ms = ApplicationContext.Current.Services.MemberService;
var member = ms.GetById(authorId);
member.SetValue("postCounter",member.GetValue("postCounter"));

But as sebastian says you probably want to do it differently for performance