I came across a scenario, where I need to update the email field for all custom forms built with ‘EmailTemplate’. Since it’s going to be a bulk update, decided to do it in SPE(so powerful!)
Are you looking for a generic method to read the values from any type of Sitecore Form fields? Here is the solution.
private static Dictionary<Guid, string> FormFieldsToDictionary(IList<IViewModel> fields)
{
Dictionary<Guid, string> fielDictionary = new Dictionary<Guid, string>();
foreach (var field in fields)
{
fielDictionary.Add(Guid.Parse(field.ItemId), field.GetType().GetProperty("Value")?.GetValue(field, null)?.ToString() ?? string.Empty);
}
return fielDictionary;
}
private static string GetValue(IViewModel field)
{
if (field == null)
{ return default(string); }
if ((field as StringInputViewModel) != null)
{
return (string)(object)(field as StringInputViewModel).Value;
}
if (field is ListViewModel)
{
var listField = (ListViewModel)field;
var array = listField?.Value?.ToArray();
if (array == null)
{
return string.Empty;
}
return String.Join(",", array);
}
if (field is DateViewModel)
{
var dateField = (DateViewModel)field;
return dateField.Value.HasValue ? dateField.Value.Value.ToShortDateString() : string.Empty;
}
if (field is NumberViewModel)
{
var numberField = (NumberViewModel)field;
return numberField.Value.HasValue ? numberField.Value.ToString() : string.Empty;
}
if (field is TextViewModel)
{
var textField = (TextViewModel)field;
return (string)(object)textField.Text;
}
if (field is CheckBoxViewModel)
{
var checkbox = (CheckBoxListViewModel)field;
return (string)(object)checkbox.Value;
}
return default(string);
}
Utilization:
protected override bool Execute(UpdateContactData data, FormSubmitContext formSubmitContext)
{
var fieldsDictionary = FormFieldsToDictionary(formSubmitContext.Fields);
// From here you can create a model from fieldsDictionary, call any API and return call status
}
Hope this helps someone. Any questions, leave a comment.
I deployed the forms to Staging environment and see how the forms were performing.
I exported the data using Export form data into CSV button and i received ‘The date range is invalid. Please select a date range that is within the range of the list.‘ error.
I tried different range and it threw the same error. I checked the db([project]. ExperienceForms) and there was NO DATA!
I submitted the form and tried again. It downloaded the CSV file with an entry on it.
I came across a scenario where I needed to access landing page fields(the page where we add Forms) in Custom Submit Action’s FormSubmitContext.
By default, the page item isn’t known at the Submit Action. So started thinking should i create a custom hidden field or use the Forms Extensions module? At the end of the day, i didn’t need either.
Here is the quick way to access the current page using HttpContext’s AbsolutePath.
protected override bool Execute(string data, FormSubmitContext formSubmitContext)
{
var contextItem = GetContextItem.GetItem(HttpContext.Current.Request.UrlReferrer?.AbsolutePath) as ILandingPageContentItem;
// Access fields from ContextItem
}
public class GetContextItem
{
public static IStandardTemplateItem GetItem(string path)
{
Item item = Sitecore.Context.Database.GetItem($"/sitecore/content/<tenant>/Home{path}");
return item?.AsStronglyTyped();
}
}
Hope this helps someone. Any questions, please leave a comment.
I came across a scenario to implement Conditional Section for Sitecore 9.0 to hide/show fields based on user input. This feature was introduced on Sitecore 9.1(Checkout my other blog here). Since the project is in Sitecore 9.0, I decided to create a custom control using speak.
Let’s get started.
Step 1: Create form element in core DB using speak
Switch to Core DB
Go to /sitecore/client/Applications/FormsBuilder/Components/Layouts/PropertyGridForm/PageSettings/Settings
Create a template based of Form Parameters. (I couldn’t find it when i tried from Insert Template, so i duplicated the existed one- MutliLine Text. If you know, how to add Form Parameters template(not using Sitecore Rocks), please leave a comment.)
Add the FormTextBox Parameters template. Since I duplicated MultiLineText field, it came with the Details, Validation, Styling and Advanced. I feel it’s best shortcut to create quick.
Fill out FormLabel, IsLableOnTop and BindingConfiguration fields.
Repeat the fields as many as you need. Here i added one more to compare the value.
NOTE: IsLabelOnTop is unchecked for additional fields
Step 2: Create form template in Master DB
On Master DB, create a custom template under Basic/Lists/Security/Structure folders based on Field Type template(/sitecore/templates/System/Forms/Field Type). I created under Structure Section as it’s Condition Section.
Fill out Property Editor field by choosing the custom control that was created in Core DB. You can see all the fields listed shown in below screen shot.
Fields(View Path, Model Type)will be filled out after creating code behind and razor view files.
Step 3: Create model and view in Visual Studio
Create model and view in project under Helix structure
Now you should see the new form control on the elements panel. Drag and Drop to any form and fill out the Target Field and True Value fields appropriately and put any element(s) inside Conditional Section to show/hide the element(s).
Don’t forget to publish all the templates and forms!
Happy Sitecoring! Leave a comment if you have any questions.
I came across a requirement from a client that they wanted to show Country and it’s states grouped in a dropownlist. I created a custom grouped dropdown to fit into the scenario. Since this is an extension of dropdownlist, no speak knowledge required.
Let’s get started
Step 1: Create custom dropdown list
Create a custom dropdownlist under /sitecore/system/Settings/Forms/Field Types/Lists based on Field Type(/sitecore/templates/System/Forms/Field Type) template like below
Step 2: Create a code behind class and razor view
create a code behind class like this in VS project
using System.Collections.Generic;
using Sitecore.Data.Items;
using Sitecore.Diagnostics;
using Sitecore.ExperienceForms.Mvc.Models.Fields;
namespace Sitecore.Project.Example.Views.StateDropdown
{
public class StateDropdown : DropDownListViewModel
{
protected override void InitItemProperties(Item item)
{
Assert.ArgumentNotNull(item, nameof(item));
base.InitItemProperties(item);
}
protected override void UpdateItemFields(Item item)
{
Assert.ArgumentNotNull(item, nameof(item));
base.UpdateItemFields(item);
}
public Dictionary<string, Dictionary<string, string>> Regions()
{
if(string.IsNullOrEmpty(DataSource)) return new Dictionary<string, Dictionary<string, string>>();
Dictionary<string, Dictionary<string, string>> counrtyCollection = new Dictionary<string, Dictionary<string, string>>();
Item item = Context.Database.GetItem(DataSource);
var children = item.GetChildren();
foreach (Item child in children)
{
counrtyCollection.Add(child.Fields["Name"].Value, new Dictionary<string, string>());
foreach (Item desc in child.Children)
{
counrtyCollection[child.Fields["Name"].Value].Add(desc.Fields["Name"].Value, desc.Fields["Code"].Value);
}
}
return counrtyCollection;
}
}
}
Create a razor view like this and build project
@using Sitecore.ExperienceForms.Mvc.Html
@model Sitecore.Project.Example.Views.StateDropdown.StateDropdown
@{
var regions = Model.Regions();
}
<label for="@Html.IdFor(m => Model.Value)" class="@Model.LabelCssClass">@Html.DisplayTextFor(t => Model.Title)</label>
<select id="@Html.IdFor(m => Model.Value)" data-trigger name="@Html.NameFor(m => Model.Value)" class="@Model.CssClass" placeholder="Select Your Work Location" data-sc-tracking="@Model.IsTrackingEnabled" data-sc-field-name="@Model.Name" @Html.GenerateUnobtrusiveValidationAttributes(m => m.Value)>
<option placeholder>Select Your Work Location</option>
@foreach (var region in regions)
{
<optGroup data-id="@region.Value" label="@region.Key">
@foreach (var state in region.Value)
{
<option value="@state.Value">@state.Key</option>
}
</optGroup>
}
</select>
@Html.ValidationMessageFor(m => Model.Value)
Step 3: Fill out the fields
Fill out the View Path, Model Type and Property Editor(should be Property Editor Settings/DropDown List).
you are now all set to utilize the custom dropdown list in Forms Designer. Don’t forget to publish the field!
Hope this is helpful. Any questions, please leave a comment.
I came across a scenario where i implemented a custom validation on Sitecore 9 Forms. Sharing my knowledge here. You can achieve this in three simple steps.
Step 1: Create custom validator
Create a custom validator under /sitecore/system/settings/forms/validation based on Validation(sitecore/templates/System/Forms/Validation) template like below
Step 2: Fill out the fields
Fill out your own regex and error message. You can copy the Type value from other validator.
Step 3: Assign the custom validator
You can now add the new custom validator to any Form fields( /sitecore/system/Settings/Forms/Field Types). All set and no code required!
Hope you found this blog helpful. Any questions, please leave a comment.