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 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!

share|improve this question
add comment

4 Answers

up vote 2 down vote accepted

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")

share|improve this answer
add comment

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)

share|improve this answer
add comment

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;
share|improve this answer
add comment

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.

share|improve this answer
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.