Blog about Microsoft technologies, Office System, SharePoint, MOSS, WSS, Search and "The new world of work"
in

Copying files and versions without losing created and modified user and datetime

During one of my projects we are writing a migration application for converting WSS 2.0 data to WSS 3.0 data. Yes, i know there are several methods from Microsoft to do migration but all of them were not sufficient enough :) So we are writing our own. One of the problems we encountered was copying files and their versions stored in SPFile and SPFileVersion objects. Trying a lot, following Microsoft documentation and reading a lot of discussions on the internet, we finally came up with the solution.

SPSecurity.RunWithElevatedPrivileges(delegate()
{
  SPGlobalAdmin admin = new SPGlobalAdmin();
  SPVirtualServer vs = admin.OpenVirtualServer(new Uri(http://someurl/));

  string fromDocTitle = "DocFrom";
  string toDocTitle = "DocTo";

  SPWeb rootWeb = vs.Sites[0].RootWeb;
  SPFileCollection collFrom = rootWeb.Folders[fromDocTitle].Files;
  SPFileCollection collTo = rootWeb.Folders[toDocTitle].Files;

  byte[] binFrom = null;
  SPFile file = null;
  string url = "";

  foreach (SPFile fileFrom in collFrom)  
  {
    SPFileVersionCollection versions = fileFrom.Versions;

    foreach (SPFileVersion version in versions)
    {
      url = fileFrom.Url.Replace(fromDocTitle, toDocTitle);
      binFrom = version.OpenBinary();

      file = collTo.Add(url, binFrom, version.CreatedBy, version.CreatedBy, version.Created, version.Created);

      file.Item["Created"] = version.Created;
      file.Item["Modified"] = version.Created;
      file.Item.UpdateOverwriteVersion();
    }

    url = fileFrom.Url.Replace(fromDocTitle, toDocTitle);
    binFrom = fileFrom.OpenBinary();

    file = collTo.Add(url, binFrom, fileFrom.ModifiedBy, fileFrom.ModifiedBy, fileFrom.TimeCreated, fileFrom.TimeLastModified);

    file.Item["Created"] = fileFrom.TimeCreated;
    file.Item["Modified"] = fileFrom.TimeLastModified;
    file.Item.UpdateOverwriteVersion();
  }
});

» Trackbacks & Pingbacks

    No trackbacks yet.
Trackback url for this post:
http://www.bloggix.com/blogs/microsoft/trackback.ashx?PostID=102

» Comments

    No comments yet. Be the first to comment!

» Leave a Comment

(required) 
(optional)
(required) 

Submit

Subscriptions

News

SlideShare.net
I'm using SlideShare.net for sharing my presentation material. You can find it here

Dutch SharePoint User Group
The Dutch SharePoint User Group is an independent social network for people to collaborate and share information around SharePoint Technology and products. By learning from others you can improve your skills and expertise, around areas, which are then again a benefit to the social network. This platform is a community platform for SharePoint developers, architects and designer who want to share information, tricks, code and content. Its also a good starting point for people without SharePoint knowledge who are interested in SharePoint Technology. Read more

Oktober 2008 i'm working for a new company called Sparked. The new world of work causes organizations to re-think about their ways of communication and interaction with their customers.

Increasing usage of web technology and the new generation workers brings changes into these organizations.

Sparked helps these organizations to accelerate adoption of this technology platform to effectively use it around content management, content exposure and collaboration.


April 2008 i have been to the first SharePoint Conference in Dubai. Dubai is an overwhelming town with a lot of passionate people. I had a great time. Thank you my friends!



You can also find my on Facebook

Tags

Alexander Meijers

Previous Blog about SharePoint