This project is read-only.

Getting the Part value for the Site content item

Topics: General, Writing modules
Jun 26, 2013 at 7:14 AM
I'm extending the Settings -> General section of the admin page to include an Enumeration Field called Erp. To do this I'm using this code in the migration:
            ContentDefinitionManager.AlterPartDefinition("Erp", part => part
                .WithField("ErpEngine", field =>
                                        new[] {"Default", "SAP", "Dynamics AX"}))));

            ContentDefinitionManager.AlterTypeDefinition("Site", builder => builder
This displays correctly and saves correctly too.

Now I want to get that selected value from inside of a driver. I can get the Site content item with:
var site = _contentManager.Query().ForType("Site").List(); 
But debugging through this, I can't see anything that resembles my Erp Field. Where would this be?
Jun 26, 2013 at 7:19 AM
Can you see the part?
Jun 26, 2013 at 7:45 AM
Edited Jun 26, 2013 at 7:46 AM
I can see the part through the admin, yes. It appears as: "Erp Engine (Enumeration Field)".

Under "Edit Content Type - Site" I can also see it as:


ErpEngine (EnumerationField)

An option is required
Check to ensure the user is providing an option for this field.

Dynamics AX

Enter an option per line.
List mode
Dropdown list

Help text.... etc...
I can also see it at the bottom on my Settings -> General page, under "Default number of items per page"

It appears as a dropdown list, with the three options in it, and persists when I click save.
Jun 26, 2013 at 7:48 AM
I meant in the debugger.
Jun 26, 2013 at 8:02 AM
Adding a qucikwatch to the line:

var site = _contentManager.Query().ForType("Site").List();

Gives me this:

Jun 26, 2013 at 8:07 AM
What's under the one called "contentpart"? you should be able to drill to the field from that one. What happens when you enumerate the fields for that part?
Jun 27, 2013 at 12:54 AM
Edited Jun 27, 2013 at 12:56 AM
Ah neat!

I had to go through linq hell to get what I wanted, but here's the result:
            var site = _contentManager.Query().ForType("Site").List();
            var contentPart = site.FirstOrDefault().Parts.FirstOrDefault(p => p.GetType() == typeof(ContentPart));
            var enumField = (Orchard.Fields.Fields.EnumerationField)contentPart.Fields.FirstOrDefault(f => f.Name == "ErpEngine");

            if (enumField.Value == "Dynamics AX") //do something...;
Jun 27, 2013 at 1:03 AM
You should be able to do:
dynamic site =  _contentManager.Query().ForType("Site").List().First();
var enumField = site.Site.ErpEngine;
Jun 27, 2013 at 2:22 AM
I can't get that to work. In Orchard.InvokeExtensions it throws:
{"'Orchard.ContentManagement.ContentItem' does not contain a definition for 'Site'"}
Jul 1, 2013 at 1:49 AM
My mistake. Should be site.Erp.ErpEngine
Jul 1, 2013 at 1:59 AM
Ah, nice, I should have guessed that!