Developer forum

Forum » Development » Extend New User created event, Existing user updated event

Extend New User created event, Existing user updated event

Dmitrij Jazel
Reply

Hello DW guys,

What events should I subscribe, in order execute some of my custom C# code, after user was created (both in the frone-end) and administation (I think it should be the same event).

And after existing user is modified both in front-end and administration.

Thanks,

Dmitrij


Replies

 
Morten Snedker
Reply
This post has been marked as an answer

Hi Dmitrij,

 

There is no notification for "user created". For both created/saved you'll have to use UserSaved, and then figure out your own logic to measure whether the user is an already-existing user or a new user. Alternatively you may take use of the UserOnBeforeSave notification.

 

 

Example:

using Dynamicweb;

namespace UserNotification
{
    [Dynamicweb.Extensibility.Subscribe(Dynamicweb.Modules.UserManagement.Notifications.UserSaved)]
    public class UserOnBeforeSaveObserver1 : Dynamicweb.Extensibility.NotificationSubscriber
    {
        public override void OnNotify(string notification, Dynamicweb.Extensibility.NotificationArgs args)
        {
            if (args == null || !(args is Dynamicweb.Modules.UserManagement.UserNotificationArgs))
                return;

            Dynamicweb.Modules.UserManagement.UserNotificationArgs item = (Dynamicweb.Modules.UserManagement.UserNotificationArgs)args;

            //TODO: Add code here
        }
    }
}

 

 

The above example is build upon our Visual Studio Templates available from our Download area. These will provide you with ready-to-go structure, ready for your code:

 

 

 

 

 

Regards /Snedker

 

 

 

Votes for this answer: 1
 
Dmitrij Jazel
Reply

Hello Morten,

Thanks for info! :)

Just one quick question than... I am able to get User from args, now what I need is a way to access custom user fields, but my question is:

Where do I find those (set them up), and how do I eventually access them in C#? I am trying to access them like this:

string dateOfBirth = "";
                    foreach (var cf in user.CustomFieldValues)
                    {
                        if (cf.CustomField.SystemName == "DateOfBirth")
                        {
                            dateOfBirth = cf.CustomField.ToString();
                        }
                    }

 

Thanks for help!

//Dmitrij

 

 

 

 

 

 

 
Morten Snedker
Reply

Hi,

Just deleted my previous post - misread your question.

 

Yes, it's a bit hard to find, but try with 

Management Center -> Control Panel -> Modules -> User Management. Choose "Edit custom fields" in the "Custom fields" section.

 

 

In C#

            foreach (var val in user.CustomFieldValues)
            {
                switch (val.CustomField.SystemName)
                {
                    case "myCustomField":
                        string fieldValue = val.Value.ToString(); //if type string
                        break;
                    default:
                        break;
                }
            }

 

 

 

 

Regards /Snedker

 

 

 

 

 

 

 

 

 
Dmitrij Jazel
Reply

Hey Morten :)

No, it was correct, that was exactly what I needed! :-)

And also this. There where 2 things, where to find it and another one was how to access it via API...

So all is ok now! thanks allot! :-) I guess I see you in 2014 and looking forward to see you @DWTC2014 :-)

Hope you can make it this time :-)

 

 

 
Dmitrij Jazel
Reply

Hej Morten,

Actually we didn't test this one good enough, actually the issue is that this event is being fired only when user is being saved in administration.

 

Nothing happens if user is being saved from frontend - that is if we have a "Extranet/Intranet (Extended)" module, with new user registration and edit existing user form.

 

Are those same events (user edit/save in administration) and (user edit/save in frontend with "Extranet/Intranet (Extended)" module)?

 

Regards,

Dmitrij

 

You must be logged in to post in the forum