Sending customized email from SharePoint Workflow
I recently got a requirement from a customer asking for a solution that will send a customized email to the people. These were the requirements
1. The 'From address' of the email should be title of discussion list.
2. The 'To address' of the email should be to all the subscribers of that list (Alert configuration)
3. The 'Email Subject' should be the subject of topic + Subject ID.
4. The Body should have the actual content of the Discussion list.
Using SharePoint Designer, creating workflow with these functionalities was not possible. So, ultimately i had to end up writing custom workflow using visual studio. I decided to use SendEmail Activity thats already available Out of the box. In the MethodInvoking of SendEmail activity, I had to put down all logic to get this requirements done.
Getting the subject and body of the email was no big deal. The From address was a bit tricky as I had to write some code to get email alias of the list and append it with the server display address from IncomingEmailService.
The real challenge was in getting the ‘To Address’. The To address should contain all the users who were actually subscribed for that list Alert. So how will I get the list of users who are subscribed for that list? There was no direct API support available wherein we could get all the subscribed users from a given list. No direct mapping existed between the subscribed users and the list.
So here is the clue. Every time a new user is subscribed for a list, a new alert gets created!! I had to loop through the complete alert collection of the web, get into the correct list and then get user email from that alert and make sure that the final collection had only unique users.
The complete code snippet : SendEmail Method Invoking
SPFarm farm = workflowProperties.Site.WebApplication.Farm;
SPIncomingEmailService emailService = (SPIncomingEmailService)farm.GetChild<SPIncomingEmailService>();
string serverDisplayAddress = emailService.ServerDisplayAddress;
SPList list = workflowProperties.List;
SPAlertCollection alertCollection = workflowProperties.Web.Alerts;
ArrayList aListUniqueUser = new ArrayList();
foreach (SPAlert alert in alertCollection)
{
if (alert.AlertType == SPAlertType.List)
{
if (alert.List.ID == list.ID)
{
if (!aListUniqueUser.Contains(alert.User.Email))
aListUniqueUser.Add(alert.User.Email);
}
}
}
string toAddress = "";
foreach (string s in aListUniqueUser)
toAddress += s + ";";
sendEmail1.From = workflowProperties.List.EmailAlias + "@" + serverDisplayAddress;
sendEmail1.To = toAddress;
sendEmail1.Subject = workflowProperties.Item["Subject"].ToString() + "-[" + workflowProperties.Item["ID"].ToString() + "]";
sendEmail1.Body = workflowProperties.Item.Fields["Body"].GetFieldValueAsText(workflowProperties.Item["Body"]);
Comments
- Anonymous
February 14, 2012
Thank's it's nice info.Where do We put the code?what file? - Anonymous
February 14, 2012
Suharto - As I already mentioned in this blog, use this code snippet in MethodInvoking event of the SendEmail activity. - Anonymous
June 21, 2012
Hi,Thank you for this info. I have a similar request. Please see if you could give some inputs for this. Thank you in advance.I have a sharepoint 2010 list customised using infopath form. On click of the submit button on the form, an email should be generated with the list columns in the email "Body" and the current user id in the "From address" of the email.The only way that i figure out to do is using managed code in visual studio 2010.I need to know which project to be created in visual studio 2010, to implement this in code. And how to deploy it.Any input would be of great help.Thanks,Mahati - Anonymous
June 21, 2012
Mahati - Seems your requirement doesn't have any workflow involved. There are many ways to send an email in InfoPah. You can use SPUtility.SendMail method to send email - msdn.microsoft.com/.../ms477270.aspx. To do this just add a reference to the SharePoint dll to your InfoPath form template project and write a class file that uses SendMail method. In a similar way you can also use Send method of SmtpClient class. The other option is to use InfoPath based Class called EmailSubmitConnection - msdn.microsoft.com/.../microsoft.office.infopath.emailsubmitconnection(VS.80).aspxHTH!! - Anonymous
September 04, 2013
I have 2 ListSTORES(storename,storeUser,RegionalHead) andTask(Title,AssignedTo-lookup storename column from STORES)when i create a task agaist storename, then i want to send an Email to its respective storeuser and RegionalHead.How i configure the TO Fields to send email with respective storeuser and RegionalHead.Please Help,Thanks in advanceEmailId-sumankr.512@gmail.com