Thursday, April 23, 2015

Preserve properties after overriding document

The value from SharePoint columns for Office documents is actually stored in the document. When user updates the column values in the SharePoint document library(Edit Form), the values are updated in document itself. Columns included in the content type are represented as properties in the content type schema stored in the document. They are identified in the document Management node of the properties element in the schema. These document properties map to document library columns, represented by Field elements in the content type definition stored in the document library.

When user overrides document with different properties in SharePoint document library then it overwrites the current values.  Following are the ways to preserve the old column values:

  • Checkout and update document content
    Checkout the document and open it from SharePoint. Update the document content with new data and check in.
  • Edit document and update the properties
    You can open the document in Client(Word, Excel etc) and update the the properties. After that upload the document to SharePoint.

SharePoint does not provide any event to check if document is overrode. One of my client wanted to preserve the old values after override the document. I came up with following code to preserve the values. Its not full proof. It only works with new document for which properties are not set or the document is not uploaded to SharePoint previously. Same time Title field should be mandatory field. This code fires on ItemUpdated list item event. Following is the code:

Above code helped me to preserve the properties in case user is uploading new document without setting properties. I hope this will help someone. Add comment if anyone knows other workaround.