I am trying to automatically assign new members to a specific membergroup.
I have written this code, to hook into MemberService.Created, but the new group isn't saved to the member (when looking in the back office). Am I doing something wrong?
The Log.Add is fired, and I can see in my db that both Id and Name of the new member is correct.
using Umbraco.Core;
using Umbraco.Core.Events;
using Umbraco.Core.Logging;
using Umbraco.Core.Models;
using Umbraco.Core.Services;
using umbraco.BusinessLogic;
namespace Umbraco_Site_Extensions.memberExtensions
{
public class RegisterEvents : ApplicationEventHandler
{
protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
{
MemberService.Created += MemberService_Created;
}
void MemberService_Created(Umbraco.Core.Services.IMemberService sender, Umbraco.Core.Events.NewEventArgs e)
{
sender.AssignRole(e.Entity.Id, "Group name");
Log.Add(LogTypes.Notify, 1, "Added " + e.Entity.Id + " - " + e.Entity.Name + " to Group name.");
}
}
}
This behavior is known, I've noticed as well and have found older posts confirming it. To me it seems like a bug, it's certainly not the expected behavior.
I noticed that if I debug my code, and look in the database for every line of code, I can see that the member and group is added to the relationship table. But once the callback method reaches its end, that database entry is removed. Why? I have no idea. I've been wanting to inspect Umbraco's source and see what happens but havent had the time. Perhaps this coming weekend :)
@David SE - Hi David, did you ever manage to find out what was causing the issue? I've been trying to use .AssignRole this morning and found it wasn't saving, after a bit of searching I eventually found this tread which shows its not just me getting stuck.
I'm on 7.2.1 and it looks like this is an issue for OnSaving/OnSaved as well.
Looking at the umbraco source code, it looks like in Umbraco.Web.Editors.MemberController.PostSave, the code Create/Updates the member, which I'm pretty sure triggers the event handlers. After that completes, it then removes and adds membership groups based on what is passed in, presumably what's coming from the page; so that's overwriting any Roles/membership groups are being done in the event handler.
AssignRole not saving
I am trying to automatically assign new members to a specific membergroup.
I have written this code, to hook into MemberService.Created, but the new group isn't saved to the member (when looking in the back office). Am I doing something wrong?
The Log.Add is fired, and I can see in my db that both Id and Name of the new member is correct.
This behavior is known, I've noticed as well and have found older posts confirming it. To me it seems like a bug, it's certainly not the expected behavior.
I noticed that if I debug my code, and look in the database for every line of code, I can see that the member and group is added to the relationship table. But once the callback method reaches its end, that database entry is removed. Why? I have no idea. I've been wanting to inspect Umbraco's source and see what happens but havent had the time. Perhaps this coming weekend :)
@David SE - Hi David, did you ever manage to find out what was causing the issue? I've been trying to use .AssignRole this morning and found it wasn't saving, after a bit of searching I eventually found this tread which shows its not just me getting stuck.
Cheers,
Dave G
Was this ever solved?
Unfortunately not :(
Did you manage to find a workaround Søren?
After looking on the issue tracker it seems many people have had the problem for over a year - not specifically assignrole, it is any member property.
No, instructed the client to remember to add new members to the specific group.
I made a post yesterday showing the same behavior while trying to save a property: https://our.umbraco.org/forum/developers/extending-umbraco//67380-cannot-update-member-property-on-created-event
Is there a workaround for this? Thanks!
I never found a solution Alejandro, unfortunately.
Thanks Matt.
I checked the tracker and the issue was reported on March 05. http://issues.umbraco.org/issue/U4-6366
I'm on 7.2.1 and it looks like this is an issue for OnSaving/OnSaved as well.
Looking at the umbraco source code, it looks like in Umbraco.Web.Editors.MemberController.PostSave, the code Create/Updates the member, which I'm pretty sure triggers the event handlers. After that completes, it then removes and adds membership groups based on what is passed in, presumably what's coming from the page; so that's overwriting any Roles/membership groups are being done in the event handler.
I can't find any work around to it.
is working on a reply...