1-888-365-2779
+1-888-365-2779
Try Now
More in this section

Forums / General Discussions / Working with Form Data from Code Behind

Working with Form Data from Code Behind

4 posts, 0 answered
  1. Shawn Weisfeld
    Shawn Weisfeld avatar
    1 posts
    Registered:
    22 Mar 2007
    29 Dec 2010
    Link to this post
    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?
  2. Radoslav Georgiev
    Radoslav Georgiev avatar
    3370 posts
    Registered:
    01 Feb 2016
    03 Jan 2011
    Link to this post
    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
  3. Madhavan
    Madhavan avatar
    19 posts
    Registered:
    31 Jan 2011
    30 Jun 2011
    Link to this post
    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
  4. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    01 Jul 2011
    Link to this post
    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
4 posts, 0 answered