This project is read-only.

Adding Custom Settings - Displaying in Views

Topics: Customizing Orchard, Writing modules
Jun 9, 2011 at 9:15 AM


I'm pretty new at Orchard. I just got started digging into code yesterday after reading and researching for about a week. 

I wrote my own module, but I'm having trouble using it. 

Basically I wanted to add some additional settings which I got working (I followed along with the Vandelay.Favicon project) but I'm having trouble accessing these settings from views. 

How would I go about accessing a custom setting that I created? 

To be honest, it seems really complicated to do what I'm trying to do. (PartDriver, PartHandler, Part, PartRecord, ViewModel, EditorTemplate, etc) maybe there's an easier way? (Obviously I know how to hack it up to make it work, but I'm trying to do it the right way for re-usability)

Jun 9, 2011 at 10:12 AM

You haven't said what you're trying to do, so I can't tell you if there's an easier way :)

But yes, building a Content Part does involve a lot of pieces. Piotr Szmyd wrote a nice codegen that will create all those pieces for you:

On the other hand, depending on what you're trying to do, there might be existing modules that could help.

To access settings, it depends how you defined them - for instance are they global settings, or on the part definition itself? It might help if you show your code.

Jun 9, 2011 at 4:55 PM

Thanks for the response.

What I'm looking to do is add some site wide variables that I feel should be defined in the admin area - Footer Text, Site Tagline, Logo Image, etc.

I want to use the same theme for multiple sites, so editing branding.cshtml or the footer with specific site settings doesn't appeal to me.

As far as showing my code, what's the easiest way to show you my project? It looks just like Vandelay.Favicon minus the FaviconService.cs and FaviconShapes.cs files. 



Jun 9, 2011 at 5:00 PM

To access site settings you go through WorkContext.CurrentSite. You have to implement your settings as a Content Part which gets attached to the Site object.

Piotr also has a nice article on that:

How you access WorkContext depends where you're doing it from - in a view, you can just do @WorkContext.CurrentSite, or from other places you can use dependency injection to get a hold of IWorkContextAccessor or IOrchardServices. Piotr's article shows you that also.

Jun 9, 2011 at 7:18 PM

Thanks. I was trying to follow along with that tutorial as well... The actual module for that one is much more complex. 

How do I inject and then access my own variables into the WorkContext.CurrentSite? 

Would I need a separate part driver to display one of the variables on a view? 

Could I e-mail you my source code?

Jun 9, 2011 at 8:35 PM

I was able to access the extra settings (in Branding.cshtml) like this:

dynamic extraSettings = WorkContext.CurrentSite.ContentItem.Parts.First(p => p.PartDefinition.Name == "ExtraSettingsPart");

And then to display it (null check is if the module is diabled it doesn't blow up)


@{ if (extraSettings != null) { <text>@extraSettings.ExtraSetting</text>}}


My intention is to make a few of these extra fields that can be sprinkled wherever people need them in their templates instead of them hardcoding the extra values in the template. Is this the right way to do something like this?



Jun 9, 2011 at 9:17 PM

That's pretty much it. If you include @using Orchard.ContentManagement at the top of your view then you can do this which is a bit simpler:

var extraSettings = WorkContext.CurrentSite.As<ExtraSettingsPart>();

if (extraSettings!=null) { ... }

There's no need to use dynamic in either case.