Prevent a Sitecore item from moving to another folder?

Suppose you have two folders under the Home node i.e. Folder A and Folder B. You want to prevent these items from being moved to another folder accidentally or deliberately. You just want to edit or copy these items. There is nothing like that out of the box in Sitecore. But you can easily create such a functionality on your own:

  1. Create a new template with one field only – checkbox “Cannot be moved“.
  2. Add your handler to <event name="item:moving"/>
  3. In the handler code cancel the event if the checkbox is checked.
protected void OnItemMoving(object obj, EventArgs args)
  {
      if (JobContext.IsJob && JobContext.Job.Category == "publish")
          return;

      SitecoreEventArgs evt = args as SitecoreEventArgs;
      Item item = Event.ExtractParameter(args, 0) as Item;

      if (MainUtil.GetBool(item["Cannot be moved"], false))
        this.CancelEvent(evt);
  }

Also, you have to apply field-level security to that Cannot be moved field.

If you have any thoughts on this, please drop a comment.

Leave a Reply

Your email address will not be published. Required fields are marked *