2

I am just trying to cancel the default behavior of event when adding Item

 public override void ItemAdding(SPItemEventProperties properties)
    {
        try
        {
            //my code
            SPListItem parentItem = oList.GetItemById(discussionId);
            SPListItem reply =SPUtility.CreateNewDiscussionReply(parentItem);
            reply["Body"] = properties.AfterProperties["Body"];
            reply.Update();

            properties.Status = SPEventReceiverStatus.CancelNoError;

        }
        catch (Exception ex)
        {
            properties.ErrorMessage = ex.Message;
            properties.Status = SPEventReceiverStatus.CancelWithError;
        }
        base.ItemAdding(properties);
    }

Still it fires the default behavior of ItemAdding. Is there anyway to to stop the default behaviour of code and to execute only my code. I just want my reply object to be created and the default object should not be added to list

2

You also need to set

properties.Cancel = true;

Your final code will look like

public override void ItemAdding(SPItemEventProperties properties)
{
    try
    {
        //my code
        SPListItem parentItem = oList.GetItemById(discussionId);
        SPListItem reply =SPUtility.CreateNewDiscussionReply(parentItem);
        reply["Body"] = properties.AfterProperties["Body"];
        reply.Update();

        properties.Status = SPEventReceiverStatus.CancelNoError;
        properties.Cancel = true;
    }
    catch (Exception ex)
    {
        properties.ErrorMessage = ex.Message;
        properties.Status = SPEventReceiverStatus.CancelWithError;
    }
}
2
  • The tooltip says its obsolete use Status instead May 11 '15 at 13:00
  • Ok its because you are in SP2013. Try updated code. May 11 '15 at 13:03
1

After removing this line and its worked for me:

base.ItemAdding(properties);
1
  • I came across the same problem. This answer actually is correct. It lacks a bit of explanation, but the downvote isn't justified. To extend the answer: base.ItemAdding(properties) resets the properties.Status to Continue. So either put the base call at top of the custom code, or remove the line to solve the issue.
    – Steve B
    Nov 21 '19 at 14:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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