Question

I am working in Ektron 8.02.

I am trying to fetch the data associated with an Ektron "HTML Form" in workarea. I need to get the Form field Names and their defualt value using API. I tried using the Ektron.Cms.API.Content.Form.GetFormFieldList API. But i am unable to get the Default value associated with a Form Field. Is there is any other API that provides this data? Can someone provide me some insight on this?

Was it helpful?

Solution

I just answered a similar question - FormBlock Server Control in Ektron

There's no way (that I could find) to get what you need using the Ektron API. For whatever reason, the API doesn't give you the default values. The HTML of the form, however, does contain the default values. You can get the HTML from the FormBlock server control using the EkItem.Html property, or you can use the ContentAPI. My first thought was to use the FormAPI and get the FormData object, but strangely enough, the FormData comes back with an empty Html property. So to do this with only API calls, you need to instantiate two classes: Ektron.Cms.API.Content.Content for the HTML, and Ektron.Cms.API.Content.Form for the list of form fields.

So, to make a long story short, here's some code that will give you a dictionary where the key is the name of the field and the value is the field's default value.

private Dictionary<string, string> GetFormFieldDefaults(long formId)
{
    var defaults = new Dictionary<string, string>();
    var formApi = new Ektron.Cms.API.Content.Form();
    var contentApi = new Ektron.Cms.API.Content.Content();

    var formFields = formApi.GetFormFieldList(formId);
    var formData = formApi.GetForm(formId); // Can't use FormData; have to use ContentAPI / ContentData to get the HTML
    //if (string.IsNullOrEmpty(formData.Html)) throw new Exception("FormData with empty HTML. Eeek!");

    var contentData = contentApi.GetContent(formId);
    var formXml = string.Concat("<ekForm>", contentData.Html, "</ekForm>");
    var ekForm = XElement.Parse(formXml);
    var inputs = ekForm.Descendants("input");
    foreach (var fieldDefinition in formFields.Fields)
    {
        var name = fieldDefinition.FieldName;
        var input = inputs.FirstOrDefault(i => i.Attribute("id").Value == name);
        if (input == null) continue;

        var defaultValue = input.Attribute("value").Value;
        defaults.Add(name, defaultValue);
    }
    return defaults;
}

This code is only looking for <input /> fields, so there might be some extra work required if you want the default value for a dropdown list.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top