1

I'm want to move documents between site collections retaining their version history(Major and Minor versions). I used this blog http://bit.ly/NPftRE as a reference and was able to move documents with versions retaining the modified date and modified by for minor versions. But for Major version document it shows the modified date as current date. Below is the partial view of the powershell script.

if ($isMajorVersion)
    {
        if ($fileDestination.CheckOutType -eq "Online")
        {
            $newItem = $fileDestination.Item
            $newItem["Modified"] = $dateModified.ToLocalTime()
            $newItem.UpdateOverwriteVersion()

            $fileDestination.CheckIn($checkInComment)
            $fileDestination.Publish($checkInComment)
        }
    }
    else
    {
        $newItem = $fileDestination.Item
        $newItem["Created"] = $dateCreated
        $newItem["Modified"] = $dateModified
        $newItem.UpdateOverwriteVersion();

        if ($fileDestination.CheckOutType -eq "Online")
        {
            $fileDestination.CheckIn($checkInComment)
        }
    }

I used the UpdateOverwriteVersion before publishing the document but after the call to Publish() method the modified date (on version history view) changes to current date.

Any suggestions?

1

As per the blog you need to pass created and modified dates while adding the file to destination library:

SPFile fileDest = libDest.RootFolder.Files.Add(
                 urlDestFile, 
                 streamFile, 
                 hashSourceProp, 
                 userCreatedBy, 
                 userModifiedBy, 
                 dateCreatedOn, 
                 dateModifiedOn, 
                 strVerComment, 
                 true);

And when it is a major version, you simply need to call Publish:

if (bolMajorVer)
/*Here we're checking if this is a major version and calling the publish method, passing in the check-in comments.  Oddly when the publish method is called the passed created and modified dates are displayed in the SharePoint UI properly without further adjustment.*/
          fileDest.Publish(strVerComment);

And you need to set created and modified dates only when it is a minor version:

else
     {
/*Setting the created and modified dates in the SPListItem which corrects the display in the SharePoint UI version history for the draft versions.*/
          SPListItem itmNewVersion = fileDest.Item;
          itmNewVersion["Created"] = dateCreatedOn;
          itmNewVersion["Modified"] = dateModifiedOn;
          itmNewVersion.UpdateOverwriteVersion();
     }

However, in your code, you are trying to update the created and modified dates in both major and minor versions.

| improve this answer | |
2

I found a solution:

First, publish the newly copied file in the needed user-context (parameter "userToken"). To change the "Modified"-Date, you need to turn off the "Minor-Versioning" for the complete DocLib. At the end, just turn it on again.

        // Publish in specific user context
        using (SPSite site = new SPSite(siteUrl, userToken))
        {
            using (SPWeb web = site.OpenWeb())
            {
                bool unsafeUpdateStatus = web.AllowUnsafeUpdates;
                web.AllowUnsafeUpdates = true;

                SPFile file = web.GetFile(fileUrl);
                file.Publish(fileComment);

                web.AllowUnsafeUpdates = unsafeUpdateStatus;
            }
        }

        // Edit modified date in administrator context
        SPSecurity.RunWithElevatedPrivileges(delegate()
        {
            using (SPSite site = new SPSite(siteUrl))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    bool unsafeUpdateStatus = web.AllowUnsafeUpdates;
                    web.AllowUnsafeUpdates = true;

                    SPList targetLib = web.Lists.TryGetList(destinationLibraryTitle);
                    targetLib.EnableMinorVersions = false;
                    targetLib.Update();

                    SPFile file = web.GetFile(fileUrl);
                    file.Item[SPBuiltInFieldId.Modified] = modifiedDate;

                    file.Item.UpdateOverwriteVersion();

                    targetLib.EnableMinorVersions = true;
                    targetLib.Update();

                    web.AllowUnsafeUpdates = unsafeUpdateStatus;
                }
            }
        });
    }
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.