i create a new class and added to the bin folder in umbraco, the code of the class is this one:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Umbraco.Forms.Core;
using Umbraco.Forms.Core.Enums;
using Umbraco.Forms.Data.Storage;
using System.Xml;
using System.Xml.XPath;
using Umbraco.Forms.Core.Attributes;
using umbraco.BusinessLogic;
namespace SendContourEmail
{
public class SendContourEmail : WorkflowType
{
// These regions set the fields in for new/cutom workflow you are wanting to create
[Setting("EmailLookUpField", description = "Enter the receiver email lookup field", control = "Umbraco.Forms.Core.FieldSetting.TextField")]
public string EmailLookUpField { get; set; }
[Setting("EmailFrom", description = "Email from field alias that contains email address", control = "Umbraco.Forms.Core.FieldSetting.TextField")]
public string EmailFrom { get; set; }
[Setting("Subject", description = "Enter the subject", control = "Umbraco.Forms.Core.FieldSetting.TextField")]
public string Subject { get; set; }
[Setting("Message", description = "Enter the intro message", control = "Umbraco.Forms.Core.FieldSetting.TextArea")]
public string Message { get; set; }
[Setting("ErrorEmailAddress", description = "Error logging email", control = "Umbraco.Forms.Core.FieldSetting.TextField")]
public string ErrorEmailAddress { get; set; }
public override List<Exception> ValidateSettings()
{
List<Exception> l = new List<Exception>();
if (string.IsNullOrEmpty(EmailLookUpField))
l.Add(new Exception("'Email Look up field' setting not filled out'"));
if (string.IsNullOrEmpty(Message))
l.Add(new Exception("'Message' setting not filled out'"));
return l;
}
private const string LookUpFieldXpath = "//fields/child::* [caption = '{0}']";
// Generates the new workflow details for Contour in the workflow dropdown in Contour
public SendContourEmail()
{
// Need to generate a new guid for the new custom workflow - add your own GUID
this.Id = new Guid("55C192F4-65FB-11E0-8798-CF7DDFD72085");
this.Name = "Send Custom Email";
this.Description = "Send Email to the person who fill the form";
}
public override WorkflowExecutionStatus Execute(Record record, RecordEventArgs e)
{
try
{
RecordsViewer viewer = new RecordsViewer();
XmlNode xml = viewer.GetSingleXmlRecord(record, new XmlDocument());
XPathNavigator navigator = xml.CreateNavigator();
XPathExpression selectExpression = navigator.Compile("//fields/child::*");
selectExpression.AddSort("@pageindex", XmlSortOrder.Ascending, XmlCaseOrder.None, "", XmlDataType.Number);
selectExpression.AddSort("@fieldsetindex", XmlSortOrder.Ascending, XmlCaseOrder.None, "", XmlDataType.Number);
selectExpression.AddSort("@sortorder", XmlSortOrder.Ascending, XmlCaseOrder.None, "", XmlDataType.Number);
XPathNodeIterator nodeIterator = navigator.Select(selectExpression);
string list = "<dl>";
while (nodeIterator.MoveNext())
{
list += "<dt><strong>" + nodeIterator.Current.SelectSingleNode("caption").Value + ": </strong><dt><dd>";
XPathNodeIterator values = nodeIterator.Current.Select(".//value");
while (values.MoveNext())
list += values.Current.Value.Trim() + "<br/>";
list += "</dd>";
}
list += "</dl>";
string body = "<p>" + Message + "</p>" + list;
IEnumerable<string> emails = GetEmailAddressesViaLookUp(xml, body);
}
catch (Exception ex)
{
LogError(ex.ToString());
}
return WorkflowExecutionStatus.Completed;
}
private IEnumerable<string> GetEmailAddressesViaLookUp(XmlNode xml, string emailBody)
{
// 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);
string fromEmailXpath = string.Format(LookUpFieldXpath, EmailFrom);
var addresses = new List<string>();
XmlNode destiniationIds = xml.SelectSingleNode(xpath);
XmlNode fromEmail = xml.SelectSingleNode(fromEmailXpath);
XmlNode emailFromNode = fromEmail.SelectSingleNode("values/value");
EmailFrom = emailFromNode.InnerText;
if (destiniationIds != null)
{
// Loops through the the List of emails and sends an email to each department
IEnumerable<string> emails = ExtractSectorsFromNodeXml(destiniationIds);
foreach (string email in emails)
{
SendMail(email, Subject, emailBody);
}
}
return addresses;
}
private IEnumerable<string> ExtractSectorsFromNodeXml(XmlNode destinations)
{
var ids = new List<string>();
XmlNodeList destinationNodes = destinations.SelectNodes("values/value");
if (destinationNodes != null)
{
foreach (XmlNode destinationNode in destinationNodes)
{
if (destinationNode.Attributes != null)
ids.Add(destinationNode.InnerText);
}
}
return ids;
}
private void SendMail(string to, string subject, string message)
{
var m = new System.Net.Mail.MailMessage
{
From = new System.Net.Mail.MailAddress(EmailFrom),
Subject = subject,
IsBodyHtml = true
};
m.To.Add(to);
m.Body = message;
var s = new System.Net.Mail.SmtpClient("localhost");
s.Send(m);
}
private void LogError(string ErrorMessage)
{
Log.Add(LogTypes.Error, 0, ErrorMessage);
if (ErrorEmailAddress != string.Empty)
{
SendMail(ErrorEmailAddress, "Contour custom workflow dmc lookup error", ErrorMessage);
}
}
}
}
at the end i only have the workflow form created with this but there is no email sent when i create a new field called email and put an email there to reaply Approved to the person who fill the form
and this is the image of the created workflow when i acces workflow in the backend of contour
thanks a lot for any help, i feel lost with this subject.
IMPORTANT NOTE: the only change that i did in the original code of the class was change "emailAdress" for "email" based on the case that i cant compile the class without having any errors.
to add more info to the problem... the change that i made on the original code of the class, was in the line 120 of the code, and when i add the function to the workflow Approved, and i fill a test of the form, and click Approved on the backend of contour, notthing happened.
yes, the default send email is working, but i was trying to setup this in the back end, to cnahged and use it as a template for diferent porpuses, i have a valid smtp server setup, and a valid sender email address in the umbracosettings.config, i just dont know why is not working, thanks again for any help!
I receive the email sent by "LogError" function, saying "System.NullReferenceException: Object reference not set to an instance of an object" at line 108 and 89 but I don't know why.
SendCountourEmail is not working, problem adding new feature to contour
hi everyone, im having some problem when im trying to add a new workflow to contour, im using the metod here described but is not working for me.
http://our.umbraco.org/wiki/how-tos/extending-contour,-sending-email-dependent-on-form-selection
i create a new class and added to the bin folder in umbraco, the code of the class is this one:
at the end i only have the workflow form created with this but there is no email sent when i create a new field called email and put an email there to reaply Approved to the person who fill the form
and this is the image of the created workflow when i acces workflow in the backend of contour
thanks a lot for any help, i feel lost with this subject.
IMPORTANT NOTE: the only change that i did in the original code of the class was change "emailAdress" for "email" based on the case that i cant compile the class without having any errors.
to add more info to the problem... the change that i made on the original code of the class, was in the line 120 of the code, and when i add the function to the workflow Approved, and i fill a test of the form, and click Approved on the backend of contour, notthing happened.
thatnks again for any help with this!
Comment author was deleted
Hi Andres,
And does the default send email workflow work?
DO you have a valid smtp sever setup in the web.config + a valid sender email address in the umbracosettings.config?
yes, the default send email is working, but i was trying to setup this in the back end, to cnahged and use it as a template for diferent porpuses, i have a valid smtp server setup, and a valid sender email address in the umbracosettings.config, i just dont know why is not working, thanks again for any help!
Hi Andres,
Some solution on this?
I've got the same problem.
I receive the email sent by "LogError" function, saying "System.NullReferenceException: Object reference not set to an instance of an object" at line 108 and 89 but I don't know why.
is working on a reply...