Copied to clipboard

Flag this post as spam?

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


  • Mubeen 15 posts 126 karma points
    Dec 30, 2020 @ 15:39
    Mubeen
    0

    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.

    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?

  • Alex Skrypnyk 6132 posts 23951 karma points MVP 7x admin c-trib
    Dec 30, 2020 @ 22:00
    Alex Skrypnyk
    1

    Hi Mubeen

    I think you are getting childNodes in the wrong format.

    Try to use IEnumerable

    var ChildNodes = root.GetValue<IEnumerable<IPublishedContent>>("linkedElements");
    

    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

  • Mubeen 15 posts 126 karma points
    Dec 31, 2020 @ 11:29
    Mubeen
    0

    Hi Alex!

    Thank you for your response. I tried to use IEnumerable the way you suggested. But i am getting null in childNodes. enter image description here 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?

  • Alex Skrypnyk 6132 posts 23951 karma points MVP 7x admin c-trib
    Dec 31, 2020 @ 14:23
    Alex Skrypnyk
    0

    Are you using umbraco 7? Can you show me your backend? how does the nested content look like?

  • Mubeen 15 posts 126 karma points
    Dec 31, 2020 @ 16:54
    Mubeen
    0

    I am using the Umbraco version 7.15.5. Following is the property i am trying to access. enter image description here

    And this is the property definition in docType enter image description here

    And the DocType i am using in the Nested Content Property is following enter image description here

  • Mubeen 15 posts 126 karma points
    Jan 04, 2021 @ 17:39
    Mubeen
    101

    I solved it using the IPublishedContent instead of IContent. Now my code looks something like this.

    private void Generate(IPublishedContent root, List<Node> nodes, List<Link> links, int levels = 1, int nodeId = 1)
            {
                var parentId = nodeId;
                var lev = 1;
                if (levels > 1)
                    lev = levels;
                else
                    lev = Levels + 1;
                nodes.Add(new Node() 
                { 
                    id = nodeId,
                    label = splitWords(root.Name)
                });
                if (root.HasProperty("linkedElements"))
                {
                    var ChildNodes = root.GetPropertyValue<IEnumerable<IPublishedContent>>("linkedElements");
                    foreach (var child in ChildNodes)
                    {
                        nodeId += 1;
                        links.Add(new Link()
                        {
                            from = parentId,
                            to = nodeId,
                            label = child.GetPropertyValue<string>("relationType")
                        });
                        Generate(child, nodes, links, lev, nodeId);
                    }
                }
    
                Levels = lev;
            }
    
Please Sign in or register to post replies

Write your reply to:

Draft