Kentico Web Api - Self referencing loop

Gideon Gontor asked on February 5, 2019 18:42

I am getting the following error in my custom web api

Newtonsoft.Json.JsonSerializationException: 'Self referencing loop detected for property 'ProviderObject' with type 'CMS.DataEngine.DataClassInfoProvider'. Path '[0].DataClassInfo.TypeInfo.ProviderObject.TypeInfo'.'

This is my code

  string customTableClassName = "customtable.countries";

    public HttpResponseMessage Get()
        // Gets the custom table
        //CMS.DataEngine.DataClassInfo customTable = CMS.DataEngine.DataClassInfoProvider.GetDataClassInfo(customTableClassName);
        // Gets a custom table record with a specific ID (25)
       var item = CustomTableItemProvider.GetItems(customTableClassName).ToList();
        //CustomTableItem item = CustomTableItemProvider.GetItems(23,customTableClassName);
            // An unhandled exception of type 'Newtonsoft.Json.JsonSerializationException' occurred in Newtonsoft.Json.dll
            // Additional information: Self referencing loop detected with type 'JSONPlayground.Author'.Path 'FavouriteAuthors'.
        catch (JsonSerializationException e)

        string countrylist = JsonConvert.SerializeObject(item, new JsonSerializerSettings()
            PreserveReferencesHandling = PreserveReferencesHandling.Objects,
            Formatting = Formatting.Indented
        // You can return a variety of things in Web API controller actions. For more details, see
        return Request.CreateResponse(HttpStatusCode.OK, countrylist);

Recent Answers

Dragoljub Ilic answered on February 6, 2019 14:06

This does not seems to be Kentico related issue. You should add additional serializer settings to handle this. Check this post on stackoverflow. It may give you a good point to start.

0 votesVote for this answer Mark as a Correct answer

Gideon Gontor answered on February 6, 2019 14:14

Thanks Dragoljub, for responding to my post.

Actually I discovered the main error wasn't the looping issue it has to do with the following

IsObjectValid = '(new System.Collections.Generic.Mscorlib_CollectionDebugView<CMS.CustomTables.CustomTableItem>(item).Items[0]).IsObjectValid' threw an exception of type 'System.Exception'

I have been advised by the support Team that i have been trying to serialize Kentico objects directly, which is not possible, because the object reference many other objects. That I would first need to create your own custom model (class) with only the properties you need, fill it with data from the custom table item, then serialize your custom class.

0 votesVote for this answer Mark as a Correct answer

Roman Hutnyk answered on February 7, 2019 17:20

Ideally you need to configure JSON serializer to skip such type of references.

But there is quick and easy workaround: * define DTO - simple class that has all fields/properties you'd like your end point to return * convert/map object you are currently trying to serialize to newly created DTO * let your end point to return DTO instead of Kentico native object

Hope that helps

0 votesVote for this answer Mark as a Correct answer

Gideon Gontor answered on February 7, 2019 18:27

Hi Roman Thanks, for your suggestion, if you have a sample that would be great.

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.