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;
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
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
Hi Marc, Thank you for the heads up, I will read through these documents and see what I can work out.
Kind regards,
Pete
Hi Marc, I thought i'd post up my findings so far on this: I created a SiteSearch.cs file with the following:
} }
Then I created the Interface, ISiteService.cs:
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 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 {
To use this in a view or partial view:
@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:
is working on a reply...