1

I want to performe some Action on ItemAdding. I have a Custom List that reacts on Item Creation. In particular on Item Adding performs some action using the After Properties..

If, at this level, i want to Delete the item (or better Broke the event chains) how could I do?

The final result must be that the action I want are performed but the item is not added.

Thank you very much!

4 Answers 4

2

Since you are still in the ADDING event, you could cancel the event by setting the appropiate properties on the event SPItemEventProperties object.

For pre SP 2010: Use the Cancel property. This will report an error message to the user - afaik there is no way to avoid it, but you can still customize the message.

public virtual void ItemAdding(SPItemEventProperties properties)
{
    // Your logic here....

    properties.Cancel = true; 
    properties.ErrorMessage = "A custom error message.";
}

For post 2010: Cancel is now deprecated, so use the Status property instead. This is an enum - to cancell the event you have 3 alternatives (taken from msdn):

  • A value of CancelNoError means to silently cancel the request as if the request has succeeded.
  • A value of CancelWithError means to cancel the request with the error message specified in ErrorMessage.
  • A value of CancelWithRedirectUrl means to cancel the request and redirect to the URL specified in RedirectUrl.

Just keep in mind that CancelNoError seems to give problem when used on a document library (the user gets redirect to a file not found page). If this is your case your best bet is to use cancelWithError or CancelWithRedirectUrl (wich could redirect the user to a page explaining why the file adding process was "cancelled")

2

I would suggest you go for the ItemAdded, much easier to handle, also make sure you apply the "Synchronous" attribute in the event registration (<Synchronization>Synchronous</Synchronization>) - this will avoid your event to spawn in different Threads.

Beyond that in SharePoint 2010 you would need to disable event firing as explained here http://buyevich.blogspot.ch/2010/10/disableeventfiring-is-obsolete-in.html

Simply delete the document via standard OM (here with Versions support too) - http://princyswebcrunch.blogspot.ch/2012/02/programmatically-delete-sharepoint-file.html (btw: do not forget to account for Version/Approval if these are enabled on your Library)

1

For SharePoint 2013:

properties.Cancel is now depreciated, use Status instead. Options for status are:

  • CancelNoError
  • Continue
  • CancelWithError
  • CancelWithRedirectUrl (Obsolete - Default list forms are committed through asynchronous XmlHttpRequests, so redirect urls specified in this way aren't followed by default.)

Example:

properties.ErrorMessage = "My error message";
properties.Status  = SPEventReceiverStatus.CancelWithError;
0

All good information here. The problem is that the CheckIn occurs after all event code has been executed and cannot be prevented. Cancel will still execute the checkin and terminate. This occurs in SP2010 with SPItem.Update() and SPItem.Delete(). IN addition, setting the status to not display the error does not work either. No updates at all can be performed in ItemAdded event without an exception stack trace being displayed to the user. There is no fix or work around that I have found for this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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