Copied to clipboard

Flag this post as spam?

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


  • Tony Groome 261 posts 804 karma points
    Jan 15, 2015 @ 14:01
    Tony Groome
    0

    Use of .Where()

    Hi All

    I have this piece of code:

    @foreach(var page in CurrentPage.Children.Where( "Visible" ))

    {

    Now I want to search through specific pages, namely Cafe and Information

    I tried:

    @foreach(var page in CurrentPage.Children.Where(DocumentTypeId == "cafe", "Visible" ))

    {

    for starters just to get the cafe one working, but there was an error telling me that I couldn't use DocumentTypeId here. Is there a way of doing this? If not I can just make new document types and do it that way!

    Thanks

    Tony

  • Jeavon Leopold 3074 posts 13631 karma points MVP 11x admin c-trib
    Jan 15, 2015 @ 14:05
    Jeavon Leopold
    3

    Hi Tony,

    I think you are after something like this:

    @foreach (var page in CurrentPage.Children.Where("Visible && DocumentTypeId == @0", "cafe"))
    

    Jeavon

  • Jan Skovgaard 11280 posts 23678 karma points MVP 11x admin c-trib
    Jan 15, 2015 @ 14:06
    Jan Skovgaard
    2

    Hi Tony

    In order for that to work you'll need to pass the id rather than the pagename I guess.

    If you look under the "Properties" tab for the pages you can find the id's. Try passing them instead of "cafe".

    Hope this makes sense.

    /Jan

  • Jeavon Leopold 3074 posts 13631 karma points MVP 11x admin c-trib
    Jan 15, 2015 @ 14:10
    Jeavon Leopold
    101

    You could filter by name

    @foreach (var page in CurrentPage.Children.Where("Visible && Name == @0", "cafe"))
    

    But it would break if a editor changed the name, probably best to go with the ID as Jan suggests

    @foreach (var page in CurrentPage.Children.Where("Visible && DocumentTypeId == @0", 1234))
    

    Jeavon

  • Jeavon Leopold 3074 posts 13631 karma points MVP 11x admin c-trib
    Jan 15, 2015 @ 14:11
    Jeavon Leopold
    3

    Or create unique document types, then

    @foreach (var page in CurrentPage.Children.Where("Visible && DocumentTypeAlias == @0", "myDocTypeAlias"))
    
  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Jan 15, 2015 @ 14:18
    Dennis Aaen
    1

    Hi Tony,

    And if you need to filter on more than one document type in the where, you will do it like this.

    @foreach(var page in CurrentPage.Children.Where("Visible && DocumentTypeAlias == @0 || DocumentTypeAlias == @1","myDocTypeAlias1","myDocTypeAlias2"))

    /Dennis

  • Tony Groome 261 posts 804 karma points
    Jan 15, 2015 @ 15:09
    Tony Groome
    0

    Crikey lads I went out for lunch and all this info!! 

    Thanks a lot. That's exactly what I've been trying to do, like what Jeavon said and Jan said about the Id (somebody could change the names all right!). 

    Thanks Dennis - I was wondering how to use && and || too. 

    :)

    Sorted! 

  • Jan Skovgaard 11280 posts 23678 karma points MVP 11x admin c-trib
    Jan 15, 2015 @ 16:44
    Jan Skovgaard
    1

    Hi Tony

    Yeah, it's nice right? :)

    I think that if possible then the best approach is to look for aliases if possible. If you know that everytime there is a document type of a certain alias then you want to do something specifically with that alias.

    If it's a certain page then it might be a good idea to use a checkbox on the page(s) so you can look for pages with the flag set and then do something specific to them. Looking for an ID can be fragile as well if someone by accident deletes the node and just creates a new one without restoring the original node. Then the site could break because the ID does no longer exist. Looking for a named node is also bad since if the name is changed the node won't be found.

    So aliases or checkboxes is usually the best practice. Perhaps even looking for "template" id's a some times as well.

    Just a little more input so you know what to consider for the different scenarios. Hope it's useful as well.

    /Jan

  • Tony Groome 261 posts 804 karma points
    Jan 15, 2015 @ 17:00
    Tony Groome
    0

    Hi Jan

    Thanks for that.

    I used the DocumentTypeAlias and got it to work. In this instance nobody will be deleting the document type - luckily for me as I couldn't get the node id to work! I got the id from the Content area under Properties, but it must be wrong! 

    There's for and against each approach all right!

    Thanks

    Tony

  • Steve Morgan 1348 posts 4457 karma points c-trib
    Jan 15, 2015 @ 19:12
    Steve Morgan
    2

    The ID under the properties tab of a content node is the node ID for that specific piece of content not the type. What you were using originally was DocumentTypeId .. If  ou want that if then you get that from settings > document types (on the move so not sure where exactly)!

     

     

    Hth

     

    Steve

     

     

     

  • Jan Skovgaard 11280 posts 23678 karma points MVP 11x admin c-trib
    Jan 15, 2015 @ 19:46
    Jan Skovgaard
    1

    Ah yes, sorry my bad! Steve is absolutely right about that one about the documenttypeid...I initially read "DocumentId". Very important distinction. And if you use the document type id then there is nothing to worry about either, unless you delete the document type but that's very unlikely.

    /Jan

  • Tony Groome 261 posts 804 karma points
    Jan 16, 2015 @ 11:02
    Tony Groome
    0

    I looked under Settings and Documents before for a node id but couldn't find any - that's why I went with the DocumentTypeAlias! 

    Thanks Steve & Jan

    Tony

  • Steve Morgan 1348 posts 4457 karma points c-trib
    Jan 16, 2015 @ 11:23
    Steve Morgan
    1

    Tony - you're quite right the DocumentTypeAliasId is not visibile in the Umbraco GUI! TBH judging on forum and blog posts "the community" generally just uses the alias anyhow (I know I do!).

    To get the Id the easy ways are to use Visual Studio and inspect or write it out on a test page / quick change to a Razor script :) 

    Glad you've got your problem sorted anyhow!


    Steve

  • Tony Groome 261 posts 804 karma points
    Jan 16, 2015 @ 11:29
    Tony Groome
    0

    Thanks Steve I thought it was hidden from me or I was going mad!! Problem well solved! It's great to find all the other ways - just in case for another day.

    At the moment it's not broke, so I'll leave it that way!!

    Tony

  • Paul Seal 524 posts 2889 karma points MVP 7x c-trib
    Jan 21, 2015 @ 22:24
    Paul Seal
    0

    I use:

    .Where ("documentTypeAlias == \"aliasName\"")

  • Paul Seal 524 posts 2889 karma points MVP 7x c-trib
    Jan 22, 2015 @ 11:46
    Paul Seal
    0

    I like this one by Dennis

    @foreach(var page inCurrentPage.Children.Where("Visible && DocumentTypeAlias == @0 || DocumentTypeAlias == @1","myDocTypeAlias1","myDocTypeAlias2"))

    If I were to use that method I would remove the duplication of DocumentTypeAlias, so it would be something like this.

    @foreach(var page inCurrentPage.Children.Where("Visible && @0 == @1 || @0 == @2","DocumentTypeAlias" ,"myDocTypeAlias1","myDocTypeAlias2"))
Please Sign in or register to post replies

Write your reply to:

Draft