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.
- Copy document with properties - and fix dates and users manually afterwards
- 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 !