Equivalent to GetXmlNodeById in Razor and Umbraco 5
Hi guys
I'm playing around with v5 of Umbraco the latest build, and I'd like to know if theres an equivalent function as GetXmlNodeById in Razor to use for grabbing some properties from a certain node?
Thanks for your answer. I tried it out using this code:
@{ var node = @Model.NodeById(1092); }
But this gives me a YSOD saying the following:
Compilation Error Compiler Error Message:CS1061: 'Umbraco.Cms.Web.Model.Content' does not contain a definition for 'NodeById' and no extension method 'NodeById' accepting a first argument of type 'Umbraco.Cms.Web.Model.Content' could be found (are you missing a using directive or an assembly reference?)
Just changed the @Model to @DynamicModel, but this gives me a different YSOD saying:
Could not finding matching Signature for 'NodeById' with 1 parameters. Did you mean to call one of these?
Line 256: "Could not finding matching Signature for '{0}' with {1} parameters. Did you mean to call one of these?\n{2}".InvariantFormat( Line 257: name, args.Length, allMethods)); Line 258: } Line 259: Line 260: private static string GetMethodsAsStringList(IEnumerable<DynamicMethod> dynamicMethods)
Looks pretty crazy in my frontend eyes, but as you say Sebastiaan, I surely also hope this gets a lot easier when the Razor engine is fully finished in V5. But thanks for helping out once again :)
We took this example a bit further by the way, and took the id from a content picker, instead of just harcoding in an id. For anyone interesting this was how we did it:
@{ var myVar = Umbraco.GetEntityById(HiveId.Parse((string)DynamicModel.ExtraInfo)); } @(myVar.Attribute<string>("title"))
The above example will output the "title"-property from the node selected in a content picker(with an alias of extraInfo) on the curent page.
Just an update on this, if you're using GetEntityId directly on a DynamicModel, you have to cast it to a typed entity:
@{ var mediaId = DynamicModel.SomePropertyName; var media = (TypedEntity)Umbraco.GetEntityById(mediaId); } @(media.Attribute<string>("myCustomProperty"))
I've been stuck on this part for about 3 days now, i wanna load a node from Id (content picker, wich gives me a hive id) and maybe use its own properties (attributes fine, but is it possible to get it as a bendy object like DynamicModel ?), But most of all i want to loop through its children and stuff, how do i do this.
I havent tried grabbing another node, and then looping through the children of that node, but would be fairly usefull I think.
If anyone finds a more elegant (more easy-to-remember) way of grabbing content from other nodes in the content tree, please share them in this post. Just thought that there might be some changes to the way we can grab content in some of the new releases of the v5 source.
If not, we'll go on with the solution we have now :)
Equivalent to GetXmlNodeById in Razor and Umbraco 5
Hi guys
I'm playing around with v5 of Umbraco the latest build, and I'd like to know if theres an equivalent function as GetXmlNodeById in Razor to use for grabbing some properties from a certain node?
Thanks in advance :)
/Kim A
You can get other nodes by using @Model.NodeById(1092) for example.
Hi Sebastiaan
Thanks for your answer. I tried it out using this code:
But this gives me a YSOD saying the following:
My entire file looks like this:
Am I missing something obvious?
-Thanks...
/Kim A
By the way, just tried changing the snippet to:
Just changed the @Model to @DynamicModel, but this gives me a different YSOD saying:
Just wanted to tell, as I don't know if the above information helps someone to help me :)
/Kim A
Ehm, ah Umbraco 5.
Razor in v5 is different from v4!
The only thing I've found to work so far is something like this:
Let's hope this gets easier..
Holy smokes :)
Looks pretty crazy in my frontend eyes, but as you say Sebastiaan, I surely also hope this gets a lot easier when the Razor engine is fully finished in V5. But thanks for helping out once again :)
/Kim A
Just for information to anyone interested.
We're at the Umbraco Hackathon in Aarhus right now, and Matt Brailsford have come up with some help to this issue.
The following code works:
You can put in the id of a node in the GetEntityById and then grab the properties from that node using the .Attribute as shown above.
In the above example I'm using the following by the way:
/Kim A
We took this example a bit further by the way, and took the id from a content picker, instead of just harcoding in an id. For anyone interesting this was how we did it:
The above example will output the "title"-property from the node selected in a content picker(with an alias of extraInfo) on the curent page.
/Kim A
Thanks for sharing Kim. I really hope this gets much more easy once 5 is about to be released in final :)
/Jan
Just an update on this, if you're using GetEntityId directly on a DynamicModel, you have to cast it to a typed entity:
Great Sebastiaan.
I have to try that snippet out as well, to see if I can grab a media-node from a media picker.
Thanks for sharing!
/Kim A
I've been stuck on this part for about 3 days now, i wanna load a node from Id (content picker, wich gives me a hive id) and maybe use its own properties (attributes fine, but is it possible to get it as a bendy object like DynamicModel ?), But most of all i want to loop through its children and stuff, how do i do this.
both:
Hive.Content.Where(x => x.Id == HiveId.Parse( strHiveID )).FirstOrDefault();
Umbraco.GetEntityById( strHiveID );
Gets me objects but, i cant seam to be able to get any children from them.
@Mattis:
I havent tried grabbing another node, and then looping through the children of that node, but would be fairly usefull I think.
If anyone finds a more elegant (more easy-to-remember) way of grabbing content from other nodes in the content tree, please share them in this post. Just thought that there might be some changes to the way we can grab content in some of the new releases of the v5 source.
If not, we'll go on with the solution we have now :)
/Kim A
is working on a reply...