Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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
add comment

1 Answer

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
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 Jul 25 '13 at 0:28
add comment

Your Answer

 
discard

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.