Saturday, January 05, 2008

Copy files between document libraries

If you wish to copy files between SharePoint sites or document libraries you cannot use the built-in function, CopyTo, found in the SPFile object, as this only works within the same document library :-(

Instead, you need to traverse all versions of a document, which raises a critical issue. How to ensure the created, createdby, modified and modifiedby properties to be the same in the new document as in the old.

SPFileCollection contains an Add method to add new files. Unfortunately it doesn't exists with all the parameters needed, i.e. properties as well as created, createdby, modified and modifiedby. This method is missing:
SPFileCollection.Add (String, Byte[], Hashtable, Boolean, SPUser, SPUser, DateTime, DateTime)

As this method doesn't exists you have two options.
  1. Copy document with properties - and fix dates and users manually afterwards
  2. Copy document with dates and users - and fix properties manually afterwards

Both methods raises some issues - as you need to manipulate the SPFile and SPListItem object directly.

Manually manipulating createdby can be done as sketched below:

listitem["Created"] = new DateTime(2006, 11,1);
listitem["Modified"] = new DateTime(2006, 12,1);
listitem.UpdateOverwriteVersion();

as outlined on MSDN. Unfortunately this code doesn't work :-(

Other ideas very much appreciated.

Check out other blogs, such as this. Unfortunately it doesn't addess all the issues.

Hint to a solution?

Various investigations led me to analyzing the properties of the listitem after having added the item. It turns out that:

listitem[SPBuiltInFieldId.Created]

listitem[SPBuiltInFieldId.Created_x0020_Date]

are not equal! Created_x0020_Date contains the value of li["Created"] whereas SPBuiltInField.Created was the time the document was added to SharePoint.

This is even the case in situations where you use the

SPFileCollection.Add (String, Byte[], SPUser, SPUser, DateTime, DateTime)

which was a surpise to me !

4 comments:

Bedre softwareudvikling og alt andet said...

Hi Morten

Perhaps the issue regarding the ID of the original creator of the entry could be handled as suggested by the accepted answer:
http://stackoverflow.com/questions/624549/add-elements-to-an-an-announcements-list-for-a-specific-user

Anonymous said...

What a great web log. I spend hours on the net reading blogs, about tons of various subjects. I have to first of all give praise to whoever created your theme and second of all to you for writing what i can only describe as an fabulous article. I honestly believe there is a skill to writing articles that only very few posses and honestly you got it. The combining of demonstrative and upper-class content is by all odds super rare with the astronomic amount of blogs on the cyberspace.

Anonymous said...

maybe this http://dandd.codeplex.com solution can do the trick

Anonymous said...

This does not inform how to copy libraries in sharepoint. How do you make a library a template?