Copied to clipboard

Flag this post as spam?

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


  • mizzle 90 posts 325 karma points
    May 22, 2019 @ 15:52
    mizzle
    0

    Better way to get nodes by ID?

    I'm trying to connect the content of a Partial to a single, specific node so that editors in Umbraco wouldn't need to use a content picker specify the same list for every page where the list appears. I want to include the partial on a template and make sure it always has the same content.

    The following code works, but for instances where the desired list might be changed, edited, or otherwise get a different ID, I was wondering if there were a way to "lock in" the information without having to go into the code and edit the ID value:

        var listNode = Umbraco.Content(3153);
    
    foreach (var item in listNode.Children.ToList())
    {
        <p> @item.Name</p>
    }
    
  • Amir Khan 1284 posts 2741 karma points
    May 22, 2019 @ 17:18
    Amir Khan
    0

    I understand that you might not want to use a content picker, but for things like this we often use a settings section on the home document type with a picker and retrieve it recursively so you don't have to select it on every page.

    Nice part about this is you can also override it any level below the homepage if you want to in the future.

    Basically this for v7: IPublishedContent typedContentPicker = Model.Content.GetPropertyValue

  • mizzle 90 posts 325 karma points
    May 22, 2019 @ 18:50
    mizzle
    0

    Thank you for the response,

    Could you clarify this a bit more with example code? I'm still new to MVC and the IPublishedContent typedContentPicker = Model.Content.GetPropertyValue isn't automatically understandable to me for what code to put where or how it should be set up in Umbraco.

    Hooking into a single content picker that can be retrieved recursively sounds like something along the lines of what I'm looking for. For old Umbraco 6 Webforms sites, I use a piece of code in a .cs file that detects a child node under a certain folder by a Name textstring, then uses the Content Picker on that child node to lock onto the actual content, and I can pull information from there. Unfortunately, I'm not too sure how to translate the code from something using DynamicNodes to MVC's standard. All I know is that it does not work as intended when pulled from one site and included on the MVC site.

  • Amir Khan 1284 posts 2741 karma points
    May 22, 2019 @ 20:15
    Amir Khan
    0

    So this will get the node with a content picker of alias "contentPickerAlias", the true tells it to look up the tree until it finds a value and store the nodes ID in typedContentPicker variable, you could put a content picker "contentPickerAlias" on your homepage, for example and no matter what page you add the partial to it would keep looking up the tree until it finds a value:

    IPublishedContent typedContentPicker = Model.Content.GetPropertyValue("contentPickerAlias", true);

    Then just do your normal loop (probably don't need the ToList bit):

    foreach (var item in typedContentPicker.Children.ToList()) {

    @item.Name

    }

  • mizzle 90 posts 325 karma points
    May 22, 2019 @ 20:35
    mizzle
    0

    I had to render the code as IPublishedContent typedContentPicker = Model.GetPropertyValue<IPublishedContent>("contentPickerAlias", true); to stop it from throwing an error in the editor, but even without errors in the editor it doesn't render. I get a "Object reference not set to an instance of an object." error on the page.

    I don't think it's actually searching through my node tree, so it's not finding the content picker alias I've set up.

  • Amir Khan 1284 posts 2741 karma points
    May 22, 2019 @ 20:37
    Amir Khan
    0

    Which version of Umbraco are you using and you're positive the alias is "contentPickerAlias"?

  • mizzle 90 posts 325 karma points
    May 22, 2019 @ 20:39
    mizzle
    0

    Umbraco version 7.14.0, and yes.

  • Amir Khan 1284 posts 2741 karma points
    May 22, 2019 @ 21:32
    Amir Khan
    0

    I wonder if something has changed with the recursive method at some point. I'll try to look tonight.

Please Sign in or register to post replies

Write your reply to:

Draft