4

I am implementing a custom processor for item:created and I'm wondering if there is any easy way to determine if the item being created is being processed through the context of a user using Sitecore itself(say someone using the browser to create an item) or if it is being created when the item is being installed through a sitecore package or update installation wizard package?

Anyone know if how to differentiate that?

3

Depending on the context of what you are trying to detect, there are a couple of options.

Based on user interaction

If you only care able the events that are fired when a user interacts with the UI, then there are a number of <ui*> pipelines that you can add a processor to, and they only fire when a user interacts with the UI (i.e. a manual action). The equivalent of the item:created event is the <uiAddFromTemplate> pipeline, which will be called when the users add an item based on a data template, branch template or command template.

You can create a processor using this structure:

public class AddFromTemplate
{
    public void Execute(ClientPipelineArgs args)
    {
        // Add your custom logic

        Assert.ArgumentNotNull((object) args, nameof (args));
        if (!args.HasResult)
            return;
        Item item = (Context.ContentDatabase ?? Context.Database).GetItem(args.Result);

        if (item.FullPaths.Path == "xyz")
        {
            // ...
        }
    }
}

And then patch this into the config:

<processors>
    <uiAddFromTemplate>
        <processor type="MyProject.Custom.Pipelines.UI.AddFromTemplate, MyProject.Custom" method="Execute" mode="on" />
    </uiAddFromTemplate>
</processors>

Package Installation

If you need to check whether an handler has been called as a result of an event being fired, then you can take advantage of the fact the package installation is run as a Job, so you can check the the Sitecore.Context.Job.Name property:

if (Sitecore.Context.Job?.Name == "Install")
{
    // event fired as a result of package installation
}

If the item has been saved by the user via the UI or from other code programatically then the Sitecore.Context.Job property will be null.

Events raised during publishing

Certain events are also raised during publishing, such as item:created and item:saved, since the item will technically be created/saved on the target database during publish.

Be sure to cater for these events, if that is something that your code needs to avoid being run during publishing.

The usual methods for this is are to check if the item database is master or use the PublishingHelper to check if the operation is called in the course of a publishing operation.

public void OnItemCreated(object sender, EventArgs args)
{
    var createdArgs = Event.ExtractParameter(args, 0) as ItemCreatedEventArgs;
    if (createdArgs?.Item?.Database.Name != "master")
    {
        return;
    }

    // OR

    if (PublishHelper.IsPublishing())
    {
        return;
    }
}

The IsPublishing() just checks if Context.Site.Name == "publisher".

  • These events also get fired during publishing, which is important to be aware of. Probably the best way to check this, is through the Context.Site which should be "publisher" while this happens. – Mark Cassidy Aug 17 '18 at 23:34
  • 1
    @MarkCassidy You can also use PublishHelper.IsPublishing() which internally does exactly that. Code sample added. – jammykam Aug 18 '18 at 3:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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