Copied to clipboard

Flag this post as spam?

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


  • Peter Laurie 47 posts 121 karma points
    Aug 28, 2019 @ 13:33
    Peter Laurie
    0

    UmbracoHelper not available in non-static C# class files

    Hi, In my efforts to port over our website code from v7 to v8, I am at present finding myself unable toi find a replacement for the following code:

    DynamicNode AdviceNode = new umbraco.MacroEngines.DynamicNode(umbraco.NodeFactory.Node.GetCurrent().Id);

    There does not seem to be DynamicNode in Umbraco 8, and the UmbracoHelper does not work in a class file, like a filter, which is not static.

    I got the following working in another static class:

    var helper = Current.UmbracoHelper; var settingsNode = helper.Content(1063);

    On this page, it all seems to be aimed towards view documents: https://our.umbraco.com/documentation/Reference/Templating/Mvc/querying

    Please can anyone shine some light on how to access the Umbraco 8 content in non-static classes.

    Thank you,

    Peter

  • Marc Goodson 2155 posts 14408 karma points MVP 9x c-trib
    Aug 28, 2019 @ 14:25
    Marc Goodson
    0

    Hi Peter

    There is some examples her of accessing the UmbracoContext and ContentCache from non Http Request places:

    https://our.umbraco.com/Documentation/Implementation/Services/

    I know NodeFactory and DynamicNode have beem removed in Umbraco 8

    IPublishedContent and UmbracoHelper would be the equivalents.

    And I think you'd need to use IUmbracoContextFactory + EnsureUmbracoContext(), to get access to the ContentCache

    https://our.umbraco.com/Documentation/Implementation/Services/#accessing-published-content-outside-of-a-http-request

    regards

    Marc

  • Peter Laurie 47 posts 121 karma points
    Aug 28, 2019 @ 15:52
    Peter Laurie
    0

    Hi Marc, Thank you for the heads up, I will read through these documents and see what I can work out.

    Kind regards,

    Pete

  • Peter Laurie 47 posts 121 karma points
    Sep 11, 2019 @ 08:28
    Peter Laurie
    0

    Hi Marc, I thought i'd post up my findings so far on this: I created a SiteSearch.cs file with the following:

    namespace Umbraco8.Utils.Services
    {
         public class SiteService : ISiteService
        {
          private readonly IUmbracoContextFactory _umbracoContextFactory;
          public SiteService(IUmbracoContextFactory umbracoContextFactory)
           {
              _umbracoContextFactory = umbracoContextFactory;
           }
    
    public IPublishedContent GetContentUsingAlias(string alias)
    {
      IPublishedContent content = null;
      using (UmbracoContextReference umbracoContextReference = _umbracoContextFactory.EnsureUmbracoContext())
      {
        IPublishedContentCache contentCache = umbracoContextReference.UmbracoContext.Content;
        IPublishedContent siteRoot = contentCache.GetAtRoot().FirstOrDefault();
        content = siteRoot?.FirstChild(f => f.ContentType.Alias == alias) ?? null;
      }
      return content;
    }
    
    public IPublishedContent GetContentUsingAlias_DescendantsOrSelf(string alias)
    {
      IEnumerable<IPublishedContent> contentList = null;
      IPublishedContent content = null;
      using (UmbracoContextReference umbracoContextReference = _umbracoContextFactory.EnsureUmbracoContext())
      {
        IPublishedContentCache contentCache = umbracoContextReference.UmbracoContext.Content;
        IPublishedContent siteRoot = contentCache.GetAtRoot().FirstOrDefault();
        contentList = siteRoot?.DescendantsOrSelf().Where(f => f.ContentType.Alias == alias);
        content = contentList.ElementAt(0);
      }
      return content;
    }
    
    public IPublishedContent GetContentUsingAliasTest(string alias)
    {
      IPublishedContent content = null;
      IPublishedContent content2 = null;
      IEnumerable <IPublishedContent> content3 = null;
      using (UmbracoContextReference umbracoContextReference = _umbracoContextFactory.EnsureUmbracoContext())
      {
        IPublishedContentCache contentCache = umbracoContextReference.UmbracoContext.Content;
        IPublishedContent siteRoot = contentCache.GetAtRoot().FirstOrDefault();
        content3 = siteRoot?.DescendantsOrSelf().Where(f => f.ContentType.Alias == alias);
        content2 = content?.FirstChild(f => f.ContentType.Alias == "websiteNavigationSettings") ?? null;
      }
      return content2;
    }
    
    public IPublishedContent GetContentUsingId(Int32 id)
    {
      IPublishedContent content = null;
      using (UmbracoContextReference umbracoContextReference = _umbracoContextFactory.EnsureUmbracoContext())
      {
        IPublishedContentCache contentCache = umbracoContextReference.UmbracoContext.Content;
        IPublishedContent siteRoot = contentCache.GetAtRoot().FirstOrDefault();
        content = siteRoot?.FirstChild(f => f.ContentType.Id == id) ?? null;
      }
      return content;
    }
    

    } }

    Then I created the Interface, ISiteService.cs:

    using System;
    

    using System.Collections.Generic; using Umbraco.Core.Models.PublishedContent;

    namespace Umbraco8.Utils.Services { public interface ISiteService { IPublishedContent GetContentUsingAlias(string templateAlias); IPublishedContent GetContentUsingAlias_DescendantsOrSelf(string templateAlias); IPublishedContent GetContentUsingAliasTest(string templateAlias); IPublishedContent GetContentUsingId(Int32 id); } }

    To use this in SurfaceControllers:

    Using Umbraco8.Utils.Services;
    

    using Umbraco.Core.Configuration; using Umbraco.Web; using Umbraco.Core.Services; using Umbraco.Core.Cache; using Umbraco.Core.Logging; using Umbraco.Core.Models.PublishedContent; using System.Web.Mvc;

    namespace Umbraco8.SurfaceControllers { public class CookieController : SurfaceController {

    private readonly ISiteService _siteService;
    
    public CookieController(ISiteService siteService)
    {
      _siteService = siteService ?? throw new ArgumentNullException(nameof(siteService));
    }
    

    To use this in a view or partial view:

    @inherits Umbraco.Web.Mvc.UmbracoViewPage
    

    @using Newtonsoft.Json; @using CommercialTrust.Utils.Services @using Current = Umbraco.Web.Composing.Current;

    @{ ISiteService SiteService = Current.Factory.GetInstance

    I have also found that i can also use this in a class file, such as a helper:

    using Umbraco8.Utils.Services;
    using System;
    using System.Collections.Generic;
    using Umbraco.Core.Models.PublishedContent;
    
    namespace Umbraco8.Utils
    {
       public class Helper
       {
           private readonly ISiteService _siteService;
    
           public Helper(ISiteService siteService)
           {
             _siteService = siteService ?? throw new ArgumentNullException(nameof(siteService));
            }
    public void IterateRecentNewsChildren(IPublishedContent parent, List<string> IgnoreAliasList, List<string> ignoreNodeList, List<string> nodeList)
    {
      foreach (var item in parent.Children)
      {
        IPublishedContent nextParent = _siteService.GetContentUsingId(item.Id);
        if (!IgnoreAliasList.Contains(item.ContentType.Alias) && !ignoreNodeList.Contains(item.Id.ToString()) && nextParent.GetProperty("umbracoNaviHide").GetValue().ToString() != "1")
        {
          nodeList.Add(item.Id.ToString());
    
        }
        if (!ignoreNodeList.Contains(item.Id.ToString()))
        {
          IterateRecentNewsChildren(nextParent, IgnoreAliasList, ignoreNodeList, nodeList);
        }
      }
    }
       }
    }
    
Please Sign in or register to post replies

Write your reply to:

Draft