Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Clickfarm 77 posts 161 karma points
    Jan 26, 2015 @ 17:16
    Clickfarm
    0

    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);

    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

    thanks,

  • Jan Skovgaard 11280 posts 23678 karma points MVP 10x admin c-trib
    Jan 26, 2015 @ 17:59
    Jan Skovgaard
    1

    Hi Matt

    You should be able to loop over the articles like this, if the above selection returns anything of course.

    @foreach (article in articles){
       <h1>article.propertyName1</h1>
       <p>article.propertyName2</p>
    }
    

    Does that work?

    /Jan

  • Clickfarm 77 posts 161 karma points
    Jan 26, 2015 @ 18:01
    Clickfarm
    0

    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?)

  • Dennis Aaen 4499 posts 18254 karma points admin hq c-trib
    Jan 26, 2015 @ 18:02
    Dennis Aaen
    0

    Hi Matt,

    I think that you should be able to do something like this.

    @{  
       
    var articles =Model.Content.Parent.Children.Where(p => p.ContentType.Alias=="ProjectArticle").OrderByDescending(p => p.CreateDate).Take(8);
       
    <ul>
            @
    foreach(var article in articles.Where(x => x.IsVisible()){
               
    <li>
                       
    @article.Name
               
    </li>
            }
        </
    ul>
    }

    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

  • Clickfarm 77 posts 161 karma points
    Jan 26, 2015 @ 18:05
    Clickfarm
    0

    Hi Dennis,

    See my previous reply, when i try to access item.propertyAlias I get an error.

  • Dennis Aaen 4499 posts 18254 karma points admin hq c-trib
    Jan 26, 2015 @ 18:44
    Dennis Aaen
    0

    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.

    @{  
       
    var articles =Model.Content.Parent.Children.Where(p => p.ContentType.Alias=="ProjectArticle").OrderByDescending(p => p.CreateDate).Take(8);
       
    <ul>
           
    @
    foreach(var item in articles.Where(x => x.IsVisible()){
               
    <li>
                       
    @item.Name
               
    </li>
            }
        </
    ul>
    }

    Looking forward to hear from you.

    /Dennis

  • Clickfarm 77 posts 161 karma points
    Jan 26, 2015 @ 19:04
    Clickfarm
    0

    @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:

  • Dennis Aaen 4499 posts 18254 karma points admin hq c-trib
    Jan 26, 2015 @ 19:39
    Dennis Aaen
    0

    Hi Matt,

    A light just went straight to me. You are using strongly typed Razor so what if you do something like this.

    @{  
       
    var articles = Model.Content.Parent.Children.Where(p => p.ContentType.Alias=="ProjectArticle").OrderByDescending(p => p.CreateDate).Take(8);
       
    <ul>
           
    @foreach(var item in articles.Where(x => x.IsVisible()){
               
    <li>
                   
    @item.GetPropertyValue("ProductLine")
               
    </li>
            }
        </
    ul>
    }

    And you can add the return value. it could be a string like this:

    @item.GetPropertyValue<string>("ProductLine")

    Hope this helps,

    /Dennis

  • Clickfarm 77 posts 161 karma points
    Jan 26, 2015 @ 20:00
    Clickfarm
    0

    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

  • Dennis Aaen 4499 posts 18254 karma points admin hq c-trib
    Jan 26, 2015 @ 20:07
    Dennis Aaen
    0

    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

  • Clickfarm 77 posts 161 karma points
    Jan 26, 2015 @ 20:13
    Clickfarm
    0

    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")

            }

    }

    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?)
  • Clickfarm 77 posts 161 karma points
    Jan 26, 2015 @ 20:15
    Clickfarm
    0

    is this supposed to be a collection of IPublishedContent? Because it looks like that object does NOT have the IsVisible or GetPropertyValue methods/properties.

  • Clickfarm 77 posts 161 karma points
    Jan 26, 2015 @ 20:21
    Clickfarm
    101

    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.

  • Dennis Aaen 4499 posts 18254 karma points admin hq c-trib
    Jan 26, 2015 @ 20:34
    Dennis Aaen
    0

    Hi Matt,

    Good to hear that you found a solution, and can move on with your Umbraco project.

    /Dennis

Please Sign in or register to post replies

Write your reply to:

Draft