Developer forum

Forum » Development » [Subscribe(Dynamicweb.Notifications.Module.Added)]

[Subscribe(Dynamicweb.Notifications.Module.Added)]

Jon Lennryd
Reply
Hello, does anyone know how to implement and use a class which subscribes to the Module.Added event?

I have created a class as below, but don't know if or where I should instantiate a class object of this class.. My guess is I must create an object from this class, but where should I create this object?
Certainly, my goal is to get notified about when my module is installed, so I can install additional tables. If there is any other way to acheive this, I am just as happy!
[Subscribe(Dynamicweb.Notifications.Module.Added)]
public class FancyInstaller : Dynamicweb.Extensibility.NotificationSubscriber
{
     public override void OnNotify(string notification, object[] args)
    {
        // Never called.
    }
}
Thanks for any help!

Replies

 
Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply
Hi there,

You can create a new C# Class Library project in Visual Studio, add references to the Dynamicweb assemblies and add the above code. When you compile the project you end up with a DLL which you should put in the application's Bin folder.

If your code is still not executed, check and see if the other overload of OnNotify is called.

For some more background, take a look here: http://devierkoeden.com/articles/notification-subscribers.aspx

There's no need to instantiate your class. Dynamicweb will do that for you.

Cheers,

Imar
 
Jon Lennryd
Reply
Hi Imar, thank's for your reply.

I tried to setup the class with the Page.Loaded event, exactly as above, and it works without a problem; The OnNotify() function is called properly upon each page load.

Can it be something with the way I install my module? I perform these steps:
  1. I upload the new dll to the bin folder.
  2. I reload the admin page, to wait for the system to restart.
  3. I install my module via the Management Center->Developer->Modules, by clicking the New module button.
  4. I give it Name and System name as supposed.
Is it possible that I misunderstand when the Module.Added event is fired? As it is now, it never fires, assuming my module is correctly written.

Thanks,
Jon
 
Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply
This post has been marked as an answer
Ah, maybe that's it. ModuleAdded fires when a module is added to a paragraph, not when it's registered in the backend.

You could create the tables the first time the module is added to a paragraph (and keep track of the fact you already did it), or you could provide a setup page in the admin area of your site in the Modules list.

Alternatively, you can auto register your module as explained here: http://devierkoeden.com/articles/custom-modules-part-9-auto-registering-modules.aspx You can then execute your own code along with the module registration code.

Hope this helps,

Imar
Votes for this answer: 0
 
Jon Lennryd
Reply
Yep, in the end I created an admin interface with a simple 'Install tables' button. It's the cleanest way apart from having a module-install hook. The documentation at (http://developer.dynamicweb-cms.com/api/extensibility/) is a bit vague about what the 'Added' notification actually does.

I did read the article you mention, but felt it is unnecessary overhead to call it on every page load.


Thank's for all advice Imar!

/Jon
 
Nuno Aguiar
Reply

 Hi Imar,

 

The auto-register code fails in DW8. I tried looking for a way to do this with no luck. I get

Could not load type 'Dynamicweb.RegisterModules.Module' from assembly 'Dynamicweb, Version=8.0.0.0, Culture=neutral, PublicKeyToken=null

 

Do you have a different solution for Dynamicweb 8?

 

Nuno

 
Morten Bengtson
Reply

Hi Nuno,

 

You can use Dynamicweb.Modules.Module in DW8

 

/Morten

 
Nuno Aguiar
Reply

Hi Morten,

 

Thanks for the quick feedback.

 

Nuno

 

You must be logged in to post in the forum