views:

195

answers:

4

Hi

I am trying to set the value of a field on a ListItem in an event receiver but its not working

All i am doing during the event is

properties.AfterProperties[<field internal name>] = 1;

No errors are thrown but the the field i'm setting does not change. I have also tried

properties.ListItem[<field internal name>] = 1;
properties.ListItem.Update();

Have also tried SystemUpdate();

I know i am meant to be setting the afterproperties but think i am missing an obvious step.

Thanks

+1  A: 

The ItemUpdating is used for validation. If you want to set the values of fields, do it in the ItemUpdated event instead.

Rob Windsor
I changed my code to use ItemUpdated instead and by using the ListItem.SystemUpdate(false) i get the result i wanted which is the value i am changing to be included in the last version.
Buzzby
A: 

By the way, do not forget to call DisableEventFiring, because guess what happens when you call Update method? ItemUpdated event gets called again and you go into an endless loop...

Janis Veinbergs
A: 

Keep in mind, that depending on whether your event receiver runs on a list or library you may need to use different properties (see this link for more info).

Assuming you are in the ItemUpdating method running on a list, all you should need is:

base.ItemUpdating(properties);
properties.AfterProperties["InternalName"] = 1;

(no updates required since you are changing the value before it gets saved)

I would verify that your Event Receiver is attached to the list. Are you able to debug your Event Receiver when you modify an item in the list?

Kit Menke
The code is defiantly running as I have enterprise logging running. I have the base call after by code so i will try moving that to the top to see if that makes a difference
Buzzby
Hmm.. are you able to log the value of AfterProperties["InternalName"] before you change it?
Kit Menke
I have loged the value before an after changing and can see the value change. It just isn't saved. Rob Windsor is right. It appears no matter what you do to the after properties it doesn't change anything
Buzzby
A: 

You can add the value in the Item Adding event using the AfterProperties.ListItem[]=;

CodenameSantosh
The listitem object does not exist in AfterProperties
Buzzby
yes, so you have to use the AfterProperties.ListItem[]=; in item adding event
CodenameSantosh