Developer forum

Forum » CMS - Standard features » Save user without raise "UserSaved" event

Save user without raise "UserSaved" event

Andreas Pettersson
Reply

Hi,

Is there a way I can save a user without raise the "UserSaved" event? I need to save some user data in the "Usersaved" event but dont want to fire the event again becauase an infinite loop starts.

var newUser = User.GetUserByID(user.Subject.ID);
newUser.ItemId = externKundId;
newUser.ItemType = "Extern_Kund";
newUser.Save();

Regards
Andreas


Replies

 
Nuno Aguiar Dynamicweb Employee
Nuno Aguiar
Reply

Hi Andreas,

 

You can simply check to see if the ItemType is already defined (and/or the ItemId). Just add that to your null checks before that code and you should be fine.

 

Best Regards,

Nuno Aguiar

 
Mario Santos Dynamicweb Employee
Mario Santos
Reply

Hi Andreas,

You can use the UserOnBeforeSave notification (Dynamicweb.Security.UserManagement.Notifications.Notifications.UserOnBeforeSave) to update the properties before it gets saved, this way you avoid the infinite loop. If this is not an option you will have to do some flag check on UserSaved notification. 

Also, the args Subject property is the User object so you can skip the User.GetUserByID(user.Subject.ID) ;-) 

 

BR, Mario

 
Nicolai Pedersen Dynamicweb Employee
Nicolai Pedersen
Reply
This post has been marked as an answer

It is also possible to silence all notifications by using a NotificationContext

using new NotificationContext(NotificationContext.NotificationState.SuppressNotifications) {
    //Notifications are silent here
}

BR Nicolai

Votes for this answer: 1

 

You must be logged in to post in the forum