3

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?

Drake
  • 435
  • 2
  • 10
  • 19

2 Answers2

5

Are you intentionally using the asynchronous (after) event? I would suspect that displaying an error message would work better if you would override the ItemCheckingOut event instead.

Rob Wilson
  • 4,180
  • 2
  • 24
  • 30
  • 2
    I missed that detail ;)

    Rob's right, .Cancel and .ErrorMessage only have any effect in the synchronous method, ItemCheckingOut.

    – James Love Sep 06 '10 at 14:58
  • Your are right! If have just found it too, Cancel works only on ItemCheckingOut, for ItemCheckedOut has no sense. Thank you. – Drake Sep 06 '10 at 14:58
0

Are you sure the code actually executes? Use System.Diagnostics.Trace.Write to write something to the trace log at some point and use DebugView (http://technet.microsoft.com/en-us/sysinternals/bb896647.aspx) to monitor the output of the trace log.

James Love
  • 25,512
  • 2
  • 45
  • 77
  • I am deploying it sandboxed directly from Visual Studio 2010, and it is executed. I have a break point at the beginning of the function and it runs all steps till the exit. – Drake Sep 06 '10 at 14:26