Copied to clipboard

Flag this post as spam?

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


  • Berto 105 posts 177 karma points
    Aug 06, 2013 @ 21:08
    Berto
    0

    Umbraco.TypedContentSingleAtXPath Help

    Hi everyone,

    So I want to use XPath to get a node, and I'm discovered that UmbracoHelper has the Umbraco.TypedContentSingleAtXPath method, but I don't have the #currentPage variable. I want to execute the following xpath:

    $currentPage/ancestor-or-self::* [@isDoc and name() = 'HomePage']/descendant-or-self::* [@isDoc and name = 'Job']
    

    My site will be multi language, so I want to go up the tree to the "root" (not the real root, but the language homepage) and get the Job node, with the right language.

    How can I get the this?

  • Tom Fulton 2030 posts 4998 karma points c-trib
    Aug 06, 2013 @ 23:04
    Tom Fulton
    1

    Hi,

    I don't believe you can use $currentPage etc in this context.

    What you might have to do is something like:

    var currentSite = <use some method to get the parent ID of the current site youre in>;
    
    var jobs = Umbraco.TypedContentSingleAtXPath("//* [@id = {0}]/descendant-or-self::* [@isDoc and name = 'Job']", currentSite.Id);
    

    This might have performance implications, but it might be the only way if you want to use XPath.

    -Tom

  • Berto 105 posts 177 karma points
    Aug 07, 2013 @ 00:27
    Berto
    0

    Hi Tom,

    But my main reason to use xpath ir because of performance... I still don't know the new api, but with Node Class and the uQuery, when I wanted to make some query a litle more performance, I used xPath, and in some sites there was a real diference with this. Maybe with the IPublished class it's direrent? I'm going to make some tests...

    Thx 

  • Tom Fulton 2030 posts 4998 karma points c-trib
    Aug 12, 2013 @ 06:13
    Tom Fulton
    0

    Hi Berto,

    Sorry about that, I see what you mean now. You can achieve this using the old NodeFactory and uQuery with something like this:

    var jobs = uQuery.GetCurrentNode().GetAncestorByPathLevel(1).GetDescendantNodesByType("Jobs");

    And if you're using MVC and want to take advantage of the new IPublishedContent, you can use something like this:

    var jobs = Model.Content.AncestorOrSelf("HomePage").Descendants("Job");

    Side note - it might be better if you can narrow down where your Jobs doctypes will be and avoid using Descendants - something like this might be best (assuming you always know where the Jobs content will exist):

    var jobs = Model.Content.AncestorOrSelf("HomePage").Children.First(x => x.IsDocumentType("Jobs")).Children.Where(x => x.IsDocumentType("Job"));

    Hope that helps, Tom

  • Berto 105 posts 177 karma points
    Aug 12, 2013 @ 11:56
    Berto
    0

    Hi Tom,

    That is what I'm doing right now, but I wan't to use the xpath. About a year ago I had some performance problems with a site, you can check it here: http://our.umbraco.org/projects/backoffice-extensions/ucomponents/questionssuggestions/31315-Performance-problem-with-uQuery

    And the main solution was to either use xpath or GetNodeByType. In this case I just want to use the xpath because of the multi language.

    Basically, I just want to know how can I with xPath and the current node, get other contents, or, what is the substitute of the variable $currentPage.

    Thanks for the patience and the help!

    Berto

  • Tom Fulton 2030 posts 4998 karma points c-trib
    Aug 19, 2013 @ 02:17
    Tom Fulton
    0

    Hi Berto,

    I see what you mean. Unfortunately I don't think this method supports variables, so you'll probably have to do something like this:

    var currentSite = umbraco.NodeFactory.Node.GetCurrent(); // assuming older umbraco version?
    var jobs = Umbraco.TypedContentSingleAtXPath("//* [@isDoc][@id = {0}]/descendant-or-self::* [@isDoc and name = 'Job']", currentSite.Id);
    

    Hope that makes sense, Tom

  • andrew shearer 513 posts 663 karma points
    Aug 19, 2013 @ 06:50
    andrew shearer
    0

    within "UmbracoTemplatePage" view:

    var home = Model.Content.AncestorOrSelf("home");
    var jobs = Umbraco.TypedContentSingleAtXPath("//* [@isDoc][@id = {0}]/descendant-or-self::* [@isDoc and name = 'Job']", home.Id);
    

    within Surfacecontroller:

    var home = SurfaceController.CurrentPage.AncestorOrSelf("home");
    var jobs = Umbraco.TypedContentSingleAtXPath("//* [@isDoc][@id = {0}]/descendant-or-self::* [@isDoc and name = 'Job']", home.Id);
    
  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies