I am trying to get a content node based on the key i am getting from a property. But i am unable to find the Content node for that key. Root has a nested content property which have the content keys in it when i get the value in the controller. I wan to use that key to get the actual nested content node.
private void Generate(IContent root, List<Node> nodes, List<Link> links, int levels, int nodeId = 0)
{
nodeId += 1;
levels += 1;
nodes.Add(new Node()
{
id = nodeId,
label = root.Name
});
var ChildNodesStr = root.GetValue<string>("linkedElements");
var ChildNodes = ChildNodesStr == null ? new List<RootObject>() : JsonConvert.DeserializeObject<List<RootObject>>(ChildNodesStr);
foreach (var child in ChildNodes)
{
nodeId += 1;
nodes.Add(new Node()
{
id = nodeId,
label = child.name
});
IContentService contentService = Services.ContentService;
var contentId = new Guid(child.key);//Key is of the following format, key = 7182e27e-860d-4b9b-9060-5acd3d7b00c9
var node = contentService.GetById(contentId);// node is null and no content is found
Generate(node, nodes, links, levels, nodeId);
}
}
public class RootObject
{
public string key { get; set; }
public string name { get; set; }
public int relationType { get; set; }
}
Can anyone please point me to the right direction?
Thank you for your response. I tried to use IEnumerable the way you suggested. But i am getting null in childNodes.
Does it have to do with something that the property i am trying to get linkedElements is a Nested Content property which in turn is using a custom DocType?
Unable to get Content by ID in Controller
I am trying to get a content node based on the key i am getting from a property. But i am unable to find the Content node for that key. Root has a nested content property which have the content keys in it when i get the value in the controller. I wan to use that key to get the actual nested content node.
Can anyone please point me to the right direction?
Hi Mubeen
I think you are getting childNodes in the wrong format.
Try to use IEnumerable
And you don't need to use Content service to get each child node, just loop through ChildNodes and get all needed fields.
Thanks, Alex
Hi Alex!
Thank you for your response. I tried to use IEnumerable the way you suggested. But i am getting null in
childNodes
. Does it have to do with something that the property i am trying to getlinkedElements
is a Nested Content property which in turn is using a custom DocType?Are you using umbraco 7? Can you show me your backend? how does the nested content look like?
I am using the Umbraco version 7.15.5. Following is the property i am trying to access.
And this is the property definition in docType
And the DocType i am using in the Nested Content Property is following
I solved it using the
IPublishedContent
instead ofIContent
. Now my code looks something like this.is working on a reply...