Search code examples
sitecoresitecore7

SitecoreEventArgs Result.Cancel = true not Canceling the event


In the sitecore Content Editor I want to cancel the adding of an item.So I used the Event Handler item:addedand wrote a method to cancel the event.. But its now working, I have spending so much effort but no clue.

Here is the code:

 public void MoreThenOneAddressAllowed(object sender, EventArgs args)
 {
     var item = Event.ExtractParameter<Item>(args, 0);

     if (item.TemplateID.ToString() == Settings.GetSetting("AddressEntryTemplateID"))
     {
        if (item.Parent.Fields["More than one address allowed"] != null && item.Parent.Fields["More than one address allowed"].Value != "1" && item.Parent.Children.Count >= 1)
        {
            SitecoreEventArgs evt = args as SitecoreEventArgs;
            evt.Result.Cancel = true;
            Sitecore.Context.ClientPage.ClientResponse.Alert("More than one address not allowed under this item!!");
        }
     }   
 }

Here the entry in the configuration file:

 <event name="item:added">
        <handler type="EventHandlers.CompanyEventHandler" method="MoreThenOneAddressAllowed" />
      </event>

I can see the message in the content editor. But the item is also added, some how I want to stop the adding of item.


Solution

  • You should use the item:creating event as this will occur earlier. item:added will be too late!