User ID as Hidden Field of Custom Form

Posted by Community Admin on 04-Aug-2018 21:01

User ID as Hidden Field of Custom Form

All Replies

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

What is the best way once a visitor has logged on to the web site to have them fill out a custom form but associate that response with that user? I'd like to have users fill out surveys once they log on the my web site. 

Thanks,
Matt

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

Hello Matt,

This can be done with the forms control. You may need to modify the log in widget(you can get its template from Sitefinity SDK) to redirect to a page where the survey is placed. By default Forms get the user ID, but not the user name. You can follow this forum thread if you want to search users by the ID and apply this to suit your needs. Also when you submit a response this created FormEntry object which has a property - UserId. This is the ID of the user submitted the form.  To get the response of a form you can use
public IEnumerable GetFormEntries(Type entryType, string filter, string orderBy, int skip, int take, ref int? count)

Below is a sample code that shows getting form results filtered by user ID

var formsManager = FormsManager.GetManager();
string entryTypeName = String.Format("0.1", "Telerik.Sitefinity.DynamicTypes.Model", "sf_yourformname");
var itemType = formsManager.GetEntryType(entryTypeName);
  
var userID = SecurityManager.GetCurrentUserId();
var fakeID = new Guid("4B15FA4B-6C36-4CE5-A56D-9FDFB6E541DF");
var filter = "UserId ==" + fakeID + "";
  
  
int? totalCount = 0;
IEnumerable items = null;
try
    items = formsManager.GetFormEntries(itemType, filter, "Title ASC", 0, 100, ref totalCount);
  
catch
  
When you get the ID you can use more logic to list the users or populate a widget with their names corresponding to the ID`s.

To take the username when submitting the form. You will have to inherit FormsControl and create a textfield that will be hidden from the user filling the form. When the form is submitted OnBeforeFormSave is called which walks trough all form fields and then save their values to be sent to the forms module. When OnBeforeFormSave is executed you must fill the value of the hidden textfield with the username by using GetUserDisplayName which gets the username by the supplied ID.

All the best,
Stanislav Velikov
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