Copied to clipboard

Flag this post as spam?

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


  • Koen Defrancq 14 posts 35 karma points
    Nov 18, 2012 @ 16:51
    Koen Defrancq
    0

    Root in MVC view

    In a razor macro I could do next code and get the content root (I traversed the root to get the languages) I know wrong setup but was basically a single language site now we want multilanguage ;)

       var contentRoot = Library.NodeById(-1);

    So trying to convert everything into views i stumbled upon this thing : I can no longer get to the root.

    tried following

    var contentRoot = Model.AncesterOrSelf(-1)
    var contentRoot = Model.AncesterOrSelf(0)
    var contentRoot = Umbraco.TypeContent(-1)
    var contentRoot = Umbraco.TypeContent(0)
    var contentRoot = Umbraco.Content(-1)
    var contentRoot = Umbraco.Content(0)

    Anyone have a clue or will i have to harcode the language nodeIds?

  • Tommy Turkijevic 7 posts 28 karma points
    Nov 20, 2012 @ 20:34
    Tommy Turkijevic
    0

    Hi Koen,

    I have the same problem, and no one seem to answer my post also,

    I was able to get the root using uQuery by usinh a syntax like this in razor.

    var latestNews = umbraco.uQuery.GetNode(-1).GetDescendantNodesByType("NewsItem").OrderByDescending(t => t.CreateDate).ToList();

    I do not know if it is the correct way to do it but it worked.

    /Tommy

  • Koen Defrancq 14 posts 35 karma points
    Nov 20, 2012 @ 21:02
    Koen Defrancq
    0

    hi Tommy,

    Thx for the tip will try it out what i ended up doing was this : 

    var root = library.GetXmlNodeByXPath("//root");

    But I have a feeling I'm using a bug fot this one, because it seems like no matter what xPath i give to library.GetXmlNodeByXPath it always returns the root.

    If the following is my content tree and website is displaying page NL/Projecten/Project1 my script will check if this page is available in the other language if not it will try 1 level up and so on. this way i get the closest page in the different language and can diplay a good language selector.

    I decided to use it this way because then we can keep existing structure.

    Content
    --NL
    ----Home
    ----Projecten
    ------Project1
    ------Project2
    ----Agenda
    ----Nieuws
    --FR
    ----Home
    ----Projecten
    ------Project1
    ----Agenda

     So i use xpath to see in my root-var if the current paht is available in the other language like this

    var langXPath = "string(//root/langPage[@nodeName='" + lang + "']" + xpath + "/@id)";
    var otherLangId = root.Current.Evaluate(langXPath).ToString()

    so xpath is a var holding the XPath to the current page (from its language)

    The evaluate will give me the id of the node if it exists if not i cut the xPath string and try again I'm at language root.

    if i use the same XPath with library.GetXmlNodeByXPath(langXPath) I will still get the complete XML thats why I think its a bug no matter what xpath i give i always get everything.

    So i will have a look at your solutions 

    thanks

  • Koen Defrancq 14 posts 35 karma points
    Nov 20, 2012 @ 21:45
    Koen Defrancq
    0

    Hi Tommy,

    Works out great but intellisense also gave me this option

    var root = uQuery.GetRootNode();
    this gets the root node just as it says ;)
    For me its better this way because i dont feel save with my first option 
    Tnx
  • Scott 95 posts 277 karma points
    Jan 30, 2014 @ 16:05
    Scott
    0

    I know this is ooooold post but if anyone stumbles upon this again then I use

    @using umbraco.NodeFactory;
    @{
    var home = CurrentPage.AncestorOrSelf(1); //get first level
    Node root = new Node(-1); //get root
    }
    
Please Sign in or register to post replies

Write your reply to:

Draft