Save/Update a Field property programmatically

Fabio Xodo asked on August 8, 2018 13:42

Hi all,

I created a Custom Scheduled Task that connects to an API and gathers some data, periodically, among which the Caption and ValidationErrorMessage for given fields of a form. Now, ther reasons I must do it are a lot, I don't want to get in details. That said, I tried to find documentation on how to save a property programmatically and I'm still not able to.

Currently, the core part of my code that should update a field given his formfieldname looks like this:

private void SetFormFieldText(string formfieldname, string caption_text, string validation_text)
    {
        BizFormInfo formObject = BizFormInfoProvider.GetBizFormInfo("MyCustomForm", SiteContext.CurrentSiteID);
        DataClassInfo dci = DataClassInfoProvider.GetDataClassInfo(formObject.FormClassID);

        if (dci != null)
        {                              
            FormInfo fi = new FormInfo(dci.ClassFormDefinition);                
            if(fi != null)
            {                
                FormFieldInfo  ffi = fi.GetFormField(formfieldname);                    
                if (ffi!=null)
                {                        
                    ffi.SetPropertyValue(FormFieldPropertyEnum.FieldCaption,caption_text);                                    ffi.SetPropertyValue(FormFieldPropertyEnum.ValidationErrorMessage,validation_text);                                                                       
                    fi.UpdateExistingFields(fi); //saving?
                    //fi.UpdateFormField(formfieldname,ffi); //tryed this, too
                }                 
            }         
        }   
    }

but neither using UpdateExistingFields or UpdateFormField (that I'm sure are reached and executed) I can trig the saving of those set properties. Using SQL Profiler, I can't see any update on the DB at all.

What's the right way to save changes made on a custom form using SetPropertyValue on a field, programmatically?

I'm using Kentico 9.

Thanks, Fabio

Correct Answer

Anton Grekhovodov answered on August 8, 2018 14:40

Hi,
UpdateExistingFields and UpdateFormField change only form object values in memory and do not modify record in DB. Try to save DataClassInfo object:

fi.UpdateExistingFields(fi);
...
dci.ClassFormDefinition = fi.GetXmlDefinition();
DataClassInfoProvider.SetDataClassInfo(dci); // or dci.Update()
1 votesVote for this answer Unmark Correct answer

Recent Answers


Arun Kumar answered on August 8, 2018 14:50 (last edited on August 8, 2018 14:50)

Hi Fabio,

You can follow this which describe how to work with form data using the API.

0 votesVote for this answer Mark as a Correct answer

Fabio Xodo answered on August 8, 2018 15:10

Thanks I thought the same, and I was exactly looking for a way to persist it! It works!

Arun, that link explains how to work with the data, not the structure of the form as I asked.

Thank yo all anyway, I solved it!

0 votesVote for this answer Mark as a Correct answer

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