Copied to clipboard

Flag this post as spam?

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


  • Andrew Camilleri 7 posts 78 karma points
    Apr 02, 2019 @ 15:07
    Andrew Camilleri
    0

    Access ApplicationContext.Current.Services.ContentService from a Class

    I am trying to access ApplicationContext.Current.Services.ContentService from a class to get content and do some modifications to it. I am also trying to access the UmbracoHelper In the Older version I had the following lines of code.

    private readonly IContentService _contentService = ApplicationContext.Current.Services.ContentService;
    private readonly Umbraco.Web.UmbracoHelper _umbracoHelper = new UmbracoHelper(UmbracoContext.Current);
    var content = contentService.GetById(1234);
    

    This is no longer available in Umbraco 8 however I cant find a reference to the new methods to access code from code behind.

  • Dennis Flæng Jørgensen 35 posts 145 karma points c-trib
    Apr 02, 2019 @ 17:51
    Dennis Flæng Jørgensen
    0

    I'm not sure if this is how Umbraco intented for us to use it, but you can access them through Umbraco.Web.Composing.Current

    Please note I use Umbraco.Web - and not Umbraco.Core. Umbraco.Core does not expose UmbracoHelper.

    If you cannot access the namespaces/classes below - please ensure that your web application is running .NET framework 4.7.2 or above

    public class MyHelpers
    {
        private readonly Umbraco.Core.Services.IContentService _contentService = Umbraco.Web.Composing.Current.Services.ContentService;
        private readonly Umbraco.Web.UmbracoHelper _umbracoHelper = Umbraco.Web.Composing.Current.UmbracoHelper;
        public void DoStuff()
        {
            var content = _contentService.GetById(1234);
        }
    }
    
Please Sign in or register to post replies

Write your reply to:

Draft