{"id":1070,"date":"2019-03-25T14:35:16","date_gmt":"2019-03-25T14:35:16","guid":{"rendered":"https:\/\/www.harepoint.com\/Blog\/?p=1070"},"modified":"2020-04-06T13:23:14","modified_gmt":"2020-04-06T13:23:14","slug":"mail-merge-in-sharepoint","status":"publish","type":"post","link":"https:\/\/www.harepoint.com\/Blog\/mail-merge-in-sharepoint\/","title":{"rendered":"Mail Merge in SharePoint"},"content":{"rendered":"\n<h2>Introduction<\/h2>\n<p><a href=\"\/Products\/SharePoint-Workflow-Office-365\/Default.aspx\"><strong>HarePoint Workflow Extensions for Office 365<\/strong><\/a> significantly improves the automation capabilities in SharePoint Online making almost any task achievable.<\/p>\n<p>In this article, we will review how to automate sending personalized emails using the workflows empowered with <b>HarePoint Workflow Extensions for Office 365<\/b>.<\/p>\n<h2>Scenario<\/h2>\n<p>We have all the subscribers listed in a <strong>Subscribers<\/strong> list. The task is to send personalized emails to these (all or only specific) subscribers.<\/p>\n<p>The list contains subscribers\u2019 data, such as first and last name, email address (but there can be more). There can also be attachments for each subscriber \u2013 invoices, coupons, agreements or anything else \u2013 these attachments will be sent in an email as well.<\/p>\n<p>There is a special <strong>Email sent<\/strong> field in the list that is used as a flag to manage sending emails.<\/p>\n<p><img decoding=\"async\" src=\"\/Pictures\/blog\/mail-merge-sharepoint\/subscribers-list.png\" alt=\"Subscribers listed in a Subscribers list\"><\/p>\n<p>The automation will be based on two workflows:<\/p>\n<ul>\n<li><strong>Send email<\/strong> workflow \u2013 a list workflow associated with <strong>Subscribers<\/strong> list, configured to trigger on item change. Creates a personalized email and sends it to a single specific subscriber.<\/li>\n<li><strong>Mail merge initiator<\/strong> workflow \u2013 a site workflow that runs through all items in <strong>Subscribers<\/strong> list and modifies all (or only specific) items, which in turn initiates Send email workflow instances for each of the changed items.<\/li>\n<\/ul>\n<p>These workflows are described in details below.<\/p>\n<h2>The first workflow: \u2018Send email\u2019 list workflow<\/h2>\n<p><a href=\"\/Pictures\/blog\/mail-merge-sharepoint\/send-email-list-workflow.png\" data-rel=\"lightbox-image-0\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img decoding=\"async\" src=\"\/Pictures\/blog\/mail-merge-sharepoint\/send-email-list-workflow.png\" alt=\"Send email list workflow\"><\/a><\/p>\n<p>Let\u2019s review the workflow stages in more details.<\/p>\n<p><strong>Personalize E-mail<\/strong> stage creates a personalized email based on a Word document template. This template contains content control fields that can be updated from a workflow. In this example, there is only one content control field (for the subscriber name), but there can be as many as you need:<\/p>\n<p><img decoding=\"async\" src=\"\/Pictures\/blog\/mail-merge-sharepoint\/personalized-word-template.png\" alt=\"Personalized email based on a Word document template\"><\/p>\n<p>The Word document (template) is stored in a document library:<\/p>\n<p><img decoding=\"async\" src=\"\/Pictures\/blog\/mail-merge-sharepoint\/template-document-library.png\" alt=\"Word template in a document library\"><\/p>\n<p>The workflow generates a random name for a personalized document and then copies the template to the same document library with this random name:<\/p>\n<p><a href=\"\/Pictures\/blog\/mail-merge-sharepoint\/generate-random-name.png\" data-rel=\"lightbox-image-1\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img decoding=\"async\" src=\"\/Pictures\/blog\/mail-merge-sharepoint\/generate-random-name.png\" alt=\"Random name for a personalized document\"><\/a><\/p>\n<p>Then the workflow saves the full URL of a personalized document in <strong>Email Body Url<\/strong> variable.<\/p>\n<p><img decoding=\"async\" src=\"\/Pictures\/blog\/mail-merge-sharepoint\/email-body-url-variable.png\" alt=\"Email Body URL variable\"><\/p>\n<p>To achieve that, we are using a <strong>Lookup for string<\/strong> as follows:<\/p>\n<p><img decoding=\"async\" src=\"\/Pictures\/blog\/mail-merge-sharepoint\/lookup-for-string.png\" alt=\"Lookup for string\"><\/p>\n<p>After that, the workflow updates the content control fields in a document, based on values from <strong>Subscribers<\/strong> list:<\/p>\n<p><img decoding=\"async\" src=\"\/Pictures\/blog\/mail-merge-sharepoint\/workflow-update-document.png\" alt=\"Workflow updates the content control fields in a document.\"><\/p>\n<p>Note that <strong>Email Body Url<\/strong> variable from the previous action is used here. If you have more than one content control field in a document, you would need separate actions for each of them.<\/p>\n<p><strong>Send E-mail<\/strong> stage of a workflow actually sends a personalized email to the given subscriber.<\/p>\n<p>Since the <strong>Subscribers<\/strong> list may contain list attachments that need to be sent together with an email, the first action in this stage gets the URLs of all attachments for the given subscriber:<\/p>\n<p><img decoding=\"async\" src=\"\/Pictures\/blog\/mail-merge-sharepoint\/list-item-attachments.png\" alt=\"Attachments to be emailed\"><\/p>\n<p>Then we need to specify the SMTP server that will be used to send emails \u2013 in this example, we are going to use Office 365 SMTP server that has <em>smtp.office365.com<\/em> address:<\/p>\n<p><img decoding=\"async\" src=\"\/Pictures\/blog\/mail-merge-sharepoint\/smtp-send-email.png\" alt=\"Specify SMTP server\"><\/p>\n<p>It is convenient to create a special email account that will be used to send out these emails. Usually <em>noreply@yourcompany.com<\/em> addresses are used.<\/p>\n<p>The workflow needs to authenticate to SMTP server. Since for security reasons HarePoint Workflow Extensions for Office 365 do not operate with credentials in plain text, we need to encrypt the credentials of <em>noreply@yourcompany.com<\/em> account \u2013 it can be done in HarePoint Workflow Extensions for Office 365 console:<\/p>\n<p><strong>Site Contents \u2013 HarePoint Workflow Extensions for Office 365 \u2013 Security \u2013 Login and Password<\/strong><\/p>\n<p><img decoding=\"async\" src=\"\/Pictures\/blog\/mail-merge-sharepoint\/smtp-login-password.png\" alt=\"SMTP security - login and password\"><\/p>\n<p>Enter login and password for <em>noreply@yourcompany.com<\/em> account:<\/p>\n<p><img decoding=\"async\" src=\"\/Pictures\/blog\/mail-merge-sharepoint\/encrypt-login-password.png\" alt=\"Encrypt login password\"><\/p>\n<p>Click <strong>Encrypt<\/strong> button, then copy and paste the encrypted token to <strong>Set Authentication<\/strong> action; choose to authenticate to an <strong>Email server<\/strong>:<\/p>\n<p><img decoding=\"async\" src=\"\/Pictures\/blog\/mail-merge-sharepoint\/email-server-authentication.png\" alt=\"Encrypted token to authenticate to an Email server\"><\/p>\n<p>Finally, an action to send an email with the body from a Word document, including attachments, to a given subscriber:<\/p>\n<p><a href=\"\/Pictures\/blog\/mail-merge-sharepoint\/send-email-word-document.png\" data-rel=\"lightbox-image-2\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img decoding=\"async\" src=\"\/Pictures\/blog\/mail-merge-sharepoint\/send-email-word-document.png\" alt=\"Send email created from a Word document\"><\/a><\/p>\n<p>At the <strong>Cleanup<\/strong> stage, the workflow removes the personalized document from a document library:<\/p>\n<p><img decoding=\"async\" src=\"\/Pictures\/blog\/mail-merge-sharepoint\/delete-document-from-library.png\" alt=\"Delete file from a document library\"><\/p>\n<h2>The second workflow: \u2018Mail merge initiator\u2019 site workflow<\/h2>\n<p>In order to initiate sending personalized emails to multiple subscribers, a <strong>site workflow<\/strong> is used. This workflow checks and updates the field <strong>Email Sent<\/strong>, type of <strong>Yes\/No<\/strong>. As soon as the field is updated, the first workflow \u2018Send email\u2019 will trigger since it is tied to an item change.<\/p>\n<p>The site workflow uses the special action, available only from HarePoint Workflow Extensions for Office 365 &#8211; <strong>Loop through and Update list Items<\/strong>, to loop through multiple items in <strong>Subscribers<\/strong> list and update them:<\/p>\n<p><img decoding=\"async\" src=\"\/Pictures\/blog\/mail-merge-sharepoint\/loop-through-update-list-Items.png\" alt=\"Loop through and Update SharePoint list Items\"><\/p>\n<p>As you can see, this action uses CAML query to choose which subscribers from the list should be affected. In this example, we use the simplest (empty) CAML query that includes all items in a list:<\/p>\n<code>&lt;Where \/&gt;<\/code>\n<p>Which is equal to<\/p>\n<code>&lt;Where&gt;<br>\n&lt;\/Where&gt;\n<\/code>\n<p>However, more complex CAML queries can be used to select only specific subscribers by a certain criteria.<\/p>\n<h2>Ready to go<\/h2>\n<p>Ok, now you can start the Mail Merge by launching the site workflow <strong>Mail merge initiator<\/strong>.<\/p>\n<p><img decoding=\"async\" src=\"\/Pictures\/blog\/mail-merge-sharepoint\/mail-merge-initiator.png\" alt=\"Mail Merge by site workflow\"><\/p>\n<p>In our case, it had changed 17 items in <strong>Subscribers<\/strong> list, thus initiating 17 Send email workflow instances for each of those affected items.<\/p>\n<p>In <strong>Subscribers<\/strong> list, in <strong>Send email<\/strong> column, we can track the status of <strong>Send email<\/strong> workflow for each subscriber:<\/p>\n<p><img decoding=\"async\" src=\"\/Pictures\/blog\/mail-merge-sharepoint\/send-email-workflow-status.png\" alt=\"Send email SharePoint workflow status\"><\/p>\n<p>Finally, here is the personalized email received by a subscriber, as seen in a mailbox:<\/p>\n<p><img decoding=\"async\" src=\"\/Pictures\/blog\/mail-merge-sharepoint\/personalized-email-received.png\" alt=\"Example of a personalized email\"><\/p>\n<h2>Conclusion<\/h2>\n<p>We have created a fully functional Mail Merge functionality in SharePoint using only the native components \u2013 a SharePoint list, a list workflow and a site workflow. Such a simple approach became possible thanks to <a href=\"\/Products\/SharePoint-Workflow-Office-365\/Default.aspx\"><b>HarePoint Workflow Extensions for Office 365<\/b><\/a>, which provides a simple and elegant solution to any task.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this article, we will review how to automate sending personalized emails using the workflows empowered with HarePoint Workflow Extensions for Office 365.<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[28],"_links":{"self":[{"href":"https:\/\/www.harepoint.com\/Blog\/wp-json\/wp\/v2\/posts\/1070"}],"collection":[{"href":"https:\/\/www.harepoint.com\/Blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.harepoint.com\/Blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.harepoint.com\/Blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.harepoint.com\/Blog\/wp-json\/wp\/v2\/comments?post=1070"}],"version-history":[{"count":38,"href":"https:\/\/www.harepoint.com\/Blog\/wp-json\/wp\/v2\/posts\/1070\/revisions"}],"predecessor-version":[{"id":1679,"href":"https:\/\/www.harepoint.com\/Blog\/wp-json\/wp\/v2\/posts\/1070\/revisions\/1679"}],"wp:attachment":[{"href":"https:\/\/www.harepoint.com\/Blog\/wp-json\/wp\/v2\/media?parent=1070"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.harepoint.com\/Blog\/wp-json\/wp\/v2\/categories?post=1070"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.harepoint.com\/Blog\/wp-json\/wp\/v2\/tags?post=1070"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}