You are on page 1of 3

4/10/2015 Thoughts 

on .NET ­ Accessing currently displayed content parts in Orchard

Thoughts on .NET

I'm Piotr Szmyd, a .NET junkie and Orchard enthusiast living in the beautiful city of
Warsaw. I also co‐founded and serve as a Chief Geek @ Proligence Ltd.

   

SPONSORED BY

RECENT BLOG POSTS

Upgrading Orchard and security exceptions


One seat on the Orchard Steering Committee ‐ vote!
Orchard Harvest 2012 conference announced!
Updating an Orchard clone from 1.5 to 1.x and later.
Wrapping up content item zones in custom markup
Wiring SignalR with Autofac
Orchard 1.4 is here!
Orchard Steering Committee elections announced

Tweets by @pszmyd

Accessing currently displayed LUT 15 2011 9:14

content parts in Orchard


Tags: orchard, module, parts, english

Sometimes you may need to acquire some knowledge about other parts displayed in the current request.
The purposes can be different, from simple logging to displaying a content part which relies on other
current content ﴾eg. you want to differentiate the display basing on other existing content﴿.

I needed it to record some information ﴾path and title﴿ about current RoutePart to create the
BreadcrumbsWidget ﴾which will arrive soon with the new release of Hierarchical Menu module﴿. Although
there are many ways to do that, this one is particularly useful when you don’t want to ﴾or cannot﴿ directly
interfere with the other Content Parts. Bertrand Le Roy ﴾as always helpful﴿ gave me an idea to do that by

http://www.szmyd.com.pl/blog/accessing­currently­displayed­content­parts­in­orchard#.VSe86_mUc3g 1/3
4/10/2015 Thoughts on .NET ­ Accessing currently displayed content parts in Orchard

pushing the information from content items instead of pulling. This can be useful in some cases, but
unfortunately I wanted something simpler, so sticked to something less intrusive – leveraging the
Content Handler’s events.

First of all, you have to create a interface of a service, which will track the parts you need and hold them
throughout the single request.

public interface IPartWatcher : IDependency {
    void Watch<T>(T part) where T: IContent;
    IEnumerable<T> Get<T>() where T: IContent;
}

and the appropriate, simple implementation

public class PartWatcher : IPartWatcher {
    readonly HashSet<IContent> _parts = new HashSet<IContent>();

    public void Watch<T>(T part) where T : IContent {
        _parts.Add(part);
    }

    public IEnumerable<T> Get<T>() where T : IContent {
        return _parts.Where(p => p.Is<T>()).Select(p => p.As<T>());
    }

Now, that we have our service that will hold the parts we want, we have to fill it with necessary data. This
can be done by a simple implementation of ContentHandler ﴾the same you write for your content parts
– this time we won’t connect it with any content part﴿.

[UsedImplicitly]
public class PartWatcherHandler : ContentHandler {
    public PartWatcherHandler(IPartWatcher service)
    {
        OnGetDisplayShape<RoutePart>((ctx, part) => service.Watch(part));
    }
}

This part of code registers a lambda expression to the handler’s event, OnGetDisplayShape, which is fired
when Orchard displays an item containing the RoutePart. This ensures that parts that will be watched by
PartWatcher are currently being displayed. Lambda is very simple and only tells the PartWatcher to
watch this particular part.

Cool! This is exactly what I needed, in a clean and simple way

You can, of course, use other events ﴾eg. OnIndexing﴿ and track different content parts, depending on what
you want to achieve.

http://www.szmyd.com.pl/blog/accessing­currently­displayed­content­parts­in­orchard#.VSe86_mUc3g 2/3
4/10/2015 Thoughts on .NET ­ Accessing currently displayed content parts in Orchard

Now, the last thing left is to inject the IPartWatcher service to your driver/handler/etc. constructor
and use it!

Cheers!

1 Comment
harmony7 said on lut 16 2011 at 12:32

This is absolutely brilliant! I will definitely check this out.

Powered by Orchard © The Bootstrap Machine 2012.

http://www.szmyd.com.pl/blog/accessing­currently­displayed­content­parts­in­orchard#.VSe86_mUc3g 3/3

You might also like