How to access a content item

Topics: Customizing Orchard
Sep 9, 2014 at 9:05 PM
How do you access a content item in orchard? It appears you can use the IContentManager interface to access a content item by id but how on earth do you figure out what the content item's id is? I enabled the id part on the content item I'm trying to access but it auto generates the id so I have no idea what it is. Any ideas?
Sep 10, 2014 at 2:28 AM
Edited Sep 10, 2014 at 2:29 AM

I think we need more context.

What do you want to accomplish?
Where do you call the IContentManager? (Driver, Controller, View??) - As far as I know it should be the first or second choice. Using the ContentManager in the view is not a good practice.

If you specify your ContentItem search you can use the ".Query<TPart>" Method.

For example like this:
I want all the ContentItems which have a CommonPart attachted and where the Container.Id from this CommonPart is equal to my linkGroup.Id and order them by the Position Field from the ContainablePart.
parentContentItem.ChildItems = _contentManager.Query<CommonPart, CommonPartRecord>()
  .Where(c => c.Container.Id == linkGroup.Id)
  .Select(item => item.ContentItem)
  .OrderByDescending(c => c.As<ContainablePart>().Position);
Sep 10, 2014 at 2:11 PM
Ultimately I would like to be able to have my controller's view render or display a specific content item. So if I have a page called "My Page" I would like to show its contents as part of my view. Is this possible?
Sep 10, 2014 at 3:22 PM
Ok! You want get the content item in your controller. What do you know about that content item that you need in your controller?

If its the Title you can get it this way:
var myPage =
    _contentManager.Query(VersionOptions.Published, "Page")
        .Where(t => t.Title == "My Page")
This will give you the ContentItem if its exists.

If you want to display the Shape of this content item in your view, I am not sure if and how this works, but I keep digging :).
Sep 10, 2014 at 4:21 PM
Awesome, thank you for your help so far. I need to grab the entire content item if possible if not then I just need the body part.
Sep 10, 2014 at 4:41 PM
Is it possible to grab the text within the body part of the content item based on its title part's value? So if I have a page with the title My Page could I then grab its body part's text?
Sep 10, 2014 at 4:52 PM
Edited Sep 10, 2014 at 4:54 PM
In myPage there should be the whole contentItem. If you want the BodyPart of this contentitem use ".As<TPart>()".
 var myPage =
    _contentManager.Query(VersionOptions.Published, "Page")
        .Where(t => t.Title == "My Page")

 var text = myPage.As<BodyPart>().Text;
If you only want the Text from this Bodypart you can use this Query:
var bodyText =
    _contentManager.Query<BodyPart, BodyPartRecord>()
        .Where(t => t.Title == "My Page")
        .Select(b => b.Text)
But keep in mind, this text is HTML per default. Otherwise u need to change the Flavor of the BodyPart.
Sep 10, 2014 at 5:26 PM
Thank you so much this worked perfectly. I just added the bodyText var to the ViewBag and was able to access the body text in the content item and now the view can be somewhat altered in that the message being displayed is alterable.
Sep 10, 2014 at 6:08 PM
Thats great to hear!

I am not sure what you exactly wanted to do, but in my opinion you should use and understand shapes in Orchard.
Sep 10, 2014 at 6:43 PM
Okay, thanks :)