I need to access all of the child nodes of a node and get the one with a matching name.
In Umbraco 7, GetChildrenByName() made this very useful, however it seems this is deprecated in umbraco 8.
IContentService contentService = Services.ContentService;
var node = contentService.GetChildrenByName(parentNodeId,
nodeObject.nodeName).FirstOrDefault();
I assume I could access all of the child nodes of the parent node, loop through them and match the name, but how would I then access that nodes content via the contentservice?
If you add a ServiceContext to your constructor you can use the dependency injection to do something like this:
public class MyClass
{
protected ServiceContext Services { get; }
public MyClass (ServiceContext services)
{
Services = services;
}
public void MyMethod(int contentId)
{
var content = Services.ContentService.GetById(contentId) as IPublishedContent; // cast to IPublishedContent to get the children collection
}
}
Umbraco Content Service GetChildrenByName
I need to access all of the child nodes of a node and get the one with a matching name.
In Umbraco 7, GetChildrenByName() made this very useful, however it seems this is deprecated in umbraco 8.
IContentService contentService = Services.ContentService; var node = contentService.GetChildrenByName(parentNodeId, nodeObject.nodeName).FirstOrDefault();
I assume I could access all of the child nodes of the parent node, loop through them and match the name, but how would I then access that nodes content via the contentservice?
There is no method like GetContent(int id).
How can I do this in umbraco 8?
If you add a ServiceContext to your constructor you can use the dependency injection to do something like this:
is working on a reply...