Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show alert message when event fires

I have the following requirement. I have stored a list of dictionary items. I want Dictionary Key to be unique. I have written the following code like whenever any new item is saving with existing key name, it should throw an alert like "Item already exist".

What I am doing is like comparing the Key value with existing dictionary keys while saving the item. I have written the code in the ItemSaving event.

 public class IsItemExist
    {
        Sitecore.Data.Database master = Sitecore.Configuration.Factory.GetDatabase("master");

        public void OnItemSaving(object sender, EventArgs args)
        {

            Item dbItem = master.GetItem("/sitecore/content/Administration/Development Settings/Lookups");
            Item selectedItem = Event.ExtractParameter(args, 0) as Item;
            foreach (Item item in dbItem.Axes.GetDescendants())
            {
                if (item.Template.Name.Contains("entry"))
                {
                    if (item.Fields["Key"].Value == selectedItem.Fields["Key"].Value)
                    {
                        Sitecore.Context.ClientPage.ClientResponse.Alert("Item is already exist");
                    }
                }
            }

        }


    }

web.config entry

<event name="item:saving">
<handler type="CustomEvent.IsItemExist, CustomEvent" method="OnItemSaving"/>
</event>

It's showing alert message and i am able to save the item. 1.I don't want to save the Item with duplicate value again. 2.I am getting 2 alert messges when click on the save button.why?

any help will be appreciated.Thanks Guys..

like image 506
user3308656 Avatar asked Feb 14 '14 03:02

user3308656


People also ask

How do you alert a function in JavaScript?

The alert() method in JavaScript is used to display a virtual alert box. It is mostly used to give a warning message to the users. It displays an alert dialog box that consists of some specified message (which is optional) and an OK button. When the dialog box pops up, we have to click "OK" to proceed.


1 Answers

You should add your handler to item:adding event and set the Cancel property of the event result to true:

  <event name="item:adding">
    <handler type="CustomEvent.DoesItemExist, CustomEvent" method="OnItemAdding" />
  </event>
public class DoesItemExist
{
    Sitecore.Data.Database master = Sitecore.Configuration.Factory.GetDatabase("master");

    public void OnItemAdding(object sender, EventArgs args)
    {
        Item dbItem = master.GetItem("/sitecore/content/Administration/Development Settings/Lookups");
        Item selectedItem = Event.ExtractParameter(args, 0) as Item;
        foreach (Item item in dbItem.Axes.GetDescendants())
        {
            if (item.Template.Name.Contains("entry"))
            {
                if (item.Fields["Key"].Value == selectedItem.Fields["Key"].Value)
                {
                    SitecoreEventArgs evt = args as SitecoreEventArgs;
                    evt.Result.Cancel = true;
                    Sitecore.Context.ClientPage.ClientResponse.Alert("Item already exists");
                }
            }
        }
    }
}
like image 133
Marek Musielak Avatar answered Oct 22 '22 21:10

Marek Musielak