SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am writing an event receiver on a document library.

I need to get the previous version of the document, before the upload. BeforeProperties unfortunately does not give me the SPFile, only the columns. I suppose I can try and go through the document's versions, but that seems ugly, and also it would be nice to not have to have versioning turned on.

Am I missing something inside properties? Do I have to find the previous version? If I do have to find the previous version, what does that code look like?

share|improve this question
up vote 1 down vote accepted

ItemUpdating allows you to get the file before the upload/action has taken place. ItemUpdated requires you to go through version history.

In code you can get the previous version as follows:

SPFile file = ...;
SPFileVersion previousVersion = file.Versions[file.Versions.Count - 1];
share|improve this answer
How would I do it with ItemUpdating then? also, don't you mean file.Versions[file.Versions.Count - 2]? as -1 would get the last item in the collection, meaning the latest version? – Nacht Jul 23 '13 at 23:58
I do mean -1 because file.Versions (Microsoft.SharePoint.SPFileVersionCollection) only contains previous versions. If you want the 'current' version that is just the SPFile reference of course. – Steven Van de Craen Jul 24 '13 at 19:21
In ItemUpdating you can use the SPFile reference to get the version that will be overwritten. It depends on your business scenario if you can actually do your logic in there already or not. – Steven Van de Craen Jul 24 '13 at 19:23
ah! i see... so in ItemUpdating, properties.ListItem.File is the old file, and in ItemUpdated, properties.ListItem.File is the new file... that's different from other stuff I've read, but does make sense with my testing. Thank you – Nacht Jul 25 '13 at 0:28

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.