SharePoint workflow activity – SharePoint Online visual studio workflow send email example

This SharePoint tutorial is all about how to use SharePoint workflow activity in a visual studio workflow. We will take an example of how to send an email using the SharePoint email workflow activity using a visual studio workflow.

SharePoint workflow activity example

Let us check out the example of how to use the SharePoint workflow email activity in a visual studio workflow. We will see, a SharePoint Online visual studio workflow send email example.

In SharePoint Online, the visual studio workflow we will develop as a SharePoint hosted add-in. So before reading further, first check out, Develop SharePoint workflows using visual studio as SharePoint hosted App. And we will deploy the workflow to the host web in SharePoint Online.

As we all know to work with the Add-in model with the visual studio we need first a developer site for local debugging. If you have not created a developer site, create a SharePoint Online developer site first.

Once you follow the above article and create your first workflow, the workflow screen will appear like below and here we can add the SharePoint workflow activities.

sharepoint workflow activity
sharepoint workflow activity

From the toolbox from the left side, I have added two “WriteToHistory” activities from the SP-Utilities section like below. In those “WriteToHistory” activities I have put some message which will be written to our Workflow History List.

Inside these two activities, I have added one “Email” SharePoint workflow activity from the SP-Utilities section from a toolbox. The workflow now looks like below:

sharepoint visual studio workflow
sharepoint visual studio workflow

Here in the above screen, you can see an error mark that is coming because we need to add the mandatory properties inside the SharePoint Email Activity.

Here if you will look at the properties, you can see there are few optional properties, as well as a few Required properties are there. Apart from that Display Name is also there, which we can change.

sharepoint visual studio workflow examples
sharepoint visual studio workflow examples

Here properties like Subject, Body will take string parameter. But properties like To, CC, BCC will take Collection of Strings as a parameter.

So If I will simply put in To as a string like below, it will through me an error.

sharepoint 2013 visual studio workflow send email example
sharepoint visual studio workflow send email example

To resolve this we need to convert the string to a collection of strings. You can write like below:

new System.Collections.ObjectModel.Collection<string>() { "Bijay@OnlySharePoint2013.onmicrosoft.com"}

The full email properties will look like below:

sharepoint 2013 visual studio workflow send email example
sharepoint online visual studio workflow send email example

Here for future propose, I have given Full Control Access in AppManifest.xml file.

Now right click on the Project -> click on Deploy.

Once it is deployed successfully. It will ask you to trust the App like below. Click on “Trust It”.

sharepoint online create workflow visual studio
sharepoint online create workflow visual studio

Once you trust it, the workflow or the app will be available in all lists and libraries. On my SharePoint developer site, I have a list name as MyTestList.

Now Open the list and then go to the Workflow Settings page. Here you can see two Apps: This List and [Your App Name]. Click on your App name and then click on Add a workflow.

sharepoint visual studio workflow examples
sharepoint visual studio workflow examples

Here in the Add a Workflow page, It will show you the workflow templates, since I have only one workflow it is showing one. Then give a name for the workflow, choose the Task List, History List, and choose the Start Options for the workflow. Then click on OK.

Email Workflow Activity sharepoint 2016 visual studio
Email Workflow Activity sharepoint 2016 visual studio

Now go back to the list and add one item to the list. Since the workflow will trigger on item add, you can see the details of the workflow.

sharepoint 2013 workflow send email
SharePoint 2013 workflow send email

Click on the … -> Advanced -> Workflows like below:

In the details page, click on your App, then you will be able to see the completed apps like below:

sharepoint online visual studio workflow
sharepoint online visual studio workflow

Click on it to see the full details of the workflow. Check out the history list, it has added the messages which we wrote inside the WriteToHistory activity.

sharepoint 2013 workflow send email when new item created
SharePoint 2013 workflow send an email when new item created

Now if you will check the email, you can see the messages like below:

send email activity sharepoint workflow
send email activity SharePoint workflow

You may like following SharePoint hosted add-in tutorials:

In this tutorial, we check how to use SharePoint workflow activity. How to send an email using visual studio workflow in SharePoint Online. We saw how to use the SharePoint visual studio workflow activities like WriteToHistory and Email.

>