x First time here? Check out the FAQ

Come work for Umbraco - The Umbraco HQ are hiring Project managers, .NET developers and DevOps people!

  • Avatar207posts221karma

    Retrieving form values in a workflow

    Paul Marden started this topic November 29, 2011 @ 04:40

    Quick question.... is the only way to access a contour form value in a workflow to use the [# ] notation and retrieve a request collection value?


  • Avatar275posts396karma
    Comment with ID: 98311
    Seth Niemuth posted this reply November 29, 2011 @ 06:24

    Here is an example of a send to an email that is entered by the user in the form workflow:

    The email service is just a method in our repository which sends off an email using the Xslt file specified in the properties but this shows being able to get the value of a field out in a workflow in XML format. 

     using System;
    using System.Collections.Generic;
    using Umbraco.Forms.Core;
    using Umbraco.Forms.Core.Enums;
    using Umbraco.Forms.Data.Storage;
    using System.Xml;
    using Umbraco.Forms.Core.Attributes;
    using System.Web;
    using QHotels.Services;
    using System.Text.RegularExpressions;
    using System.IO;
     public class SendEmailToUser : WorkflowType
        {
            // Generates the new workflow details for Contour in the workflow dropdown in Contour
            public SendEmailToUser()
            {
                // Need to generate a new guid for the new custom workflow - add your own GUID
                this.Id = new Guid("6E638E92-0F61-493c-A1AE-DED9D3F3333A");
                this.Name = "Send User Email";
                this.Description = "If you want to send an email to a field entered in the form";
            }

     

            [Setting("Email Look Up Field", description = "Enter the receiver email lookup field", control = "Umbraco.Forms.Core.FieldSetting.TextField")]
            public string EmailLookUpField { get; set; }

     

            [Setting("Xslt File", description = "The name of the file within the XSLT folder that will be the email template", control = "Umbraco.Forms.Core.FieldSetting.TextField")]
            public string XsltFile { get; set; }

     

            [Setting("Email Subject", description = "Subject of the email", control = "Umbraco.Forms.Core.FieldSetting.TextField")]
            public string EmailSubject { get; set; }

     

            private const string LookUpFieldXpath = "//fields/child::* [caption =  \"{0}\"]";

      

            public override List<Exception> ValidateSettings()
            {
                List<Exception> exceptions = new List<Exception>();
                if (string.IsNullOrEmpty(EmailLookUpField))
                    exceptions.Add(new Exception("'Email Look up field' setting not filled out"));

     

                if (string.IsNullOrEmpty(EmailSubject))
                    exceptions.Add(new Exception("'Email subject field' setting not filled out"));

     

                if (string.IsNullOrEmpty(XsltFile))
                    exceptions.Add(new Exception("'Xslt File' setting not filled out"));
                else
                {
                    try
                    {

     

                        string mappedXsltPath = HttpContext.Current.Server.MapPath("/xslt" + XsltFile);
                        if (!File.Exists(mappedXsltPath))
                        {
                            exceptions.Add(
                                new Exception("'Xslt File' is not a valid file within the xslt folder of the website"));
                        }

     

                    }
                    catch (Exception)
                    {
                        exceptions.Add(new Exception("'Xslt File' is not a valid file within the xslt folder of the website"));

     

                    }
                }

     

                return exceptions;
            }

       

            public override Umbraco.Forms.Core.Enums.WorkflowExecutionStatus Execute(Record record, RecordEventArgs e)
            {
                IEmailServices emailServices = IoCWrapper.Resolve<IEmailServices>();
                try
                {
                    RecordsViewer viewer = new RecordsViewer();
                    XmlNode xml = viewer.GetSingleXmlRecord(record, new XmlDocument());

     

                    string email = GetEmailAddressViaLookUp(xml);
                    string mappedXsltPath = HttpContext.Current.Server.MapPath("/xslt/" + XsltFile);

     

                    if (!string.IsNullOrEmpty(email) && Regex.IsMatch(email, Resources.Regex.Email))
                    {
                        emailServices.SendContourEmail(email, EmailSubject, xml, mappedXsltPath);
                    }

     

                }
                catch (Exception ex)
                {

     

                }
                return WorkflowExecutionStatus.Completed;
            }

     

            private string GetEmailAddressViaLookUp(XmlNode xml)
            {
                // EmailLookUpField - this is the field which we have specified in the workflow that has the email address 
                // values assigned to them, in our case the "Sectors" checkbox list.
                string xpath = string.Format(LookUpFieldXpath, EmailLookUpField);

     

                XmlNode destinationEmailNode = xml.SelectSingleNode(xpath);

     

                if (destinationEmailNode != null)
                {
                    XmlNodeList destinationNodes = destinationEmailNode.SelectNodes("values/value");
                    if (destinationNodes != null)
                    {
                        foreach (XmlNode destinationNode in destinationNodes)
                        {
                            if (destinationNode.Attributes != null)
                            {
                                return destinationNode.InnerText;

     

                            }
                        }
                    }
                }
                return string.Empty;
            }
        }

  • Avatar98posts172karma
    Comment with ID: 98312
    Tom Smith posted this reply November 29, 2011 @ 06:32

    Hi Paul,

    I use a quick (and dirty) helper method to grab content of a form field by field caption

    public static RecordField GetByLabel(this Record record, string labelName)

    {

    return record.RecordFields.Values.Where(value => value.Field.Caption == labelName).FirstOrDefault();

    }

    You already have the record from the public override WorkflowExecutionStatus Execute(Record record, RecordEventArgs e)

    Can use the ValuesAsString() method on the returned record field to get a string value e.g.

    string recipient = CogFormHelpers.GetByLabel(record, EmailFieldLabel).ValuesAsString(); 

    where EmailFieldLabel is the caption of the email field e.g. "Email Address"

    Many Thanks,

    Tom

     

     

     


Please login or Sign up To post replies