How to get the current content item in NotificationProvider class?

Topics: Core, Customizing Orchard, Writing modules
Dec 26, 2014 at 5:52 AM
Edited Dec 26, 2014 at 12:42 PM
I have a class which implements INotificationProvider interface. Is there anyway of finding the content item which is currently being accessed, in the GetNotifications() method?

For example, I am accessing a content item of content type Page, and I wan't to show some notification to the user for this content item based on some condition. How do I get this Content Item.

I have tried accessing the content item in class that implements IShapTableProvider and it was easy. I couldn't figure out how to access it in the class that implements INotificationProvider.

Dec 26, 2014 at 5:42 PM
There are various ways you could achieve this. In your case, probably the easiest way is to implement a ContentHandler class and its BuildDisplay method, which receives a context argument, providing access to the content item being rendered. Depending on your requirements, you may want to check for the display type the content is being rendered, since multiple content items might be rendered on the screen (e.g. a list of comments together with the blog post).

What you could then try is have your ContentHandler class implement INotificationProvider, and store the content item in a private field, and access it from your INotificationProvider.GetNotifications method, from which you then access the private field pointing to the content item.
This obviously only works if "BuildDisplay" is invoked before "GetNotifications". If that's not the case, then there is another way to find out the current content item by checking the currently requested route and see if it maps to a content item (and if it does, use the route values to load the content item). I wrote a post about this here: I essentially implemented a service that you can inject anywhere to get the current content item.