Do we always need the Umbraco context to get nodes from cache?
Hello again :-)
Is there a way to get nodes from the cache without depending on the Umbraco context? I am writing a class library that hopefully will make it easier to retrieve content from the tree, read fields from nodes and return images/content picker relations/etc as strongly types entities.
I am now forced to force client programmers of my lib to always provide an Umbraco.Context, because the UmbracoHelper needs an Umbraco.Context instance to work. If you create the UmbracoHelper without a Umbraco.Context instance as its constructor parameter it will throw the this error if you use the helper to fetch nodes:
No Umbraco.Web.UmbracoContext reference has been set for this Umbraco.Web.UmbracoHelper instance
What is the rationale behind this? The Umbraco cache is just an XML document so why would a context instance be required to access it?
As a side note, I must say I am really impressed with the improvements in the Umbraco 7 interface. It is much more modern, clean and responsive. Good job.
The real question boils down to "why do we need the UmbracoContext when creating an UmbracoHelper?".
// The helper below works, but we have to be in a controller in order to have access to the UmbracoContext UmbracoHelper helper = new UmbracoHelper(UmbracoContext);
// The code below compiles, but the helper can't be used for fetching nodes. It throws an exception when you try // to fetch nodes with it: "No Umbraco.Web.UmbracoContext reference has been set for this Umbraco.Web.UmbracoHelper // instance" UmbracoHelper helper = new UmbracoHelper();
So the fetching of nodes via the UmbracoHelper is dependent on the UmbracoContext. Why is that? And can I access the node tree (cache!) without having to supply the UmbracoContext somehow?
UmbracoContext is documented as: Class that encapsulates Umbraco information of a specific HTTP request
There's a LOT of things going on in Umbraco to serve up content seemingly simply coming from and XML file. There's things like routing to consider, 404's, redirects, preview mode, debug mode, etc., etc., etc.
If you just want access to the raw data, it would be easiest to just open an XML reader on the umbraco.config file of course. ;-)
The other hack I can think of is to just run the site in a seperate IIS and Web API to get content out of it, plenty of sites running that way (http://our.umbraco.org/documentation/Reference/WebApi/).
Do we always need the Umbraco context to get nodes from cache?
Hello again :-)
Is there a way to get nodes from the cache without depending on the Umbraco context? I am writing a class library that hopefully will make it easier to retrieve content from the tree, read fields from nodes and return images/content picker relations/etc as strongly types entities.
I am now forced to force client programmers of my lib to always provide an Umbraco.Context, because the UmbracoHelper needs an Umbraco.Context instance to work. If you create the UmbracoHelper without a Umbraco.Context instance as its constructor parameter it will throw the this error if you use the helper to fetch nodes:
No Umbraco.Web.UmbracoContext reference has been set for this Umbraco.Web.UmbracoHelper instance
What is the rationale behind this? The Umbraco cache is just an XML document so why would a context instance be required to access it?
As a side note, I must say I am really impressed with the improvements in the Umbraco 7 interface. It is much more modern, clean and responsive. Good job.
I don't have an answer of the top of my head, but it is always helpful to show some code so we can see how you're trying to get those nodes.
The real question boils down to "why do we need the UmbracoContext when creating an UmbracoHelper?".
So the fetching of nodes via the UmbracoHelper is dependent on the UmbracoContext. Why is that? And can I access the node tree (cache!) without having to supply the UmbracoContext somehow?
UmbracoContext is documented as:
Class that encapsulates Umbraco information of a specific HTTP request
There's a LOT of things going on in Umbraco to serve up content seemingly simply coming from and XML file. There's things like routing to consider, 404's, redirects, preview mode, debug mode, etc., etc., etc.
People trying to unit test Umbraco face similar problems that you do. We're working on improving that, but currently it is what it is. https://groups.google.com/forum/?fromgroups=#!topic/umbraco-dev/vEjdzjqmtsU
There's some hints (but it looks like it's still work in progress) in this issue here: http://issues.umbraco.org/issue/U4-1717 Further reading: http://jlusar.es/unit-test-umbraco-rendermvccontroller and http://our.umbraco.org/forum/developers/api-questions/37255-How-can-I-unit-test-a-class-inheriting-from-SurfaceController (has an example using reflection to create UmbracoContext).
You might have more success with the NodeFactory (http://our.umbraco.org/wiki/reference/api-cheatsheet/working-with-nodefactory to get nodes by id) but I don't know exactly what dependencies need to be satisfied for that one. It's probably not going to help much for you.
If you just want access to the raw data, it would be easiest to just open an XML reader on the umbraco.config file of course. ;-) The other hack I can think of is to just run the site in a seperate IIS and Web API to get content out of it, plenty of sites running that way (http://our.umbraco.org/documentation/Reference/WebApi/).
Ok, thanks for the info. I will keep my lib tied to the Umbraco context for now and I will see what is coming in the future.
Hi Julius and Sebastiaan,
You might already be aware of this, but I think the following line:
should read:
No? :-)
All the best,
Bo
is working on a reply...