Press Ctrl / CMD + C to copy this to your clipboard.
This post will be reported to the moderators as potential spam to be looked at
How do you access IContent in a class in Umbraco 8?
I can access IPublishedContent no bother using dependency injection:
private readonly IUmbracoContextFactory _context; public MyController(IUmbracoContextFactory context) { _context = context; } public MyMethod(int currentNodeID) { using (var cref = _context.EnsureUmbracoContext()) { var cache = cref.UmbracoContext.Content; var currentNode = cache.GetById(currentNodeID); } }
Just dont have a clue how to get IContent.
Hi Marc
Have you tried this:
using System.Web.Mvc; using Umbraco.Core.Models; using Umbraco.Core.Services; namespace Umbraco814 { public class MyController { private IContentService _contentService; public MyController(IContentService contentService) { _contentService = contentService; } public ActionResult MyMethod(int currentNodeID) { IContent currentNode = _contentService.GetById(currentNodeID); return null; } } }
I got this working in a class library with UmbracoCms.Core and UmbracoCms.Web nuget packages installed.
Cheers
Paul
Hi Paul,
Thanks for that, I must have had a brain freeze for not trying that.
Cheers,
Marc
I got a notification on email that you asked about Descendants and Ancestors.
You can use the content service like this:
IContent currentNode = _contentService.GetById(currentNodeID); IEnumerable<IContent> ancestors = _contentService.GetAncestors(currentNode); IEnumerable<IContent> descendants = _contentService.GetPagedDescendants(currentNodeID, 0, 10, out long totalRecords);
Yes, I deleted the question as I figured it out, thanks for the info.
is working on a reply...
Write your reply to:
Upload image
Image will be uploaded when post is submitted
Access IContent in class file
How do you access IContent in a class in Umbraco 8?
I can access IPublishedContent no bother using dependency injection:
Just dont have a clue how to get IContent.
Hi Marc
Have you tried this:
I got this working in a class library with UmbracoCms.Core and UmbracoCms.Web nuget packages installed.
Cheers
Paul
Hi Paul,
Thanks for that, I must have had a brain freeze for not trying that.
Cheers,
Marc
Hi Marc
I got a notification on email that you asked about Descendants and Ancestors.
You can use the content service like this:
Cheers
Paul
Hi Paul,
Yes, I deleted the question as I figured it out, thanks for the info.
Cheers,
Marc
is working on a reply...