Retrieving content items from a list

Topics: Customizing Orchard, General, Writing modules, Writing themes
Nov 24, 2011 at 11:01 AM

I am trying to access the items of a list but I've been unsuccessful. I find it odd that the items in the list with containeable part are able to know who is the container but I can't seem to find these items in the list itself.

 

Any thoughts on this?

 

Thanks in advance,

 

João Reis

Nov 24, 2011 at 12:55 PM

How are you trying to access them, can you show us the code that isn't working?

Nov 24, 2011 at 1:03 PM
Edited Nov 24, 2011 at 1:09 PM

I got it to work now by using the following:

 

var List = _contentManager.Query("CTType").List();
           
            var parentId = part.ContentItem.Id;
            var Highlight = (from News in List
                           where (from parts in News.Parts where parts.As<CommonPart>().Container.ContentItem.Id == parentId
                                   select parts.Id).Count() > 0
                                   && News.IsPublished()
                           orderby News.As<CommonPart>().PublishedUtc descending                          
                           select News).First();

 

But I feel like there has to be some other way to get it

Nov 24, 2011 at 1:12 PM

The problem there is that you start by calling List() which enumerates the entire query in memory before you've even filtered. The News.IsPublished() check is also redundant because ContentManager by default queries for published content unless you specify VersionOptions to say otherwise.

You can do a join query with ContentManager like this:

var query = _contentManager.Query("CTType").Where<CommonPartRecord>(c=>...)

Nov 24, 2011 at 1:22 PM

That does make sense and confirms my first impression, thank you very much.

Jan 4, 2012 at 5:19 PM

How can i use this query for termcontentitem view to sort then by created date?

I am trying

var list1 = contentManager.Query("list").OrcderBy(list => list.As<CommonPart>().CreatedUtc);

but it's not working.

Jan 4, 2012 at 6:05 PM

Don't use As<> within the OrderBy, that'll create performance problems and probably errors. Instead use OrderBy<CommonPartRecord>(c=>c.CreatedUtc)

Jan 4, 2012 at 6:24 PM

Thanks for reply but It's throwing error CommonPartRecord could not be found.missing assembly reference

Jan 4, 2012 at 6:40 PM

add the reference in project to Orchard.Core  if I am not mistaken, it's the dependency that is missing

Jan 4, 2012 at 7:22 PM

Do I have to add it in web.config in core?

what should I write there?

Jan 4, 2012 at 8:37 PM

Not write. In your module add a reference to Orchard.Core.

Read this:

http://msdn.microsoft.com/en-us/library/f3st0d45%28v=vs.80%29.aspx