@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.
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:
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);
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?
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):
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 = newDocument(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
}
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).
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).
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
Soren,
If its in backend extension use api and get Document object though you would need to hardcode the root id.
Regards
Ismail
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.
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
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
@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
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:
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:
Matt
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):
Or maybe you're using it in a different scenario where Node.GetCurrent() is undefined?
David
@David: Yeah, doesn't work in my scenario. Just tested it.
/SoerenS
@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.
Sweet. Glad I could help.
Matt
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):
Thanks for the help guys! High-fives handed out as deserved.
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:
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
Thanks Matt - that was my only other option really but was hoping I could do it a simpler way - Lazy coder I guess :-)
Thanks Matt - that was my only other option really but was hoping I could do it a simpler way - Lazy coder I guess :-)
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
is working on a reply...