6

I am looking to run some custom logic (likely some rules) whenever an item is added from a particular branch template. How can I trigger this logic to run, in Sitecore.

1 Answer 1

5

There are actually a few options here:

  1. Command Templates
  2. Event handlers (item:added)
  3. Pipeline processors (uiAddFromTemplate, addFromTemplate)

Command Templates

This is by far the simplest solution, requiring the least implementation effort but is also the least extensible and flexible.

What you would do is create a command template item under the branch template that you would would like to run the logic from. You would then reference your code from the command template or you could add a script directly to the command template, though I advise against this. To add a little extensibility, you could make a branch template specifically for the command template and add an instance of that branch template to whichever branch template(s) you would like the logic to be run for.

Event Handler

I should start by noting that the item:added event has been deprecated (not sure which version), and unfortunately the branch template cannot be retrieved from the args of the item:created event. However, if you are using an older version of Sitecore then this is still an (albeit unrecommended) option.

What you would do is create your item:added handler and trigger your custom logic, rules, etc. from there.

Pipeline Processors

This is my recommended approach, and the most extensible, flexible and currently supported solution.

There are two pipeline processors that you could use for this: the uiAddFromTemplate pipeline and the new AddFromTemplate (item provider) pipeline.

The uiAddFromTemplate pipeline runs whenever someone right-clicks an item and adds an item via the "Add From Template" flyout context menu. There is some good documentation out there on this pipeline, between Sitecore docs and blog posts, but there is a major problem with it: the item has to be added from the UI (using the method described above), i.e. adding an item from code or any other method will not trigger the pipeline.

The other option is to use the AddFromTemplate pipline, which is one of the new item provider pipelines. The AddFromTemplate pipeline succeeds where the uiAddFromTemplate pipeline fails, in that it is run whenever an item is added, regardless of whether the ite was added from the UI or from code. Note that the pipeline will run once per transaction, i.e. if you are adding a single item it will run once, and if you are adding a branch template it will run once for the entire subtree being added. In contrast to the uiAddFromTemplate pipeline, there is pretty much no documentation and I have seen very little in the way of blog posts about the AddFromTemplate pipeline. This is likely because of how new the item provider pipelines are and is likely to change in the future.

For now, I'm just going to leave this right here:

public override void Process([NotNull] AddFromTemplateArgs args)
{
    // this is managed in configuration (runIfAborted=true would have to be set to override the value)
    if (args.Aborted)
    {
        return;
    }

    Assert.IsNotNull(args.FallbackProvider, "FallbackProvider is null");

    try
    {
        var item = args.FallbackProvider.AddFromTemplate(args.ItemName, args.TemplateId, args.Destination, args.NewId);
        if (item == null)
        {
            return;
        }

        args.ProcessorItem = args.Result = item;
    }
    catch (Exception ex)
    {
        Log.Error("PROCESSOR NAME for Pipeline AddFromTemplate failed. Removing partially created item if it exists.", ex, this);

        var item = args.Destination.Database.GetItem(args.NewId);
        item?.Delete();

        throw;
    }

    // your logic here
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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