R – SharePoint Document Library version restore not behaving as expected

sharepoint

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.

The versions page clearly displays multiple versions and restoring through the UI restores the desired file.

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

Can anyone provide any insight?

Best Answer

The versions page generates a version entry for each relevant combination of file and item versions. When SPFile.Versions is 1, this indicates that the file has one version in addition to the current version. SPListItem.Versions contains all versions of the file, including the current version (at index 0).

When SPFile.Versions.Restore(iIndex) is called, the file is restored along with the associated SPListItem version which applies to this file

Related Topic