Copied to clipboard

Flag this post as spam?

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


  • Toby 10 posts 110 karma points
    Oct 28, 2020 @ 11:27
    Toby
    0

    How to get a node by it's name?

    Hi there,

    I'm getting a node by it's ID during development, but I know that the ID is probably going to be different in test/prod, so I want to get the same (default content) node by it's name. How would I go about that?

    It's a default copy node in a set of global document types (containing some widget text properties). I get the default node to populate the widget text when one hasn't been selected for the current document.

    Currently: textNode = Umbraco.TypedContent(11315);

    But I'm expecting that I can do something like:

    textNode = Umbraco.GetNodesByNamepath/search thing;

    The menu-path to the node I want is Global > Signup > Default Signup, where 'Default signup' is the node that has the properties I want (with ID 11315, as above).

    I looked at using XPath, but wasn't sure this was the best way.

    Perhaps I can set a default item without this (I am happy to set a default content from the content picker, but I spent some time looking into it and couldn't see a way of doing it through the Umbraco interface).

    ** The important thing is that the default item is set for all existing pages, not just new ones!

  • Marc Goodson 2157 posts 14431 karma points MVP 9x c-trib
    Oct 28, 2020 @ 19:56
    Marc Goodson
    0

    Hi Toby

    With Umbraco V7, the published content cache is stored as XML, and so XPath is the super fastest most performant way to find a node...

    ... if you look in your App_Data folder, you'll likely find a file called umbraco.config, and this is the XML document that is the published cache...

    what you'll find here is that the Umbraco content is published as XML node structures, using the alias of the document type of the page, and the actual 'names' of the items are stored as an attribute called nodeName.

    Anyway, if you have Global > Signup > Default Signup, then you will probably want something like this

    var defaultSignup = Umbraco.TypedContentSingleAtXPath("root/globalSettings/signUpFolder/signUpitem[@nodeName='Default Signup'");
    

    But will depend on the alias of each doc type for each content item down the tree until you get to the node you want to pick.

    So here I'm guessing that your Global content item has doc type alias globalSettings, and your Signup content item has doc type alias signUpFolder... etc

    hopefully that gives you the gist?

    It's case sensitive and usually takes 4 or 5 attempts to get it 'right'!!

    regards

    Marc

  • Toby 10 posts 110 karma points
    Oct 29, 2020 @ 10:08
    Toby
    0

    Thanks Marc, that was really informative (I wish this was in the docs!).

    I tried it, but I could not find the name of my content in the umbraco.config file (I didn't even try the Xpath you recommended, since the nodes didn't exist). I figured it was because the cache was out of date, so I followed the advice on this site to refresh it, but nothing appears to have changed in that file.

    https://www.jondjones.com/learn-umbraco-cms/umbraco-7-tutorials/umbraco-caching/how-to-refresh-the-umbraco-cache/

    So, I did the URL-based refresh and also right-clicking on the global node and choosing publish (excluding unpublished items). I can see the content on my site, so I know that it has been published. I just don't see any references to the node name (or even content) in the xml file. That seems strange, is there another step I can perform to force this? Or is something else going on?

    For detail: I can see loads of content in the file, it seems to cover everything except my new signup content. I've deleted the temp/examineindexes folder and restarted, tried the publish entire site again, but still the file hasn't changed.

    I've searched using the IDs, too, but nothing. The modified date of my umbraco.config is 12/08/2020, so it's clearly not being updated with the content.... :/

    I've checked the web.config and it's pointing the xmlcontent to app_data as expected.

    What's weirder, is that the logs say they are writing to an XML file:

    2020-10-29 10:15:49,135 [P11228/D2/T18] INFO  umbraco.BusinessLogic.Log - Log scrubbed.  Removed all items older than 2020-10-28 10:15:49
     2020-10-29 10:15:57,003 [P11228/D2/T15] INFO  umbraco.content - Save Xml to file...
     2020-10-29 10:15:57,060 [P11228/D2/T15] INFO  umbraco.content - Saved Xml to file.
     2020-10-29 10:17:00,605 [P11228/D2/T41] INFO  umbraco.content - Loading content from database...
     2020-10-29 10:17:04,723 [P11228/D2/T47] INFO  umbraco.content - Save Xml to file...
     2020-10-29 10:17:04,777 [P11228/D2/T47] INFO  umbraco.content - Saved Xml to file.
     2020-10-29 10:39:42,220 [P11228/D2/T382] INFO  umbraco.content - Loading content from database...
     2020-10-29 10:39:46,335 [P11228/D2/T396] INFO  umbraco.content - Save Xml to file...
     2020-10-29 10:39:46,391 [P11228/D2/T396] INFO  umbraco.content - Saved Xml to file.
     2020-10-29 10:40:44,062 [P11228/D2/T377] INFO  Umbraco.Core.PluginManager - Resolving umbraco.interfaces.IDiscoverable
     2020-10-29 10:40:44,063 [P11228/D2/T377] INFO  Umbraco.Core.PluginManager - Resolved umbraco.interfaces.IDiscoverable (took 0ms)
     2020-10-29 10:40:44,063 [P11228/D2/T377] INFO  Umbraco.Core.PluginManager - Resolving Umbraco.Web.HealthCheck.HealthCheck
     2020-10-29 10:40:44,078 [P11228/D2/T377] INFO  Umbraco.Core.PluginManager - Resolved Umbraco.Web.HealthCheck.HealthCheck (took 14ms)
    
  • Marc Goodson 2157 posts 14431 karma points MVP 9x c-trib
    Oct 29, 2020 @ 12:42
    Marc Goodson
    100

    Hi Toby

    There is a setting that will use the Environment Temp folder as the location for the published cache file,

    in which case the file would be in your ASP.Net temporary folder, outside of the web app.

    Essentially when you publish a file it updates a version of the config file in memory, then moments later, writes this to disk - when Umbraco starts up it reads the contents of the file on disk into memory... so if after a site restart, your content is still published, then it's writing to the published cache file in a different location to App_Data

    Even without the published cache file, you can still probably guess the XPath, by looking at each item above your default signup, seeing what type of Document Type it is, and making a note of it's alias... to give it a whirl...

    to experiment you could start with a really lazy Xpath expression eg

    //defaultEmailSignUpAlias

    the double slash just means find all content published anywhere with a doc type alias 'defaultEmailSignUpAlias

    it isn't the most efficient way, eg having the exact path as mentioned above is the quickest - but it's quite exciting when you start returning content from an XPath expression, and you can revise and experiment from there!

    regards

    Marc

  • Toby 10 posts 110 karma points
    Oct 29, 2020 @ 13:22
    Toby
    0

    Thanks, Marc, that's really helpful and it was exactly where you suggested. This is much easier than the alternative method I came up with in the meantime

    var signupNodeRoot = new umbraco.MacroEngines.DynamicNode(1215).GetChildrenAsList.Items.Where(x => x.Name == "Signup").FirstOrDefault();
    var defaultSignupBlock = new umbraco.MacroEngines.DynamicNode(signupNodeRoot.Id).GetChildrenAsList.Items.Where(x => x.Name == "Default Signup").FirstOrDefault();
    signupNode = Umbraco.TypedContent(defaultSignupBlock.Id);
    

    but now this works, too:

    signupNode = Umbraco.TypedContentSingleAtXPath("root/Global/signupBase/signup[@nodeName='Default Signup']");
    

    They both work, but the XPath one is much cleaner and doesn't use the ID, which feels dirty (even if I know it's never going to change).

    Thanks a lot for your help. Really good of you.

    Toby.

  • Marc Goodson 2157 posts 14431 karma points MVP 9x c-trib
    Oct 30, 2020 @ 19:31
    Marc Goodson
    0

    Hi Toby

    Great you got it working, also (in V7) this is the most efficient way too!

    There is an article here that might be of interest that compares the speeds of different approaches:

    https://skrift.io/issues/testing-the-performance-of-querying-umbraco/

    and also these slides from the UK festival in 2017 - of a talk by Dave Woestenborghs, are a good reference:

    https://www.slideshare.net/dawoe/the-need-for-speed-uk-fest

    Both articles talk about using the MiniProfiler that is shipped with Umbraco to measure how long different bits of code take to execute - there is a bit in the docs about getting started with this: https://our.umbraco.com/Documentation/Getting-Started/Code/Debugging/index-v7#miniprofiler if you ever are unsure if something 'that works' is a 'good way' in the context of your site!

    regards

    Marc

Please Sign in or register to post replies

Write your reply to:

Draft