Search This Blog

Thursday, January 26, 2012

Validate comments at the time of Check in of a document

When a document library has "Require documents to be checked out before they can be edited" enabled and if the comments are required - there is no out of the box way to do so.

Well here is a small trick to get that functionality:

Write a cusom event handler on ItemCheckingIn which validates if the comments are entered; and if not - Cancels the check in with a error message shown to the end user. In the following code snippett an additional check is made for length of characters entered which can ofcourse be removed or changed to a different length. Would be ideal if that length is read from another List which could be called as Config list for a project hence if it was 20 chars - change the config list and we are done. 

public override void ItemCheckingIn(SPItemEventProperties properties)
string checkInComment = properties.AfterProperties["vti_sourcecontrolcheckincomment"]  as string;
//check for comments being blank or length less than some number of chars.
 if (string.IsNullOrEmpty(checkInComment) || checkInComment.Trim().Length < 5)
      properties.ErrorMessage = "The Version Check in comments should be atleast 5  characters long.";

properties.Cancel = true  


The key above is the property "vti_sourcecontrolcheckincomment" which contains the comments entered by user. Just to re-iterate ChekingIn event is raised before the document is checked in.

-- Mohan