Copied to clipboard

Flag this post as spam?

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


  • Andy 13 posts 54 karma points
    Apr 08, 2020 @ 17:07
    Andy
    0

    How to get all pages by document type using Umbraco API and spit out data into Json?

    Hi all,

    So as the title suggests I have created a document type called 'clients' with a few basic properties, some strings called 'clientname' and 'clientage'.

    I've built a controller using the umbracoApi:

    [System.Web.Http.Route("api/{controller}")]
    public class ApiController : UmbracoApiController
    {
        [System.Web.Http.HttpGet]
        public string GetAllPages()
        {
            var content = Umbraco.ContentAtRoot().FirstOrDefault().DescendantsOfType("clients");
    
            var stuff = JsonConvert.SerializeObject(content);
    
            return stuff;
        }
    
    }
    

    but when I try to call the endpoint in my postman I get this error:

    "Self referencing loop detected for property 'ContentType' with type 'Umbraco.Core.Models.PublishedContent.PublishedContentType'. Path '[0].ContentType.PropertyTypes[0]'."

    aside from the json output, am I getting the pages correctly via the api?

    Grateful for any help on the matter!

    Andy

  • Malthe Petersen 68 posts 383 karma points c-trib
    Apr 08, 2020 @ 17:48
    Malthe Petersen
    0

    Hi Andy.

    Instead of returning the serialized string, you should be able to return IEnumerable<IPublishedContent>.

    Not sure whether you would get the same error. But it is worth the try. :-)

  • Andy 13 posts 54 karma points
    Apr 08, 2020 @ 18:20
    Andy
    0

    Hi! Thanks for your answer.

    I changed it to

        public IEnumerable<IPublishedContent> GetAllPages()
        {
            var content = Umbraco.ContentAtRoot().FirstOrDefault().DescendantsOfType("clients");
    
    
            return content;
        }
    

    however I am not getting this error:

    The 'ObjectContent`1' type failed to serialize the response body for content type 'application/json; charset=utf-8

    it needs to be serialized into json as I was doing in my initial post, probably not doing it right but unsure where I am going wrong!

    Andy

  • Malthe Petersen 68 posts 383 karma points c-trib
    Apr 08, 2020 @ 19:51
    Malthe Petersen
    0

    Hmm..

    What about the settings they suggest in answer 1: https://stackoverflow.com/questions/13510204/json-net-self-referencing-loop-detected

    Does that help? :-)

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies