Tagged: Submit

Sitecore 9 Forms: Access landing page fields in FormSubmitContext?

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.

Happy Sitecoring!

0

Sitecore 9 Forms: Save Data

Are you developing Sitecore 9 Forms and wondering where does the data get saved on Save Data?

  • It goes to local DB called Experience Forms (The DB comes with it as part of Sitecore 9 installation OOTB)
  • It has two tables – dbo.FormEntry and dbo.FieldData holds all the information.
  • The below first query gives the top 1000 form submissions. From the form ID, you can query against dbo.FieldData for any specific field values.

SELECT TOP (1000) [ID]
,[FormItemID]
,[Created]
FROM [sc91local_ExperienceForms].[dbo].[FormEntry]


SELECT TOP (1000) [ID]
,[FormEntryID]
,[FieldItemID]
,[FieldName]
,[Value]
,[ValueType]
FROM [sc91local_ExperienceForms].[dbo].[FieldData]

You can also export the data from ‘Export the data to CSV‘ button from Form Designer.

Hope this information is helpful. Any questions, please leave a comment.

Happy Sitecoring.

0

Sitecore 9 Forms: Redirecting to formbuilder on Submit?

Problem:

Have you submitted a form and it redirected to form builder url and displayed without styles? If so, here is the solution

Resolution:

  • Make sure to add these scripts to MVC OuterLayout.cshtml and reference it in MVCLayout.cshtml. More details, refer the documentation (Steps 3 and 4)
@using Sitecore.ExperienceForms.Mvc.Html  

@Html.RenderFormStyles()
@Html.RenderFormScripts() 

Any questions, please leave a comment. Happy sitecoring!

0

Sitecore 9 Forms: Custom Submit Action

One of most common scenario I came across in Sitecore 9 Form is to create your custom submit action. You can achieve this in three simple steps.

Step 1: Create submit action button in Sitecore

Create the custom action button under /sitecore/system/Settings/Forms/Submit Actions.

Step 2: Create code behind class

Create a class that inherits SubmitActionBase class and override the Execute method.

public class CustomSubmitAction : SubmitActionBase<string>
{
public CustomSubmitAction(ISubmitActionData submitActionData) : base(submitActionData)
{
}

public override void ExecuteAction(FormSubmitContext formSubmitContext, string parameters)
{
Assert.ArgumentNotNull((object)formSubmitContext, nameof(formSubmitContext));

if (this.TryParse(parameters, out string target))
{
try
{
if (this.Execute(target, formSubmitContext))
return;
}
catch (ArgumentNullException ex)
{
}
}
formSubmitContext.Errors.Add(new FormActionError()
{
ErrorMessage = this.SubmitActionData.ErrorMessage
});
}

protected override bool Execute(string data, FormSubmitContext formSubmitContext)
{
Assert.ArgumentNotNull(data, nameof(data));
Assert.ArgumentNotNull(formSubmitContext, nameof(formSubmitContext));

//Prepare model here
var model = new EmailModel()
{
Email = GetValue(formSubmitContext.Fields.FirstOrDefault(f => f.Name.Equals("EmailAddress")))
};

// Call any API service call

return true;
}

private static string GetValue(object field)
{
return field?.GetType().GetProperty("Value")?.GetValue(field, null)?.ToString() ?? string.Empty;
}

protected override bool TryParse(string value, out string target)
{
target = string.Empty;
return true;
}

Step 3: Set the custom submit action fields

Update the ModelType and Error Message on the button shown below.

Bind the custom submit action to Submit button like below.

Publish the button. The submit button on form should now trigger the custom save action! Any questions, let me know.

Happy sitecoring.

2