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 have some code which I am attempting to use to "rollback" a SharePoint document and it's metadata properties to a previous version using ootb version control and the SharePoint API. Here is a sample of my code:

int versionCount = item.Versions.Count;
if (versionCount > 0)
{
 // this only restores properties
 item.Versions.Restore(1);
}

The expected result of calling restore using 1 (the newest version before the current version) would be to restore the file and metadata. Instead, only the metadata is restored and the current file is copied to the new "restored" version.

Furthermore, I have attempted to use item.File.Versions only to find item.File.Versions.Count is 1.

Can anyone provide any insight?

share|improve this question

2 Answers 2

make sure that you use the SPFile object to get the Versions property from like below. Restoring an SPListItem will only restore the metadata not the binary document.

SPListItem item = ...
SPFile file = item.File;
int versionCount = file.Versions.Count;
if (versionCount > 0){ 
    file.Versions.Restore(1);
}

Cheers /WW

share|improve this answer
    
The item.File.Versions collection only contains the current version even though the version page is showing the current (2.0) and previous (1.0) versions. –  Dylan Berry Jan 20 '10 at 17:16
    
@Dylan Is your version 2 checked in? I'm not sure, but I would guess there is an alternate API method to retreive drafts if Wictor's code doesn't access them. –  Tom Resing Feb 4 '10 at 15:07

I have found that restoring a previous version will create a new version with information from the 'selected' version. It will not 'restore' and delete the 'current' version. IE: Version 1 Version 2 Version 3

If you restore version 1, it will become version 4, all 4 version will be available afterwards.

share|improve this answer

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.