How to avoid unwanted code execution in Content Handler

While you’re working with Sense/Net it’s really common to write your own Content Handler to execute special actions before or after a Node gets created, modified or deleted. Sooner or later it turns out that some lines of code shouldn’t be executed when you're importing all your contents into the Repository.
In the following example I will show you a really simple and useful way to avoid the above scenario.

Let’s say that you’re overriding the OnCreating event of a Node:

protected override void OnCreating(object sender, 
    SenseNet.ContentRepository.Storage.Events.CancellableNodeEventArgs e)
{
    base.OnCreating(sender, e);
    // my funky code here
}


All you need to do is adding a new key into the App.Config of your Importer project:

<add key="SpecialWorkingMode" value="Import" />


Now you can easly decide(1) whether you’re in a special thread or running your code is safe:

protected override void OnCreating(object sender, 
    SenseNet.ContentRepository.Storage.Events.CancellableNodeEventArgs e)
{
    base.OnCreating(sender, e);            

    if (RepositoryConfiguration.SpecialWorkingMode != "Import")
    {
        // my funky code here
    }
}


Works like a charm.


Note 1: Don’t forget to add SenseNet.ContentRepository.Storage.Data namespace to your class to access RepositoryConfiguration

Pingbacks and trackbacks (1)+

Comments are closed

Welcome to the blog!

Sense/Net ECM is ever evolving. Community means the world to us so we want to keep you apprised on what’s happening on our side of the woods. Want to make us happy? Add a comment and tell us what you think!

Month List