Hello,
I am working on a ribbon button for SharePoint 2013 with which users (with contribute permissions) can open a dialog to move selected documents to a different library (with the same content type) and enter soms Metadata to be set for the document. One of the requirements for this is that the version History should be copied along with the document. Of course I googled for some code that allready does what I want to do and i stumbled upon this blog: http://sharepointvenividivici.typepad.com/sharepoint-customization/2011/06/maintain-file-version-history-when-movingcopying-files-between-sharepoint-sites.html
Because this code also sets the modified and created information for the document the code runs under elevated priviliges because the users do not have the rights to modify that data.
At first this seemed to be the perfect solution, but after a while we found something truly annoying. The first time you copy the document it works perfectly and the document including version history is being transferred. But the second time the document does get copied with the version history but in the version history the modified by is set to System Account for every version.
Does anyone know what can cause this?
After doing some more digging, I believe that the problem lies with the following function
SPFile fileDest = libDest.RootFolder.Files.Add(
urlDestFile,
streamFile,
hashSourceProp,
userCreatedBy,
userModifiedBy,
dateCreatedOn,
dateModifiedOn,
strVerComment,
true);
This function apparently sets the Created, Created By, Modified and Modified By properties to the values you provide, but when you later try to read this properties with code you get the userwith which you ran the code and not the user you provided. Does anyone know a way to fix this?