Try our conversational search powered by Generative AI!

Create XForm fields programmatically

Vote:
 

I have a situation where I would like to create an EPiServer page programmatically, and this page should have a form with different types of input fields. The type of input fields (radio button, checklist, textfield etc) will come from another application, along with the input title and any selection values for the question. 

As an example, one page should be created with an "attend: yes/no" radio button, while another page (using the same page type) should be created with a "comment" input text field. 

Is it possible to create an XForm property on the page programmatically? I've seen many examples of using XForm to store data (answers), but not the actual input fields (questions). We are using EPiServer 7 with Web Forms.

The alterative as far as I can see is to store the input type, title and selection values in my own db table, and then construct the form from those values. Ideally I would use XForm, so that an editor could edit the questions afterwards if needed.

#72967
Jul 03, 2013 10:53
Vote:
 

I don't know If I've understood exactly what you want to accomplish but as far as I think I've got it:

First of all, when you have an XForm Property on a Page you need to select a specific XForm on that Page. I think you can accomplish this by adding some logic on overriding the CreateDefaultValues method.
Though I don't think creating a new XForm for each page would be a good idea. Maybe if you could use a generic XForm for all these pages?

Another problem would be how your code should know whether the page should be created with "attend" or "comment" since they are the same Page Type.

Either way, if you only want to store things in the XForm, you don't need to create the fields in the XForm to be able to store it.

Say you have an instance of XForm in the variable form and the page that holds your values in the variable pageData.

All you need to do is:

XFormData data = form.CreateFormData();
data.PageGuid = pageData.PageGuid;
data.SetValue("MyCustomFieldName", "MyValueAsString");
data.ChannelOptions = ChannelOptions.Database;
data.Send();

    

#72986
Jul 03, 2013 13:55
Vote:
 

Hi Alf,

Thanks for your help. To clarify I will list what I have done and what I try to achieve. It is basically just to do programatically what an Editor will do when creating a new form, and putting this form on a page with an XForm property.

What I have done is:

1) Created a page type, RegistrationPage, which has an XForm property

2) Created a new page, NewYearsEve, which is of type RegistrationPage

What I want to continue to do programmatically is:

3) Create a new form with some form fields, e.g. an "attending: yes/no" checkbox.
    I know an editor can create such a form from the UI in edit mode, but I would like to do the same from code (if possible).

4) Connect the form created in step 3 with the page created in step 2.
    This is the same as going to the created page in the UI, and then click on the "select form" for the XForm property

I will take a look at your suggestion of overriding the CreateDefaultValues method, and see if this can help.

-----
Update:

Got it to work by setting the XForm.Document directly in the code where I created the page.

 

#73022
Edited, Jul 04, 2013 12:35
Vote:
 

Hey Olav, I know it has passed some time, but do you maybe have the code that you used for this problem?

I really don't know how to programatically add select and submit to the Xform.Document, on which way?

Thanks in advance :)

#114206
Edited, Dec 05, 2014 16:44
Vote:
 

Hi,

I wonder if you can you please send me the code that you did to resolve this issue will be highly appreciated?

My email id is  "emmathomasmcad@outlook.com"


Thanks,
Emma

#115815
Jan 19, 2015 15:28
This thread is locked and should be used for reference only. Please use the Episerver CMS 7 and earlier versions forum to open new discussions.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.