Working with Form Data from Code Behind

Posted by Community Admin on 03-Aug-2018 16:23

Working with Form Data from Code Behind

All Replies

Posted by Community Admin on 29-Dec-2010 00:00

I was attempting to retrieve data from the Forms module in code behind.  I "invented" this technique because I could not see a way to get the data strongly typed. If there is a way to do this strongly typed it would be far superior to using dynamic. Wanted to get your thoughts on this approach (i.e. using the .NET "dynamic" keyword). Please advise. . . . 

01.var formObjects = App.WorkWith().Forms().Form(this.FormName).Entries().Get()
02.    .ToArray()
03.    .Cast<dynamic>()
04.    .Select(x => new Location()
05.    
06.        Email = x.FormTextBox_4,
07.        Phone = x.FormTextBox_0,
08.        Name = x.FormTextBox_1,
09.        Address = x.FormTextBox_2,
10.        LatLong = x.FormTextBox_3
11.    );

On line 1, using the fluent api I get an IQueryable of FormEntry objects. Then I convert it to an Array to execute it against the DB and return an IEnumerable of FormEntry. Then I convert it to dynamic, then project it into my new type (Location is a real class I created, with a bunch of string properties). I was able to figure out the column names (i.e. FormTextBox_XX) by looking at the csv export that sitefinity generates on the "Responses" screen in the back end. 

Is there a strongly typed way to do this? If not what are your thoughts on my use of dynamic to retrieve this data?

Posted by Community Admin on 03-Jan-2011 00:00

Hi Shawn Weisfeld,

Thank you for using our services.

You cannot use strongly typed property names with dynamic fields. What you can do is to set field names for each text box when you add it to your form, see attached image. Then from code you will be able to use this field name to get (GetValue()) or set value (SetValue()):

using Telerik.Sitefinity.Model;
...
var formObjects = App.WorkWith().Forms().Form(this.FormName).Entries().Get()
    .ToArray()
    .Select(x => new Location()
    
        Email = x.GetValue("EmailFieldName"),
        Phone = x.GetValue("PhoneFieldName"),
        Name = x.GetValue("NameFieldName"),
        Address = x.GetValue("AddressFieldName"),
        LatLong = x.GetValue("LatLongFieldName")
    );


Best wishes,
Radoslav Georgiev
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items

Posted by Community Admin on 30-Jun-2011 00:00

Hi,

  How do I get all the form fields dynamically. In this case you know the fields. In my case I donot know the fields. There can be 1 field or 10 fields. How do I get all the fields in a form.

Thanks
Madhavan

Posted by Community Admin on 01-Jul-2011 00:00

Hi Madhavan,

You can use TypeDescriptor.GetProperties Method

Kind regards,
Ivan Dimitrov
the Telerik team

Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items

This thread is closed