Advertisement
Guest User

Untitled

a guest
Mar 27th, 2015
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.89 KB | None | 0 0
  1. SPFile fileSource = itmSource.File;
  2. /*Here we'll get the created by and created on values from the source document.*/
  3. SPUser userCreatedBy = fileSource.Author;
  4. /*Note we need to convert the "TimeCreated" property to local time as it's stored in the database as GMT.*/
  5. DateTime dateCreatedOn = fileSource.TimeCreated.ToLocalTime();
  6. //Get the versions
  7. int countVersions = itmSource.File.Versions.Count;
  8. /*This is a zero based array and so normally you'd use the < not <= but we need to get the current version too which is not in the SPFileVersionCollection so we're going to count one higher to accomplish that.*/
  9. for (int i = 0; i <= countVersions; i++)
  10. {
  11. Hashtable hashSourceProp;
  12. Stream streamFile;
  13. SPUser userModifiedBy;
  14. DateTime dateModifiedOn;
  15. string strVerComment = "";
  16. bool bolMajorVer = false;
  17. if (i < countVersions)
  18. {
  19. /*This section captures all the versions of the document and gathers the properties we need to add to the SPFileCollection. Note we're getting the modified information and the comments seperately as well as checking if the version is a major version (more on that later). I'm also getting a stream object to the file which is more efficient than getting a byte array for large files but you could obviously do that as well. Again note I'm converting the created time to local time.*/
  20. SPFileVersion fileSourceVer = itmSource.File.Versions[i];
  21. hashSourceProp = fileSourceVer.Properties;
  22. userModifiedBy = (i == 0) ? userCreatedBy: fileSourceVer.CreatedBy;
  23. dateModifiedOn = fileSourceVer.Created.ToLocalTime();
  24. strVerComment = fileSourceVer.CheckInComment;
  25. bolMajorVer = fileSourceVer.VersionLabel.EndsWith("0") ? true : false;
  26. streamFile = fileSourceVer.OpenBinaryStream();
  27. }
  28. else
  29. {
  30. /*Here I'm getting the information for the current version. Unlike in SPFileVersion when I get the modified date from SPFile it's already in local time.*/
  31. userModifiedBy = fileSource.ModifiedBy;
  32. dateModifiedOn = fileSource.TimeLastModified;
  33. hashSourceProp = fileSource.Properties;
  34. strVerComment = fileSource.CheckInComment;
  35. bolMajorVer = fileSource.MinorVersion == 0 ? true : false;
  36. streamFile = fileSource.OpenBinaryStream();
  37. }
  38. string urlDestFile = libDest.RootFolder.Url + "/" + fileSource.Name;
  39. /*Here I'm using the overloaded Add method to add the file to the SPFileCollection. Even though this overload takes the created and modified dates for some reason they aren't visible in the SharePoint UI version history which shows the date/time the file was added instead, however if this were a Microsoft Word document and I opened it in Word 2010 and looked at the version history it would all be reflective of the values passed to this Add method. I'm voting for defect but there could just be something I'm missing.*/
  40. SPFile fileDest = libDest.RootFolder.Files.Add(
  41. urlDestFile,
  42. streamFile,
  43. hashSourceProp,
  44. userCreatedBy,
  45. userModifiedBy,
  46. dateCreatedOn,
  47. dateModifiedOn,
  48. strVerComment,
  49. true);
  50. if (bolMajorVer)
  51. /*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.*/
  52. fileDest.Publish(strVerComment);
  53. else
  54. {
  55. /*Setting the created and modified dates in the SPListItem which corrects the display in the SharePoint UI version history for the draft versions.*/
  56. SPListItem itmNewVersion = fileDest.Item;
  57. itmNewVersion["Created"] = dateCreatedOn;
  58. itmNewVersion["Modified"] = dateModifiedOn;
  59. itmNewVersion.UpdateOverwriteVersion();
  60. }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement