3

I'm trying to get the form data and store it in text file. I'm not getting the form data in the code.

using System;
using System.IO;
using System.Linq;
using Sitecore.Diagnostics;
using Sitecore.ExperienceForms.Models;
using Sitecore.ExperienceForms.Processing;
using Sitecore.ExperienceForms.Processing.Actions;
using static System.FormattableString;
using Newtonsoft.Json;
using System.Collections.Generic;

namespace Sitecore.ExperienceForms.Samples.SubmitActions
{
    /// <summary>
    /// Executes a submit action for logging the form submit status.
    /// </summary>
    /// <seealso cref="Sitecore.ExperienceForms.Processing.Actions.SubmitActionBase{TParametersData}" />
    public class LogSubmit : SubmitActionBase<string>
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="LogSubmit"/> class.
        /// </summary>
        /// <param name="submitActionData">The submit action data.</param>
        public LogSubmit(ISubmitActionData submitActionData) : base(submitActionData)
        {
        }

        /// <summary>
        /// Tries to convert the specified <paramref name="value" /> to an instance of the specified target type.
        /// </summary>
        /// <param name="value">The value.</param>
        /// <param name="target">The target object.</param>
        /// <returns>
        /// true if <paramref name="value" /> was converted successfully; otherwise, false.
        /// </returns>
        protected override bool TryParse(string value, out string target)
        {
            target = string.Empty;
            return true;
        }

        /// <summary>
        /// Executes the action with the specified <paramref name="data" />.
        /// </summary>
        /// <param name="data">The data.</param>
        /// <param name="formSubmitContext">The form submit context.</param>
        /// <returns>
        ///   <c>true</c> if the action is executed correctly; otherwise <c>false</c>
        /// </returns>
        protected override bool Execute(string data, FormSubmitContext formSubmitContext)
        {
            createFile(data, formSubmitContext);

            Assert.ArgumentNotNull(formSubmitContext, nameof(formSubmitContext));

            if (!formSubmitContext.HasErrors)
            {
                Logger.Info(Invariant($"Form {formSubmitContext.FormId} submitted successfully."), this);
            }
            else
            {
                Logger.Warn(Invariant($"Form {formSubmitContext.FormId} submitted with errors: {string.Join(", ", formSubmitContext.Errors.Select(t => t.ErrorMessage))}."), this);
            }

            return true;
        }

        private static IViewModel GetFieldById(Guid id, IList<IViewModel> fields)
        {
            return fields.FirstOrDefault(f => Guid.Parse(f.ItemId) == id);
        }

        public void createFile(string data, FormSubmitContext formSubmitContext)
        {
            string path = @"C:\inetpub\wwwroot\sc-health-grades.sc\SCTest.txt";
            if (!File.Exists(path))
            {
                // Create a file to write to.
                using (StreamWriter sw = File.CreateText(path))
                {
                    sw.WriteLine("Hello");
                    sw.WriteLine("And");
                    sw.WriteLine("Welcome");
                    sw.WriteLine(data);
                    sw.WriteLine(data);
                    sw.WriteLine(formSubmitContext.FormId);
                    sw.WriteLine(formSubmitContext.ButtonId);
                    var emailid = 
                    /// var jsonString = JsonConvert.SerializeObject(formSubmitContext.Fields.SingleOrDefault());
                    /// sw.WriteLine(jsonString);
                    sw.WriteLine("Has errors: " + formSubmitContext.HasErrors);

                }
            }

            // Open the file to read from.
            using (StreamReader sr = File.OpenText(path))
            {
                string s = "";
                while ((s = sr.ReadLine()) != null)
                {
                    Console.WriteLine(s);
                }
            }
        }
    }
}

can someone help me on what I'm doing wrong here ?

4

The "data" string you are referring to holds the configuration of your submit action. This does not contain the content of the form.

On your submit action sitecore item, the config is stored as json in the parameters field. This json is deserialised into a class you need to create that is passed to your custom submitaction in the "data" parameter. So this "data" you are referring to, is actualy just configuration. enter image description here

The content of the form can be found in the formSubmitContext.

Have a look at the sitecore documentation on how to build custom submit action. https://doc.sitecore.com/developers/91/sitecore-experience-management/en/walkthrough--creating-a-custom-submit-action.html

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.