Get field value during an event handler.


Hi everyone,

Today I will talk about “event handler” and more specifically those used on elements of lists and libraries.

3 types of events are attached to the lists and libraries: Adding, updating and deleting

Each of its events can be retrieved synchronously (at the time of the action) andasynchronous (right after the end of the action).

It’s very easy to spot if a function is synchronous or asynchronous based on its name.

Action

Synchronous

Asynchronous

Adding an item

ItemAdding

ItemAdded

Updating an item

ItemUpdating

ItemUpdated

Deleting an item

ItemDeleting

ItemDeleted

As you can see, synchronized action’s name ends with “ing” and asynchronous with “ed”.

During these actions, we can know the field values and sometimes the previous value of the field. Ways to retrieve data are:

Properties.ListItem[«FieldName »]

Properties.AfterProperties[«FieldName »]

Properties.BeforeProperties[«FieldName »]

And yes, three possibilities depending on the actions and the synchronicity of the eventcan contain either the new value, or the old value, or nothing at all! And where it reallygets tough, it also depends on the type of list, whether the current list is a list or library.

Given this number of possibilities a little summary table seemed necessary, here it is.

For the lists

Event

ListItem

BeforeProperties

AfterProperties

ItemAdding

X

X

New value

ItemAdded

New value

X

New value

ItemUpdating

Old value

X

New value

ItemUpdated

New value

X

New value

ItemDeleting

Old value

X

X

ItemDeleted

X

X

X

For the libraries :

Event

ListItem

BeforeProperties

AfterProperties

ItemAdding

X

X

X

ItemAdded

X

X

New value

ItemUpdating

Old value

Old value

New value

ItemUpdated

New value

Old value

New value

ItemDeleting

Old value

X

X

ItemDeleted

X

X

X

Those informations are very important to consider when planning a development.

Additional information that is important is that during the upload of a document by a user in a library, adding events are triggered after addition of the upload but before the user can fill in themetadata! This filling triggering the updating events.

If you plan to modify the contents of the properties of this document at the event”ItemAdded”, your users may see a message like this when they will safeguard their metadata:

“File ‘mydoc.pdf’ has been modified by ‘user’ the ‘date'”.

To avoid this error, we can define the event “ItemAdded” as synchronous! Indeed, all asynchronous event can be defined as synchronous!

This will allow us to pre-fill the data in our forms before the user does not grasp anything! Given the asynchronous nature of the ItemAdded, if you change the data content, you are not 100% sure that the form displayed to the user include the new data and thus avoid conflicts backup.

For this we must add the synchronization parameter to our xml definition.


<Receiver>
<Name>EventReceiver1ItemAdded</Name>
<Type>ItemAdded</Type>
<Assembly>$SharePoint.Project.AssemblyFullName$</Assembly>
<Class>TutoTaxonomy.EventReceiver1.EventReceiver1</Class>
<SequenceNumber>10000</SequenceNumber>
<Synchronization>Synchronous</Synchronization>
</Receiver>

In our code we add an update of the column “test”


public override void ItemAdded(SPItemEventProperties properties)
{
base.ItemAdded(properties);
properties.ListItem["test"] = "test synchro";
properties.ListItem.Update();
}

And now, with this little trick our form is pre-filled every time!

Hope this helps!

Christopher.