This project is read-only.

How to figure out which RoutePartRecord entry is published?

Topics: Writing modules
Feb 17, 2012 at 4:22 PM


I am currently building a module for Orchard, and the problem that I came into is that I'm not sure how to find out which RoutePartRecord entry is actually valid (published). Because of the versioning, I have multiple entries in RoutePartRecord table for the same instance of a content type that contains Routable part. For example, if I wanted to find which Title or Body part is currently pulished I would look into ContentItemVersionRecord and see which one has Published set to true, but I can't find similar table for RoutePartRecord. The thing is that Orchard itself manages to find this information, so I know it's possible to do that, but I couldn't find exactly how, so if somebody knows, I would be very grateful.

Thanks in advance,

Feb 17, 2012 at 5:37 PM

If you want to get the url for an item (which I guess is your underlying need) use the following:


Feb 17, 2012 at 6:16 PM

Yes, but I also need the Title from RoutePartRecord table. The thing is that I don't have an item whose URL I need to find. What I want to do is to, let's say for example, display a list of pairs Title/Path from RoutePartRecord table for each content that contains Route part, but for some contents I have multiple entries in this tables, because I updated them and saved them several times (Orchard saves all versions in db), and I can't find out which entry is published, and which ones are old versions.

Feb 17, 2012 at 7:26 PM

Firstly - why do you need to display the title/path pairs, and are you aware this is all completely changing in Orchard 1.4?

Secondly - you can get the title for an item with @Html.ItemDisplayText(content). So actually you don't need any reliance on RoutePart, or the new AutoroutePart and TitlePart, to get Urls and titles for items. And it's best to avoid tying your code to any specific content part if at all possible, and this will mean your code is compatible with both 1.3 and 1.4.

Feb 17, 2012 at 8:34 PM

I made a custom content type Event, and it contains Route, Body and DataTime(this is also a custom part) parts. Now, I needed to somehow display first 3 events to come, but I can't do sorting by custom parts, only by those predefined. I then wrote a module that works directly with Orchard's database - I fetch all records that are related to Event, from RoutePartRecords, but I get multiple entries for some event's, as I described before. I'm doing everything in my controller, so I can't use something like @Url.ItemDisplayUrl(content) or @Html.ItemDisplayText(content).

Feb 17, 2012 at 8:41 PM

To query you can use content manager instead -


And also it is possible to use the Url/Html helpers in a controller. You just need to import a HttpContextBase dependency and do:

var url = new UrlHelper(_httpContext);



Feb 18, 2012 at 7:14 AM

Duplicate records probably come from your not restricting your query by version. Content manager queries make that easy. To get the URL of an item, you can also go through ContentManager.GetItemMetadata.

Feb 18, 2012 at 7:57 AM
Edited Feb 18, 2012 at 7:58 AM

Going thru GetItemMetadata will just give you the routevalues; you still need the UrlHelper to generate a url...

Edit: But, you can get the title (DisplayText) straight from GetItemMetadata.

Feb 18, 2012 at 8:14 AM

Absolutely, that's the way to go, but ItemMetadata has its own usefulness, it's good to know.

Feb 18, 2012 at 8:31 AM

Oh definitely, well it's what the Url/Html helper extensions are doing behind the scenes in any case :)

Feb 18, 2012 at 2:37 PM

Thank you very much for helping! I solved it in another way, but I will definitely try to do it the way you told me because it seems that it's smoother and faster :)

Btw, this is what I was doing:

var query3 = _rpr.Fetch(c => set.Contains(c.ContentItemRecord.Id) && c.ContentItemVersionRecord.Published == true).ToList();

where _rpr is a RoutePartRecord and in "set" I have all Id's that i want to take into consideration (went trough the ContentItemRecord to see which items belong to a specific type, in my case it is Event), but I'm not sure if this is the right way to do it, because I have 3 or 4 queries like this in my controller, and I wonder if it would be more efficient to do it with ContentManager.

Once again, thanks for your assistance!

Feb 18, 2012 at 3:29 PM

With IContentManager you can just use .ForVersion(VersionOptions.Published).

Also be aware that your current code simply won't work in Orchard 1.4 - there is no more RoutePartRecord, so your module will fail compilation and not even run.