I'm just starting out with Umbraco on a project. We are starting with 4.7.
I am using Razor in the templates, and everything was going smoothly until I started looking at how to get the information on a page referenced by a Content Picker. I'd like to be able to obtain things like the title and nice url.
I've seen some information in the forums on doing this within u5, but I don't think those examples are suitable for the 4.7 environment. Any advice on how to approach this?
If you're starting out a new project, then you must be using the latest version of Umbraco 4.7, version 4.7.1.1, right? And it sounds like you'd like to get content, not media. The content picker will store the id of the picked node as a string. If the alias of the content picker property is "content", then:
var node = Library.NodeById(Model.content);
...will give you access to the node. node is now a DynamicNode, just like Model, so you have access to all its properties. Refer to the handy cheatsheet. For a link to the node using its title (name) and nice url:
I guess it's really user preference, but I'd probably do it this way:
@{ var site = Model.AncestorOrSelf("Example1");
if (site.aliasName.GetType() != typeof(DynamicNull) && site.aliasName != "") { var bla2 = Library.NodeById(site.aliasName);
<a href="@bla2.NiceUrl"> We like Umbraco </a> } }
It's not really that different... but I like using the Library methods instead of contructors, but that's just me. I also like checking for DynamicNull, which is what an alias would return if the node didn't have the property.
Content Picker in u4.7 with Razor
I'm just starting out with Umbraco on a project. We are starting with 4.7.
I am using Razor in the templates, and everything was going smoothly until I started looking at how to get the information on a page referenced by a Content Picker. I'd like to be able to obtain things like the title and nice url.
I've seen some information in the forums on doing this within u5, but I don't think those examples are suitable for the 4.7 environment. Any advice on how to approach this?
Hi Chris
Try using the umbraco.library.NiceUrl(int) method.
@umbraco.library.NiceUrl(id)
Cheers
Evan
Get Media item with the following:
Get the Url for the media item with the following:
If you're starting out a new project, then you must be using the latest version of Umbraco 4.7, version 4.7.1.1, right? And it sounds like you'd like to get content, not media. The content picker will store the id of the picked node as a string. If the alias of the content picker property is "content", then:
...will give you access to the node. node is now a DynamicNode, just like Model, so you have access to all its properties. Refer to the handy cheatsheet. For a link to the node using its title (name) and nice url:
Hope that helps!
The node code was perfect!! Thank you!
Though I wasn't looking for media, thanks for that information too, because media is my next adventure.
No problem, glad I could help.
What do you think about this method?
It works, but I'm just curious, maybe you would do it a different way :)
I guess it's really user preference, but I'd probably do it this way:
It's not really that different... but I like using the Library methods instead of contructors, but that's just me. I also like checking for DynamicNull, which is what an alias would return if the node didn't have the property.
is working on a reply...