Developer forum

Forum » Development » Item field event subscriber

Item field event subscriber

António Ramos
Reply

Hi guys,

We have a client that need to send an email when an item field is changed. ie: When dropdown field "status" is changed from waiting to finished.

There is anyway to do this? In last resource we could make a trigger in the database but honestly i dont like this approach.

BR,

António Ramos

 


Replies

 
Nicolai Høeg Pedersen
Reply
This post has been marked as an answer

Hi Antonio

You can use a notification subscriber when the item is saved:

[Dynamicweb.Extensibility.Subscribe(Dynamicweb.Notifications.Items.Saving)]

public class ItemSavesSubscriber : Dynamicweb.Extensibility.NotificationSubscriber

{

public override void OnNotify(string notification, Dynamicweb.Extensibility.NotificationArgs args)

{

if (args == null)

{

return;

}

var loadedArgs = (Dynamicweb.Notifications.Items.ItemArgs)args;

Dynamicweb.EmailHandler.Send(new System.Net.Mail.MailMessage("noreply@dynamicweb.com", "someone@dynamicweb.com"));

}

}

Dynamicweb.Notifications.Items.Saving is executed before the actual persistence to DB. ​You can also use Dynamicweb.Notifications.Items.Saved after it has been persisted.

Votes for this answer: 1
 
António Ramos
Reply

Good! Thank you Nicolai.

BR,

António Ramos

 
António Ramos
Reply

Hi Nicolai,

The variable args is from type ItemUpdateArgs and when i try ( following the example from http://developer.dynamicweb.com/forum.aspx?PID=48&ThreadID=36925 )

loadedArgs.ToItem(loadedArgs.Item)["Field"] = "Test value";

It give me a cast error... "Unable to cast object of type 'ItemUpdateArgs' to type 'ItemArgs'". 

Were made some changes in this version?

DW Version: 8.7.0.3

BR,

António Ramos

 
Nicolai Høeg Pedersen
Reply
This post has been marked as an answer

Hi Antonio

My bad - args cast in my example. Right one below:

[Dynamicweb.Extensibility.Subscribe(Dynamicweb.Notifications.Items.Saving)]

public class ItemSavesSubscriber : Dynamicweb.Extensibility.NotificationSubscriber

{

public override void OnNotify(string notification, Dynamicweb.Extensibility.NotificationArgs args)

{

if (args == null)

{

return;

}

var loadedArgs = (Dynamicweb.Notifications.Items.ItemUpdateArgs)args;

Dynamicweb.EmailHandler.Send(new System.Net.Mail.MailMessage("noreply@dynamicweb.com", "someone@dynamicweb.com"));

}

}

 

Votes for this answer: 1
 
António Ramos
Reply

Hi Nicolai,

It's perfect now.Thank you.

BR,

António Ramos

 

You must be logged in to post in the forum