Add files to the SpFileCollection and update the time of creation WITHOUT creating a new version.

The normal solution for this would be to add the file into the file collection using the appropriate overloaded Add method – with the creation date & modification date parameters along with the normal file parameters. But as we know this Add method doesn’t update the custom time sent as parameters.

To elaborate - if you try to add a file and want some custom creation and modified date which is different from the actual time, you will not be able to provide the dates.

SPFile newFile = newFolder.Files.Add("New File.TXT", inputFile, userAdmin, userSH, createdTime, lastModifiedTime);

The code piece above would add the file but with the actual creation and modidied datetime i.e. current datetime. To provide your custom date times for the creation & modified datetimes, you need to edit the listitem once again. A possible code piece could be:

SPListItem listItem = newFile.Item;

listItem["Created"] = createdTime;

listItem["Modified"] = lastModifiedTime;

listItem.Update();

This code piece works well but there is a catch here. In our problem statement we need to accomplish the above without creating new versions. The Update() method would create a new version for all updates.

Most of the SharePoint developers would suggest using the SystemUpdate() method. Yes this works but to some extent.

SystemUpdate() method collapses all the versions to a single version for all the changes but doesn’t affect the Modified By and the Modified Time columns. As per MSDN’s definition of SystemUpdate() method - Updates the database with changes that are made to the list item without effecting changes in the modified time or modified by fields.

While studying in school, my teacher once told me always read carefully what comes after the clause – “Without”.  So quite clearly, SystemUpdate() would not allow any changes to the modified time field.

The ultimate solution for this problem is achieved through the – UpdateOverwriteVersion() method. Per MSDN’s definition - Updates the item without creating another version of the item.

So finally after so many versions of Update methods we were able to achieve the implementation per our problem statement.

Comments

  • Anonymous
    October 30, 2015
    Old topic, but still true ! Fantastic job man. Thank you Ben