hi, i've been searching and searching, someone please point me in the right direction...
in my razor template, i need to be able to get a list of pages for a specific document type and to be able to loop through them and access their properties. Please give me an example of how to do this with version 7.2.1.
I was able to get a list using this:
var articles = Model.Content.Parent.Children.Where(p => p.ContentType.Alias == "ProjectArticle").OrderByDescending(p => p.CreateDate).Take(8);
but i have no clue how to access the properties for each item, nor do i have any idea if this is the current/proper way to get a collection of pages in the view.
please point me to an example of how to do this with the current version of umbraco
the collection does in fact have items in it, but when i try to do what you mentioned i get this error:
@foreach (var article in articles) { @article.productLine
}
'Umbraco.Core.Models.IPublishedContent' does not contain a definition for 'productLine' and no extension method 'productLine' accepting a first argument of type 'Umbraco.Core.Models.IPublishedContent' could be found (are you missing a using directive or an assembly reference?)
Did you just get the same error if you try to use the item.Name, what I am trying to find out, is why you are getting this. Is it because that you access a wrong propertyAlias perhaps,
What if you do this, then you should get the name of the item.
thanks for your help, but my object doesn't even have access to the .GetPropertyValue method. when i try to use it i get the following error:
'Umbraco.Core.Models.IPublishedContent' does not contain a definition for 'GetPropertyValue' and no extension method 'GetPropertyValue' accepting a first argument of type 'Umbraco.Core.Models.IPublishedContent' could be found
am i missing something else? is there a "using" statement or something else i'm missing? it really shouldn't be this hard for me to just get access to a collection of pages and their properties
Is it possible for you to share the whole file of code that you have. Then I think it eaiser for people to see what is going on, and come up with some ideas ofwhat canbewrong since you can access custom properties.
var articles = Model.Content.Parent.Children.Where(p => p.ContentType.Alias=="ProjectArticle").OrderByDescending(p => p.CreateDate).Take(8);
@foreach(var item in articles.Where(x => x.IsVisible())){
@item.GetPropertyValue("ProductLine")
}
}
but even this code gives me an error:
'Umbraco.Core.Models.IPublishedContent' does not contain a definition for 'IsVisible' and no extension method 'IsVisible' accepting a first argument of type 'Umbraco.Core.Models.IPublishedContent' could be found (are you missing a using directive or an assembly reference?)
is this supposed to be a collection of IPublishedContent? Because it looks like that object does NOT have the IsVisible or GetPropertyValue methods/properties.
collection of pages by document type
hi, i've been searching and searching, someone please point me in the right direction...
in my razor template, i need to be able to get a list of pages for a specific document type and to be able to loop through them and access their properties. Please give me an example of how to do this with version 7.2.1.
I was able to get a list using this:
var articles = Model.Content.Parent.Children.Where(p => p.ContentType.Alias == "ProjectArticle").OrderByDescending(p => p.CreateDate).Take(8);
thanks,
Hi Matt
You should be able to loop over the articles like this, if the above selection returns anything of course.
Does that work?
/Jan
Hi Jan,
the collection does in fact have items in it, but when i try to do what you mentioned i get this error:
@foreach (var article in articles)
{
@article.productLine
}
'Umbraco.Core.Models.IPublishedContent' does not contain a definition for 'productLine' and no extension method 'productLine' accepting a first argument of type 'Umbraco.Core.Models.IPublishedContent' could be found (are you missing a using directive or an assembly reference?)
Hi Matt,
I think that you should be able to do something like this.
And if you have a custom property you can access it by article.PropertyAlias. Try to see this documentation too for an for each loop. http://our.umbraco.org/documentation/Reference/Querying/UmbracoHelper/#TypedContent%28intid%29
Hope this helps,
/Dennis
Hi Dennis,
See my previous reply, when i try to access item.propertyAlias I get an error.
Hi Matt,
Did you just get the same error if you try to use the item.Name, what I am trying to find out, is why you are getting this. Is it because that you access a wrong propertyAlias perhaps,
What if you do this, then you should get the name of the item.
Looking forward to hear from you.
/Dennis
@item.Name works, but none of the property aliases work, they all give the above error. this is what's available to me at runtime:
Hi Matt,
A light just went straight to me. You are using strongly typed Razor so what if you do something like this.
And you can add the return value. it could be a string like this:
Hope this helps,
/Dennis
thanks for your help, but my object doesn't even have access to the .GetPropertyValue method. when i try to use it i get the following error:
'Umbraco.Core.Models.IPublishedContent' does not contain a definition for 'GetPropertyValue' and no extension method 'GetPropertyValue' accepting a first argument of type 'Umbraco.Core.Models.IPublishedContent' could be found
am i missing something else? is there a "using" statement or something else i'm missing? it really shouldn't be this hard for me to just get access to a collection of pages and their properties
Hi Matt,
Is it possible for you to share the whole file of code that you have. Then I think it eaiser for people to see what is going on, and come up with some ideas of what can be wrong since you can access custom properties.
/Dennis
i've tried with a template as simple as this:
@inherits Umbraco.Web.Mvc.UmbracoTemplatePage
@{
var articles = Model.Content.Parent.Children.Where(p => p.ContentType.Alias=="ProjectArticle").OrderByDescending(p => p.CreateDate).Take(8);
@foreach(var item in articles.Where(x => x.IsVisible())){
@item.GetPropertyValue("ProductLine")
}
}
is this supposed to be a collection of IPublishedContent? Because it looks like that object does NOT have the IsVisible or GetPropertyValue methods/properties.
figured it out... was missing a "using" statement as i had thought might be the issue.
if i add:
@using Umbraco.Web;
then GetPropertyValue("productLine") is available to me.
I really wish there was clearer documentation for how to do this. I saw nothing that tells me to include that using statement. oh well, moving on! :)
thanks for your help.
Hi Matt,
Good to hear that you found a solution, and can move on with your Umbraco project.
/Dennis
is working on a reply...