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);
}
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>();
}
}
}
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 :)
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 thisUmbraco.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 (whereumbracoContextReference
is the passed in instance of IUmbracoContextFactory:Hope that helps,
Nik
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.
Finaly found a solution...
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.
You then need to register your service in a composer.
is working on a reply...