Getting field level values in blog post

Topics: Customizing Orchard
Apr 18, 2012 at 11:55 PM

I have a taxonomy Alerts, that has some terms like Security Alerts, Outage Alert etc. To this Alerts Type term Content Type, I added a datetime field.

Then I have a Blog that shows a feed for all such alerts. For this, I added a Taxonomy Field to the 'Blog Post' Content Type.

So, the hierarchy is like this BlogPost->Taxonomy->Terms->DatetimeFields

Now, I have to access the datetime field value related to all the terms from the code. I am into Orchard.Blogs module and trying to do this but no success so far.

I drilled down into blogPost Part but cannot figure out, how to get values for datetime fields.

 

Thanks

Developer
Apr 19, 2012 at 8:19 AM

If I understood correctly, you want to know how to access the selected verb for the DatetimeFields term of a BlogPost? In that case, I think you will find your answer on this thread: http://orchardtaxonomies.codeplex.com/discussions/263916

Apr 19, 2012 at 3:50 PM

Thanks for the link,, but that is not something I am looking for.

In my case, Datetime field is not a term. It is a field that I attached to a term. So, when from the UI, an admin creates a new term, he sees this field and can put in some value. For, e.g when you create a new term, you must provide a title. Similarly, Datetime field will appear and you provide some value there.

So, now my issue is to access this value in a blog post.

I hope the scenario is clear now. Please help !

Coordinator
Apr 19, 2012 at 6:08 PM

If you have your blog post content item in a dynamic variable ci, it should be something along the lines of ci.BlogPost.YourTaxonomyFieldContentTypeName.NameOfTheField.Terms[i].YourTermContentTypeName.NameOfDateField

Apr 19, 2012 at 8:25 PM

I tried but, ci.BlogPost.YourTaxonomyFieldContentTypeName.NameOfTheField.Terms comes as null.

Coordinator
Apr 19, 2012 at 8:51 PM

You did replace YourTaxonomyFieldContentTypeName and NameOfTheField with the relevant names, right? Explore each step with a debugger to see where I screwed up.

Apr 19, 2012 at 10:03 PM

For this portion,ci.BlogPost.YourTaxonomyFieldContentTypeName , when I look in the debugger, I don't see any property for NameOfTheField. I only see Terms there as null.

What could be wrong?

Thanks!

Coordinator
Apr 19, 2012 at 10:11 PM

You did replace YourTaxonomyFieldContentTypeName and NameOfTheField with the relevant names, right? What I'm suggesting is that you test each part one after the other and check where it begins to be null.

Apr 19, 2012 at 10:48 PM

You did not get my point.

When I look through my debugger, upto this point ci.BlogPost.YourTaxonomyFieldContentTypeName, I can see values.

Then when I try ci.BlogPost.YourTaxonomyFieldContentTypeName.NameOfTheField, it throws an exception  - 'Contrib.Taxonomies.Fields.TaxonomyField' does not contain a definition for 'AlertType'.

2 issues here -

1. The exception gets thrown for 'NameOfTheField' property. I don't see any value related to this in debugger. What is this exactly? What value should it have?

2. On the other hand, if I follow debugger and do ci.BlogPost.YourTaxonomyFieldContentTypeName.Terms, they come up as null.

I am lost here. Please advice!

Coordinator
Apr 19, 2012 at 11:20 PM

1. It should be the name of your taxonomy field.

2. That's because empty dynamic properties can evaluate as null.

Apr 19, 2012 at 11:22 PM

Try this:

Get your blog part to display on the front end somewhere, with the taxonomy terms. Enable shape tracing, and then look in the shape tracing panel. Find your BlogPost item within its containing Zone. Under the "Model" tab, look at the properties to see what the object graph looks like to get to your terms and then the fields on those terms. 

If I remember correctly I had trouble finding properties on dynamic obj's through the VS debugger (I could have been doing something wrong), but they always displayed in the shape tracing panel. And it's easier this way too.  

Apr 19, 2012 at 11:36 PM

I have already tried Shape Tracer and get the similar results. There is nothing as ci.BlogPost.YourTaxonomyFieldContentType.NameOfTheField.

I only see ci.BlogPost.YourTaxonomyFieldContentType.Terms whose value is null. I wish I could add a screenshot !

Apr 19, 2012 at 11:53 PM

Are you looking at the model, or the contentItem? Try looking at both.  If you haven't already, try taking a step back, and stop looking for "ci.BlogPost.YourTaxonomyFieldContentType.NameOfTheField", and instead just explore the whole tree of objects under the Shape Tracer "Model" tab. I think Bertrand was giving you guidance on that off the top of his head, which is why he said to debug to figure out where the chain of properties starts being wrong.

The taxonomy terms are displaying on the web page when you are looking through Shape Tracer, correct? 

Apr 20, 2012 at 12:11 AM

Yes, the terms are getting rendered on the web page. But, I am unable to drill down to the terms values from anywhere in the Model. I followed a top down approach from the Model. And, the closest I could get to terms is via the Model.ContentItem.BlogPost.MyTaxonomyFieldContentTypeName.Terms which is null.

Really confusing, how Orchard is able to render the terms but I don;t see them in Shape Tracer ?

Any other suggestion?

Thanks!

Apr 20, 2012 at 3:47 PM

Does anyone has any idea? Please help!

Coordinator
Apr 20, 2012 at 6:53 PM

One idea would be to look at the driver for that field.