Document events are a perennial problem in SharePoint. This is, in part, due to the way documents are put into SharePoint:
- You upload the document into SharePoint…
- …which fires ItemAdded …
- …then you complete the metadata…
- …which fires item updated.
So, my problem was that our customer wanted an email sent when a document was first ‘added’ to SharePoint – except that by added they meant “Has been uploaded and it’s metadata completed for the first time”. While SharePoint does, technically, fire the correct events at the correct times, it’s pretty easy to see this ‘business event’ is probably more useful.
So how, in a remote event receiver, can I tell if this is the first update for a document? There’s no special event for it or anything. Well, I realised that after the document was first added, then Created and Modified times should be the same. After the first update, they should always be different. Thus, I checked the before properties for the document’s event.
DateTime created = (DateTime) properties.ItemEventProperties.BeforeProperties["vti_timecreated"]; DateTime modified = (DateTime)properties.ItemEventProperties.BeforeProperties["vti_timelastmodified"]; bool justAdded = (created == modified);
Great!
(As a side note, why is it so complicated as to which type of items have what values in their before and after properties during events?)
(Also, why do we still have to contend with Checking in/out a document also firing the ItemUpdated event? I mean, there is no earthly reason for it! You already have an ItemCheckedIn event to use if you want to! Very frustrating – but good information on how to solve it here.)