I'm facing a problem in my Sitecore project. As we are working in a team, we can't track all the item who has created and what name they have given. The problem is, people are creating the item with same name. This is causing some serious problem while moving the items to different environments.
What I want is, while creating the Sitecore item, pipeline method should execute and validate whether its immediate parent already has the same item name.
For example : Parent A
has 3 subitems called Child1, Child2, Child3
, when developer tried to create a item with name Child2
the popup/alert should display and not to allow him to create the item.
Please help me with this.
You can add your own handler to item:creating
event and check if the parent already contains a child with proposed name.
Here is nice post describing how to prevent duplicates items in Sitecore. I've copied the following code from there:
<event name="item:creating">
<handler type="YourNameSpace.PreventDuplicates, YourAssembly" method="OnItemCreating" />
</event>
namespace YourNamespace
{
public class PreventDuplicates
{
public void OnItemCreating(object sender, EventArgs args)
{
using (new SecurityDisabler())
{
ItemCreatingEventArgs arg = Event.ExtractParameter(args, 0) as ItemCreatingEventArgs;
if ((arg != null) && (Sitecore.Context.Site.Name == "shell"))
{
foreach (Item currentItem in arg.Parent.GetChildren())
{
if ((arg.ItemName.Replace(' ', '-').ToLower() == currentItem.Name.ToLower())
&& (arg.ItemId != currentItem.ID))
{
((SitecoreEventArgs)args).Result.Cancel = true;
Sitecore.Context.ClientPage.ClientResponse.Alert
("Name " + currentItem.Name + " is already in use.Please use another name for the page.");
return;
}
}
}
}
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With