Copied to clipboard

Flag this post as spam?

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


  • Kasper 14 posts 49 karma points
    Jun 05, 2020 @ 20:15
    Kasper
    1

    How to get Content(node) by id in c#

    Hi there, using Umbraco 8 and having some troubles find the right way for getting a a node by nodeid programmatically in C#.

    help much appreciated :)

  • Nik 1614 posts 7260 karma points MVP 7x c-trib
    Jun 05, 2020 @ 20:59
    Nik
    1

    Hi Kasper,

    Are you trying to get a Content node, or the Published version of it?

    If you are trying to get the Content node you need to use the Content Service. On there you'll find a method called (I think), GetById into which you can pass the Id. In general however, you probably don't want to use the content service as this hits the database.

    Generally you are after the most recent published version of content node in question in which case you can access this via an UmbracoHelper instance.

    For example, if you are in a controller that inherits from any of the Umbraco base controllers, e.g. Surface Controller, RenderMVC Controller, etc, they all expose a property called Umbraco which is a helper, so you can make a call like this Umbraco.Content(myContentsId);

    Alternatively, if you are using your own service you can, using Dependency Injection, get access to IUmbracoContextFactory. To access the published content cache using this is as follows (where umbracoContextReference is the passed in instance of IUmbracoContextFactory:

    using (var umbracoContextReference = umbracoContextFactory.EnsureUmbracoContext())
    {
        var content = umbracoContextReference.UmbracoContext.Content.GetById(id);        
    }
    

    Hope that helps,

    Nik

  • Kasper 14 posts 49 karma points
    Jun 05, 2020 @ 21:32
    Kasper
    0

    Hi Nik

    Thanks for replying. I am trying to access the Published version of a Content Node and values from it. But nothing i do seems to work.

    Can you by any chance post which namepaces to use and a complete method to call a Content node by id.

  • Kasper 14 posts 49 karma points
    Jun 05, 2020 @ 22:06
    Kasper
    0

    Finaly found a solution...

        public IPublishedContent getUmbracoNode(int nodeid)
        {
    
            var helper = Umbraco.Web.Composing.Current.UmbracoHelper;
            IPublishedContent node = helper.Content(nodeid);
            return node;
    
        }
    
  • Bo Jacobsen 606 posts 2404 karma points
    Jun 05, 2020 @ 22:37
    Bo Jacobsen
    102

    Hi Kasper.

    Just a heads up. Its not recommended to use Umbraco.Web.Composing.Current.UmbracoHelper because it might not always be populated.

    The way to do it, is like Nik suggested.

    using Umbraco.Core.Models.PublishedContent;
    using Umbraco.Web;
    
    namespace Example.Services
    {
        public class ExampleService : IExampleService
        {
            private readonly IUmbracoContextFactory UmbracoContextFactory;
    
            public ExampleService(IUmbracoContextFactory umbracoContextFactory)
            {
                UmbracoContextFactory = umbracoContextFactory;
            }
    
            public IPublishedContent getUmbracoNode(int nodeid)
            {
                using (var umbracoContextReference = umbracoContextFactory.EnsureUmbracoContext())
                {
                    return umbracoContextReference.UmbracoContext.Content.GetById(nodeid);
                }
            }
        }
    }
    

    You then need to register your service in a composer.

    using Umbraco.Core;
    using Umbraco.Core.Composing;
    using Example.Services;
    
    namespace Example.Composers
    {
        public class InstallerComposer : IUserComposer
        {
            public void Compose(Composition composition)
            {
                composition.Register<IExampleService, ExampleService>();
            }
        }
    }
    
Please Sign in or register to post replies

Write your reply to:

Draft