Press Ctrl / CMD + C to copy this to your clipboard.
This post will be reported to the moderators as potential spam to be looked at
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())
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
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.
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())
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.
IPublishedContent typedContentPicker = Model.GetPropertyValue<IPublishedContent>("contentPickerAlias", true);
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.
Which version of Umbraco are you using and you're positive the alias is "contentPickerAlias"?
Umbraco version 7.14.0, and yes.
I wonder if something has changed with the recursive method at some point. I'll try to look tonight.
is working on a reply...
Write your reply to:
Image will be uploaded when post is submitted