Home > CMS- Content Management System > Sharepoint > How to send an email through Visual Studio workflow in SharePoint Hosted App

How to send an email through Visual Studio workflow in SharePoint Hosted App

Hello,

In this article, I will show you how to send an email through Visual Studio workflow in SharePoint Hosted App, I am using VS17, We have seen we can send email using SharePoint Designer, Microsoft Flow, and RET API. Send an email through Microsoft.SharePoint.DesignTime.Activities.Email activity is quite old but I had recently used in a SharePoint Hosted app.

So I assuming you have a setup of basic SharePoint add-in with the SP list. Read more SharePoint Add-in articles in this series. So let’s start.

  1. Open the VS, and Create a basic SP list with the email address column, follow this article to know more how to create a list. 
  2. To Create a workflow Right Click on the Solution Name, then Add > New Item or press Ctrl + Shift + A
  3. Select Workflow option under Office/SharePoint, and Name it
  4. Select the workflow type, select List Workflow
  5. Select the History and Task list if you performing any log and task in the workflow
  6. It will land you workflow designer page, expand the SP – List tab on the Toolbox then drag the LookupSPListItem activity onto the designer
  7. Add the LookupSPListItem activity from Toolbox to get current list Item value, Set the ItemId as the current item in the property section 
  8. After that, add the GetDynamicValueProperties activity by clicking on the Get Properties link and get the current item email address 
    1. Create a string type variable varListEmailAddress
    2. Select the list type from Property pop-up
    3. Assign the varListEmailAddress to the EmailAddress column and click OK.
  9. Email activity only accept Collection<String> in the recipient address, So we have to convert string type variable [varListEmailAddress] to object Collection<String>  variable
  10. Add BuidCollectoin<Syste.String> activity, with Tvalue string form Toolbox
  11. Create a variable with variable type Collection<String> called varEmailCollect
  12. Convert varListEmailAddress to Collection<String>
    1. Select Browse for Types from the Variable type dropdown list as shown in Figure
    2. In the pop-up dialog box, type Collect into the Type Name text box to filter the selections as shown in figure
    3. Choose System.Collections.ObjectModel.Collection<T>, then specify String as the type
    4. In the dialog box, choose to Create Argument to specify a value to add to the collection. On values, property adds the varListEmailAddress, Click OK and on Result property add the varEmailCollect variable.
  13. Now assign the varEmailCollect to the Email recipient and add the subject, body.
  14. I added WriteToHistory Activity in the Workflow for debugging  We done here.

Now we have to set when workflow run,

  1. So Right-click on the Workflow Name and select Properties,
  2. Set the workflow on New or Update item

Testing your app

  1. Now Save and Start the Build, once the app installed
  2. Open the DemoList, Type the Email address and Save it
  3. Now you can see mail has been sent to the entered email address
  4. Here is Workflow Log History

Note: Email activity accepts SharePoint user values in the form of email address, user ID, or claims user name.

Here are some various method by initiating them you can send the email

  1. Using LookupWorkflowContextProperty you can send the email to the initiator 
  2. Using Assign activity you can send email to the Static Email Address on add new or update list item. Please check below figure.
  3. Using Assign activity you can send email to the static email address with Site Workflows

We have now completed this walkthrough. Hope you find it helpful. If you liked this article, then please share and comment.

This Article is TAGGED in , , , , , , , . BOOKMARK THE permalink.

Avatar photo
Chintan Upadhayay
I am a senior web developer with more than 4+ years experience in WordPress, PHP, MySQL, HTML, CSS, jQuery and other web technologies. I am the beginner in SharePoint and Learning SharePoint.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">