Remote Event Receivers: Identify when a document's metadata is first completed

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.)

Advertisements
Remote Event Receivers: Identify when a document's metadata is first completed

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s