locked
Copying Documents With Version History RRS feed

  • Question

  • 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?

    • Edited by RonaldKors Wednesday, July 9, 2014 8:18 AM
    Wednesday, July 9, 2014 7:35 AM

Answers

  • Hi Jason,

    Thanks for the reply, I found out that the add function doesn't work so well. Apparently when you pass along the usermodifiedby and usercreatedby properties they are visible when you access them via the SharePoint site, but when you use code, you get system account as the value (probably because the documents are added with elevated priviliges). I just used impersonation some temporary custom permissions as a workaround.

    • Marked as answer by RonaldKors Monday, July 21, 2014 12:02 PM
    Monday, July 21, 2014 12:02 PM

All replies

  • Hi,

    According to your post, my understanding is that you wanted to copy documents with version history.

    I had written a simple demo to copy files to another library, and modified the code as below,  you can refer to the following code snippets to check whether it works.

    public static void CopyFileWithVersion()
    
            {
    
                using (SPSite site = new SPSite("YourSiteURL"))
    
                {
    
                    using (SPWeb web = site.OpenWeb())
    
                    {
    
                        SPList sourceLib = web.Lists["Lib1"];
    
                        SPList DestinationLib = web.Lists["Lib2"];
    
    
                        SPListItem itemSource = sourceLib.GetItemById(2);
    
    
                        SPFile fileSource = itemSource.File;
    
                        SPUser userCreatedBy = fileSource.Author;
    
                        
    
                        DateTime dateCreatedOn = fileSource.TimeCreated.ToLocalTime();
    
                        //Get the versions
    
                        int countVersions = itemSource.File.Versions.Count;
    
                                            for (int i = 0; i <= countVersions; i++)
    
                        {
    
                            Hashtable hashSourceProperties;
    
                            Stream streamFile;
    
                            SPUser userModifiedBy;
    
                            DateTime dateModifiedOn;
    
                            string strVersionComment = "";
    
                            bool bolMajorVerison = false;
    
                            if (i < countVersions)
    
                            {
    
                                SPFileVersion fileSourceVerison = itemSource.File.Versions[i];
    
                                hashSourceProperties = fileSourceVerison.Properties;
    
                                userModifiedBy = (i == 0) ? userCreatedBy : fileSourceVerison.CreatedBy;
    
                                dateModifiedOn = fileSourceVerison.Created.ToLocalTime();
    
                                strVersionComment = fileSourceVerison.CheckInComment;
    
                                bolMajorVerison = fileSourceVerison.VersionLabel.EndsWith("0") ? true : false;
    
                                streamFile = fileSourceVerison.OpenBinaryStream();
    
                            }
    
                            else
    
                            {
    
                                userModifiedBy = fileSource.ModifiedBy;
    
                                dateModifiedOn = fileSource.TimeLastModified;
    
                                hashSourceProperties = fileSource.Properties;
    
                                strVersionComment = fileSource.CheckInComment;
    
                                bolMajorVerison = fileSource.MinorVersion == 0 ? true : false;
    
                                streamFile = fileSource.OpenBinaryStream();
    
                            }
    
                            string urlDestinationFile = DestinationLib.RootFolder.Url + "/" + fileSource.Name;
    
    
                            SPFile fileDestination = DestinationLib.RootFolder.Files.Add(
    
                                        urlDestinationFile,
    
                                        streamFile,
    
                                        hashSourceProperties,
    
                                        userCreatedBy,
    
                                        userModifiedBy,
    
                                        dateCreatedOn,
    
                                        dateModifiedOn,
    
                                        strVersionComment,
    
                                        true);
    
    
                            SPListItem itmNewVersion1 = fileDestination.Item;
    
                            itmNewVersion1["Created"] = dateCreatedOn;
    
                            itmNewVersion1["Modified"] = dateModifiedOn;
    
                            itmNewVersion1.UpdateOverwriteVersion();
    
                           
    
                        }
    
                    }
    
                }
    
    
    
    
            }
    

    Thanks,
    Jason
    Forum Support
    Please remember to mark the replies as answers if they help and unmark them if they provide no help. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    Jason Guo
    TechNet Community Support

    • Marked as answer by Lindali Sunday, July 20, 2014 9:48 AM
    • Unmarked as answer by RonaldKors Monday, July 21, 2014 12:02 PM
    Thursday, July 10, 2014 9:22 AM
  • Hi Jason,

    Thanks for the reply, I found out that the add function doesn't work so well. Apparently when you pass along the usermodifiedby and usercreatedby properties they are visible when you access them via the SharePoint site, but when you use code, you get system account as the value (probably because the documents are added with elevated priviliges). I just used impersonation some temporary custom permissions as a workaround.

    • Marked as answer by RonaldKors Monday, July 21, 2014 12:02 PM
    Monday, July 21, 2014 12:02 PM