Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can I not access UmbracoHelper in my own class

I am trying to get an image from Umbraco in my own class.

I can´t understand why I can not use @umbraco helper in my class. I have its namespace defined in the class, such as:

using Umbraco.Core.Models;
using Umbraco.Web;

I can only use UmbracoHelper, when I write:

var umbracoHelper = new UmbracoHelper(Umbraco.Web.UmbracoContext.Current);

Here is my class:

using Umbraco.Core.Models;
using Umbraco.Web;

namespace House.Site.Helpers
{
    public static class ImageClass
    {
        public static string GetAbsoluteImageUrl(this IPublishedContent node, string propertyName)
        {
            var umbracoHelper = new UmbracoHelper(Umbraco.Web.UmbracoContext.Current);

            var imgID = node.GetPropertyValue<string>("propertyName");
            var imgObject = umbracoHelper.TypedMedia(imgID);

            return imgObject.Url;
        }

    }
}
like image 949
user1032019 Avatar asked Dec 08 '22 19:12

user1032019


1 Answers

Since your class is not inheriting from any Umbraco base classes you will have to instantiate the UmbracoHelper yourself.

Doing so by using:

var umbracoHelper = new UmbracoHelper(UmbracoContext.Current)

is perfectly fine and will get you access to what you need.

You should be able to use just UmbracoContext.Current instead of the full namespace and class name, since you already have the using statement for Umbraco.Web.

like image 70
Claus Avatar answered Dec 27 '22 22:12

Claus