Copied to clipboard

Flag this post as spam?

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


  • Saba Tandashvili 29 posts 181 karma points
    Nov 22, 2018 @ 12:20
    Saba Tandashvili
    0

    Umbraco IPublishedContent to json

    Hi, I am trying to return IEnumerable<IPublishedContent> from controller with return Json(allOffers, JsonRequestBehavior.AllowGet); But it seems that Umbraco's IPublishedContent can not be serialized to json. I got error A circular reference was detected while serializing an object of type 'Umbraco.Web.Models.DynamicPublishedContent'. Anyone knows solution ?

  • Marcio Goularte 374 posts 1346 karma points
    Nov 22, 2018 @ 12:27
    Marcio Goularte
    100

    Hi,

    You're probably using some method that returns a dynamic, switch to a typed one. This example link I already used and works.

    https://dejanstojanovic.net/umbraco-cms/2014/august/fastest-way-to-return-json-result-from-a-controller/

  • Saba Tandashvili 29 posts 181 karma points
    Nov 22, 2018 @ 13:08
    Saba Tandashvili
    0

    Hi Marcio.

    I have almost exact code, here it is :

    public class OffersSurfaceController : SurfaceController
    {
        public ActionResult GetFilteredOffers(int ? offersTypeId)
        {
            IEnumerable<IPublishedContent> allOffers = Umbraco.Content(1362).Children();
            return Json(allOffers.Where(x => x.GetPropertyValue<int>("offersItemCategory") == 1353),  JsonRequestBehavior.AllowGet);
        }
    }
    

    And i changed it 100 times, tried everything but i always get same error:

    A circular reference was detected while serializing an object of type 'Umbraco.Web.Models.DynamicPublishedContent'.
    

    The error flashes when something comes from database, when it is empty it works and logs empty array in console(I am using ajax request).

  • Saba Tandashvili 29 posts 181 karma points
    Nov 22, 2018 @ 13:49
    Saba Tandashvili
    0

    I have seen the example u linked mate but id didn't worked for me. After you said that u had already used this example and it works I copied whole example and try to make it work. At last i got perfectly working controller with ajax request which returns json response. Here is code if anyone will need it in future :

    public class OffersSurfaceController : SurfaceController
    {
        public ActionResult GetFilteredOffers(int ? offersTypeId, int ? offersCategoryId)
        {
            IEnumerable<IPublishedContent> allOffers = Umbraco.Content(1362).Children();
            if (offersCategoryId != 0 && offersCategoryId != null)
            {
                allOffers = allOffers.Where(x => x.GetPropertyValue<int>("offersItemCategory") == offersCategoryId);
            }
    
            if (offersTypeId != 0 && offersTypeId != null)
            {
                allOffers = allOffers.Where(x => x.GetPropertyValue<int>("offersItemType") == offersTypeId);
            }
    
            return Json(allOffers.Select(l => new { text = l.GetPropertyValue("offersItemTitle") }),  JsonRequestBehavior.AllowGet);
        }
    }
    

    Notice allOffers.Select(l => new { text = l.GetPropertyValue("offersItemTitle") }) makes code working, ToList(), ToArray() gives error which i mentioned above.

    Thanks to Marcio.

  • louisjrdev 107 posts 344 karma points c-trib
    Nov 22, 2018 @ 13:11
    louisjrdev
    0

    Hi Saba,

    Ensure that this

    IEnumerable<IPublishedContent> allOffers =Umbraco.Content(1362).Children();
    

    is this

    IEnumerable<IPublishedContent> allOffers =Umbraco.TypedContent(1362).Children();
    

    they keyword being Typed, this makes the returned object strongly typed as an IPublishedContent

  • Marcio Goularte 374 posts 1346 karma points
    Nov 22, 2018 @ 16:57
    Marcio Goularte
    2

    I suggests use the examine to kind of filter. I will share similar code i have done some time ago, coming soon

  • Saba Tandashvili 29 posts 181 karma points
    Nov 26, 2018 @ 06:18
    Saba Tandashvili
    1

    I am looking forward for it, thanks in advance :)

Please Sign in or register to post replies

Write your reply to:

Draft