within a helper class you usually won't have access to the UmbracoContext, so you either a) have to create it or b) extend it with your helper functions.
you can create it
var umbHelper = new UmbracoHelper(UmbracoContext.Current);
but this is not ideall (because its a refrence based on internal singletons and not always best - there are some long threads on this
a better way is to extend the class
as an example :
public static class MyHelperExtension {
public static string DoSomethingClever(this UmbracoHelper umbraco, string mything) {
// umbraco.TypedContent() will work in here
}
}
then in your razor you can do
Umbraco.MyHelperExtension("something");
and your code will be called and you don't need to create or manage your own Umbraco Helper objects.
Where / when is your helper library going to be called ?
if you are calling it from a URL (like a REST API call) then you can inherit from the UmbracoApiController class and that has an Umbraco object - Similary the SurfaceController class also has one. and if you want to do something with the backoffice then the UmbracoBackOfficeApiController will also let you get to Umbraco object .
which road you go down will really depends on how the helper library is being called.
If you are talking about standalone - not called via IIS as part of any website request eg from a command line - then there is a lot more to do in terms of getting the whole Umbraco application context up and it a bit more of an in depth topic.
Get a node by id in umbraco as IPublishedContent or similar?
Hi I was just wondering how to get node by id in v7 seeing as @Libraray is gone?
Cheers,
Tom
Hi Tom
Have you tried one of these solution.
Or if you want to have multiple ids:
The documentation can be found here: http://our.umbraco.org/documentation/Reference/Mvc/querying
I hope this can help you.
/Dennis
Is this code for a Razor view? How do I do this in a code, i.e. in a helper class?
Hi,
within a helper class you usually won't have access to the UmbracoContext, so you either a) have to create it or b) extend it with your helper functions.
you can create it
but this is not ideall (because its a refrence based on internal singletons and not always best - there are some long threads on this
a better way is to extend the class
as an example :
then in your razor you can do
and your code will be called and you don't need to create or manage your own Umbraco Helper objects.
But I don't want somethin that works in Razor but something that works in a helper library.
OK,
Where / when is your helper library going to be called ?
if you are calling it from a URL (like a REST API call) then you can inherit from the UmbracoApiController class and that has an Umbraco object - Similary the SurfaceController class also has one. and if you want to do something with the backoffice then the UmbracoBackOfficeApiController will also let you get to Umbraco object .
which road you go down will really depends on how the helper library is being called.
If you are talking about standalone - not called via IIS as part of any website request eg from a command line - then there is a lot more to do in terms of getting the whole Umbraco application context up and it a bit more of an in depth topic.
is working on a reply...