Kentico Form Data in Json Object

Yowaraj Chhetri asked on October 15, 2020 04:49

Hi all,

I have a FormEventHandlerModule which tries to get form data when new record is added. I refered this link : https://docs.xperience.io/k12sp/custom-development/working-with-form-data-using-the-api

However, what I am looking is instead of getting form data as one by one using formDataItem.GetStringValue("").

Is there a way to get form data in json straight away instead of binding them. This is because the form field might change as and when required. I want in whole data in json and dont want to modle bind the fields or get get field values one by one.

Thank you in advance

Correct Answer

Liam Goldfinch answered on October 30, 2020 01:53

Hi Yowaraj,

You could try using the ToJSON extension method, like this:

using CMS;
using CMS.DataEngine;
using CMS.OnlineForms;
using DancingGoat.GlobalEvents;

[assembly: RegisterModule(typeof(FormGlobalEventsModule))]
namespace DancingGoat.GlobalEvents
{
    public class FormGlobalEventsModule : Module
    {
        public FormGlobalEventsModule() : base("DancingGoat.FormGlobalEventsModule")
        {
        }

        protected override void OnInit()
        {
            base.OnInit();

            // Register custom events
            BizFormItemEvents.Insert.Before += Insert_Before;
        }

        private void Insert_Before(object sender, BizFormItemEventArgs e)
        {
            var item = e.Item;

            // Sanity check to ensure we actually have a bizform item.
            if (item == null)
            {
                return;
            }

            var json = item.ToJSON("data", false);
        }
    }
}

This would produce JSON for an example form on Dancing Goat like this:

{
    "data": {
        "FormInserted": null,
        "FormUpdated": null,
        "UserEmail": "liam@test.com",
        "UserMessage": "Test",
        "DancingGoatMvcContactUsNew_1ID": null,
        "UserLastName": "GOLDFINCH",
        "UserFirstName": "LIAM"
    }
}
1 votesVote for this answer Unmark Correct answer

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