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"
.
Sitecore.Context.Job.Name = "Install"
uiAddFromTemplate
processor: community.sitecore.net/technical_blogs/b/sitecorejohn_blog/…