Friday, January 18, 2008

Avoid checkin when uploading files

When uploading files to SharePoint they sometimes are left in checked out status. If the users don't checkin files after upload, other users cannot see the files.

This can happen even for Document Libraries without versioning - and changing it to avoid checkin is not simple.

You need to ensure at least that:
  • Require documents to be checked out before they can be edited = false
  • No versioning af Document library - probably not required
  • Ensure no mandatory columns exists - or ensure they have default values

Check out this blog for information.

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);

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:



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 !