I am trying to implement a custom logic during the checkout of an item of a list in SharePoint 2010.
For this reason I added an Event Receiver that ovverides the method ItemCheckedOut.
The idea is to block the possibility to checkout a file if its Approval Status is Pending.
This is the code:
Public Overrides Sub ItemCheckedOut(ByVal properties As SPItemEventProperties)
Try
Me.EventFiringEnabled = False
Dim item As SPListItem = properties.ListItem
If item.ModerationInformation.Status = SPModerationStatusType.Pending Then
properties.ErrorMessage = "The document is still 'Pending' and cannot be checked out."
properties.Status = SPEventReceiverStatus.CancelWithError
properties.Cancel = True
End If
Catch ex As Exception
properties.ErrorMessage = String.Format("An error occurred during execution of ItemCheckedOut event: {0}.", ex.Message)
properties.Status = SPEventReceiverStatus.CancelWithError
properties.Cancel = True
Finally
Me.EventFiringEnabled = True
End Try
End Sub
The code is executed correctly when a user try to checkout a file in "Pending" status, but no errors are displayed and checkout operation completes successfully.
Do you know how to solve?
Rob's right, .Cancel and .ErrorMessage only have any effect in the synchronous method, ItemCheckingOut.
– James Love Sep 06 '10 at 14:58