0

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?

1 Answer 1

1

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];
4
  • 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
    Commented Jul 23, 2013 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. Commented Jul 24, 2013 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. Commented Jul 24, 2013 at 19:23
  • 1
    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
    Commented Jul 25, 2013 at 0:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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