Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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?

share|improve this question
add comment

2 Answers

up vote 3 down vote accepted

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.

share|improve this answer
1  
I missed that detail ;) Rob's right, .Cancel and .ErrorMessage only have any effect in the synchronous method, ItemCheckingOut. –  James Love Sep 6 '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 6 '10 at 14:58
add comment

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.

share|improve this answer
    
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 6 '10 at 14:26
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.