Copied to clipboard

Flag this post as spam?

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


  • Soeren Sprogoe 575 posts 259 karma points
    Jul 07, 2010 @ 11:54
    Soeren Sprogoe
    0

    Getting property of root node

    Hi guys,

    I have a habbit of storing site settings as properties on the root node. Now I need to get hold of these from a backend extension that I'm working on.

    How do I do that in C#?

    Remember, this is for a backend extension. So I probably don't have access to stuff like Node.GetCurrent() and such. Or do I?

    Best regards,
    Soeren Sprogoe

  • Ismail Mayat 4511 posts 10091 karma points MVP 2x admin c-trib
    Jul 07, 2010 @ 11:55
    Ismail Mayat
    0

    Soren,

    If its in backend extension use api and get Document object though you would need to hardcode the root id.

    Regards

    Ismail

  • Soeren Sprogoe 575 posts 259 karma points
    Jul 07, 2010 @ 12:02
    Soeren Sprogoe
    0

    Thanks Ismail,

    unfortunately hardcoding the root id is not an option, as it then would be impossible to migrate from dev to live.

    /Soeren S.

  • Matt Brailsford 4125 posts 22222 karma points MVP 9x c-trib
    Jul 07, 2010 @ 12:13
    Matt Brailsford
    0

    Hey Sorren,

    I don't think you would have access to Node.GetCurrent(), but you should still be able to access the XML and run an XPath to get the root element?

    Matt

  • Matt Brailsford 4125 posts 22222 karma points MVP 9x c-trib
    Jul 07, 2010 @ 12:14
    Matt Brailsford
    0

    Checkout a the Umbraco Helper methods on Hendys blog for some extension methods to easily fetch nodes based on an XPath

    http://blog.hendyracher.co.uk/umbraco-helper-class/

    Matt

  • Soeren Sprogoe 575 posts 259 karma points
    Jul 07, 2010 @ 13:29
    Soeren Sprogoe
    0

    @Matt: Looks like Hendy's Helper uses GetCurrent() too.

    I gave up on this and moved the settings/properties that I need to the web.config instead. Not the perfect solution, but so far I've spent 2 hours trying to figure this out. And I just can't afford that.

    /SoerenS

  • Matt Brailsford 4125 posts 22222 karma points MVP 9x c-trib
    Jul 07, 2010 @ 23:24
    Matt Brailsford
    1

    Hi Soeren,

    Been there before, by FYI Hendy's methods only use GetCurrent() if it's possible to do so, and you don't have to use it in your xpath statement. You sould be able to run something like the following:

     UmbracoHelper.GetNodesFromXpath("//node[@nodeTypeAlias='homePageDocTypeAlias'");

    Assuming you only have one home page node, this should return a collection with one item in. I like to add an additional helper method to Hendys set for returning a single node from an XPath, as follows:

    public static Node GetNodeFromXpath(string xPath)
    {
        var nodes = UmbracoHelper.GetNodesFromXpath(xPath);


        if (nodes.Count > 0)
            return nodes[0];

        return null;
    }

    Matt

  • David Conlisk 432 posts 1008 karma points
    Jul 08, 2010 @ 11:02
    David Conlisk
    0

    Hi Soeren,

    Are you certain that you can't access Node.GetCurrent()?

    I'm using the following in a user control which I'm using as a custom action in the Umbraco back-end. So when a user right-clicks a node in the Content tree and chooses the custom action, this user control is called as a popup dialogue box. In the code-behind I'm using this code to get the home node of my site (which is not the root node, but change the 2 to a 1 should sort it for you):

    var homeId = int.Parse(Node.GetCurrent().Path.Split(',')[2]);
    Node homeNode = new Node(homeId);

    Or maybe you're using it in a different scenario where Node.GetCurrent() is undefined?

    David

  • Soeren Sprogoe 575 posts 259 karma points
    Jul 08, 2010 @ 13:09
    Soeren Sprogoe
    0

    @David: Yeah, doesn't work in my scenario. Just tested it.

    /SoerenS

  • Soeren Sprogoe 575 posts 259 karma points
    Jul 08, 2010 @ 13:52
    Soeren Sprogoe
    0

    @Matt: Ah, didn't read enough of Hendy's code to see that it actually checks if GetCurrent() is available.

    I got it to work now by using your GetNodeFromXPath method. Thanks :-)

    /SoerenS.

  • Matt Brailsford 4125 posts 22222 karma points MVP 9x c-trib
    Jul 08, 2010 @ 13:55
    Matt Brailsford
    0

    Sweet. Glad I could help.

    Matt

  • David Conlisk 432 posts 1008 karma points
    Aug 18, 2010 @ 18:39
    David Conlisk
    2

    Since upgrading to 4.5.1 this morning, Node.GetCurrent() fails in the back-end as described. However, with the new xml schema, I used the following (following from Matt's comments, above, and using Hendy's UmbracoHelper methods):

     

    Node homeNode = UmbracoHelper.GetNodeFromXpath(@"//*[local-name()='homepage']");

     

    Thanks for the help guys! High-fives handed out as deserved.

  • Simon steed 376 posts 688 karma points
    Jun 01, 2012 @ 09:44
    Simon steed
    0

    Hi All

    Old post but relevant to something i'm working on atm. Basically i've got a large multi site multi domain installation where the client wants to automatically assign the templates based upon the homepage template selection.

    I've got the code working to do this but only if I fix the homepage template in code so i've got a need to access the homepage properties to do this. This is all running in a Before Published event handler and I cannot find a way to accurately get the homepage of the site i'm publishing from

    Heres the code so far:

    void Document_BeforePublish(umbraco.cms.businesslogic.web.Document sender, umbraco.cms.businesslogic.PublishEventArgs e)
            {
                // get site root - how?
                List<Node> nodes = UmbracoHelper.GetNodesFromXpath(@"//*[local-name()='homepage']"); // get all the nodes
                Document itemPage = new Document(nodes[0].Id); //how do I know which is my currently selected site?
                // now i've got my itemPage, I can grab the properties for the template
            }
  • Matt Brailsford 4125 posts 22222 karma points MVP 9x c-trib
    Jun 01, 2012 @ 11:18
    Matt Brailsford
    0

    Hey Simon,

    Give the sender object is the current document being published, can't you navigate down it's parent chain till you hit a homepage? (might be quicker to use the node api though, assuming parent pages are published).

    Matt

  • Simon steed 376 posts 688 karma points
    Jun 01, 2012 @ 11:22
    Simon steed
    0

    Thanks Matt - that was my only other option really but was hoping I could do it a simpler way - Lazy coder I guess :-)

  • Simon steed 376 posts 688 karma points
    Jun 01, 2012 @ 11:23
    Simon steed
    0

    Thanks Matt - that was my only other option really but was hoping I could do it a simpler way - Lazy coder I guess :-)

  • Matt Brailsford 4125 posts 22222 karma points MVP 9x c-trib
    Jun 01, 2012 @ 11:26
    Matt Brailsford
    0

    The only problem with using BeforePublish is that you can't be sure the current node will be in the xml cache. If it was, you could probably do it all using Xpath queries (ie lookup nodes of type 'homepage' where one of it's decendants has the same ID as sender) which should be faster. But that would require you to fire your event on AfterPublish (might not be a big deal?). Failing that, as per my first suggestion, assuming paren nodes are also published, you could just use the documents parent property to query against the xml cache instead (keeping it faster).

    Matt

Please Sign in or register to post replies

Write your reply to:

Draft