Developer forum

Forum » Development » Update Item with Notificationsubscriber

Update Item with Notificationsubscriber

Per Jensen
Reply

Hi.

I would like to update a field on a item, when the item is being saved.

Im using the Saving Notification Subscriber, and i can access the ID and Sort. My problem is, that i dont know how to access the data fields in the item. I can see them in the ItemArgs, but how do i update them ?

Per J


Replies

 
Vladimir
Reply

Hi Per,

you should use SerializeTo/DeserializeFrom  methods:

   var itemState = new Dictionary<String, Object>();
   args.Item.SerializeTo(itemState);
   // update itemState as you want
   args.Item.DeserializeFrom(itemState);

Best regards,

Vladimir

 
Nicolai Høeg Pedersen
Reply

Hi Per

On the ItemArgs you have an Item property - that will give you access to the item and its field values.

There are 2 different notifications when an item is saved:

Notifications.Items.Saving is broadcasted before the item is persisted to db

Notifications.Items.Saved is broadcasted after the item is persisted to db

So you should use the Saving notification.

Something like this:

using Dynamicweb;

namespace Dynamicweb.Examples.CSharp.Notifications.Standard.Item

{

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

public class ItemSavingObserver : Dynamicweb.Extensibility.NotificationSubscriber

{

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

{

if (args == null)

return;

Dynamicweb.Notifications.Items.ItemArgs itemArgs = (Dynamicweb.Notifications.Items.ItemArgs)args;

itemArgs.ToItem(itemArgs.Item)["MyField"] = "New value";

//itemArgs.Item

//TODO: Add code here

}

}

}
 
Jan Sangill
Reply

Hi,

In DW9 - the Notification reference have been updated, but not all the way through.

This is how it will work in DW9:
Dynamicweb.Notifications.Items.ItemUpdateArgs itemArgs = args as Dynamicweb.Notifications.Items.ItemUpdateArgs;
However, this is deprecated behaviour.

Should be :
Dynamicweb.Notifications.ItemNotification.ItemUpdateArgs itemArgs = args as Dynamicweb.Notifications.ItemNotification.ItemUpdateArgs;

Dont know where else to report these type of "bugs:" than here)

 

 
Vladimir
Reply

Hi Jan,

Thank you for notice - we will fix it asap.

 

Kind regards,

Vladimir

 
Kristian Kirkholt Dynamicweb Employee
Kristian Kirkholt
Reply

Hi Per and Jan

The problem TFS#32401 "Obsolete notifications are used in item repository" has now been fixed in version 9.2.12

You are able to find this build in the download section:

http://doc.dynamicweb.com/releases-and-downloads/releases

Please contact Dynamicweb Support if you need any additional help regarding this.

Kind Regards
Dynamicweb Support
Kristian Kirkholt

 

You must be logged in to post in the forum