Copied to clipboard

Flag this post as spam?

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


  • Kim Andersen 1447 posts 2196 karma points MVP
    Dec 01, 2011 @ 13:46
    Kim Andersen
    0

    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

  • Sebastiaan Janssen 5058 posts 15520 karma points MVP admin hq
    Dec 01, 2011 @ 13:54
    Sebastiaan Janssen
    0

    You can get other nodes by using @Model.NodeById(1092) for example.

  • Kim Andersen 1447 posts 2196 karma points MVP
    Dec 01, 2011 @ 15:01
    Kim Andersen
    0

    Hi Sebastiaan

    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?)

     

    My entire file looks like this:

    @inherits RenderViewPage
    @using System.Web.Mvc.Html
    @using Umbraco.Cms.Web;
    @{
    Layout = "_DevDatasetLayout.cshtml";
    }

    @{
    var node = @Model.NodeById(1092);
    }

    <div id="content">
    <div class="sidebar-wrap">
    Venster side her
    </div>
    <div id="center-wrap">
    <div class="center-content-main">
    <h1>@DynamicModel.Name</h1>

    <div id="manchet">
    <h2>@DynamicModel.Manchet</h2>
    </div>

    <div id="main-text">@Umbraco.Field(Model, "content")</div>
    </div>
    </div>
    </div>

    Am I missing something obvious?

    -Thanks...

    /Kim A

  • Kim Andersen 1447 posts 2196 karma points MVP
    Dec 01, 2011 @ 15:56
    Kim Andersen
    0

    By the way, just tried changing the snippet to:

    @{
    var node = @DynamicModel.NodeById(1092);
    }

    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)

    Source File: d:\Data\HG\Umbraco Codeplex\Source\Libraries\Umbraco.Framework\Dynamics\BendyObject.cs    Line: 258

    Just wanted to tell, as I don't know if the above information helps someone to help me :)

    /Kim A

  • Sebastiaan Janssen 5058 posts 15520 karma points MVP admin hq
    Dec 02, 2011 @ 14:56
    Sebastiaan Janssen
    1

    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:

        var content  = Hive.Content.Where(x => x.Id == new HiveId("content://p__nhibernate/v__guid/00000000000000000000000000001049")).FirstOrDefault();

    Let's hope this gets easier..

  • Kim Andersen 1447 posts 2196 karma points MVP
    Dec 05, 2011 @ 21:01
    Kim Andersen
    0

    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

  • Kim Andersen 1447 posts 2196 karma points MVP
    Dec 08, 2011 @ 15:41
    Kim Andersen
    1

    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:

    @{
    var myNode = Umbraco.GetEntityById(HiveId.Parse("content://p__nhibernate/v__guid/00000000000000000000000000001048"));
    }

    @(myNode.Attribute<string>("propertyAlias"))

    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:

    @using Umbraco.Cms.Web;
    @using Umbraco.Framework;
    @using Umbraco.Framework.Persistence;

    /Kim A

  • Kim Andersen 1447 posts 2196 karma points MVP
    Dec 08, 2011 @ 21:42
    Kim Andersen
    3

    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.

    /Kim A

  • Jan Skovgaard 11280 posts 23678 karma points MVP 11x admin c-trib
    Dec 08, 2011 @ 23:23
    Jan Skovgaard
    0

    Thanks for sharing Kim. I really hope this gets much more easy once 5 is about to be released in final :)

    /Jan

  • Sebastiaan Janssen 5058 posts 15520 karma points MVP admin hq
    Dec 09, 2011 @ 11:42
    Sebastiaan Janssen
    1

    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"))  
  • Kim Andersen 1447 posts 2196 karma points MVP
    Dec 12, 2011 @ 08:47
    Kim Andersen
    0

    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

  • Mattias 52 posts 75 karma points
    Jan 07, 2012 @ 20:12
    Mattias
    0

    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.

  • Kim Andersen 1447 posts 2196 karma points MVP
    Jan 24, 2012 @ 21:42
    Kim Andersen
    0

    @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

Please Sign in or register to post replies

Write your reply to:

Draft