SharePoint Custom Workflow using Visual Studio – Step by Step

In this SharePoint tutorial, we will discuss, how we can create a workflow using visual studio for SharePoint online site, and also we will see how to deploy the visual studio sharepoint workflow to host web list or library. Let us develop a very simple custom workflow in SharePoint using visual studio.

Since we are going to develop as a SharePoint hosted add-in, make sure you have a developer site is ready which you. You can check an article on how to create a developer site for SharePoint online Office 365.

Create SharePoint workflow visual studio

Follow the below steps to create a SharePoint workflow using visual studio as an add-in and deploy to host web in SharePoint Online Office 365.

Open visual studio 2015/2017 and then File -> New -> Project. And then in the New Project dialog box, choose SharePoint Add-in from Templates -> Visual C# -> Office/SharePoint like below:

SharePoint visual studio workflow
SharePoint visual studio workflow

Then in the Add-in settings dialog box, give SharePoint online developer site URL and choose the host type for SharePoint Add-in as “SharePoint-hosted“.

SharePoint visual studio workflow
SharePoint visual studio workflow

Then it will ask for credentials to connect to SharePoint online site. Once entered successfully, it will show the below screen.

visual studio sharepoint workflow
visual studio sharepoint workflow

Now the SharePoint Add-in project will look like below:

sharepoint workflow development
sharepoint workflow development

To make the workflow available in the host web we need to choose “Integrated App” project properties to true. But since we have not added Workflow yet, you will not be able to see the “Integrated App” project properties.

visual studio workflow
visual studio workflow

Next, we will add a Workflow to the project. Right-click on the Project, Add -> New Item. And in the Add New Item dialog box, choose Visual C# Items -> Office/SharePoint. And then choose the Workflow template like below:

sharepoint workflow visual studio
sharepoint workflow visual studio

Then in the next screen, give a workflow name and choose List Workflow in the type of workflow like below:

sharepoint online custom workflow
sharepoint online custom workflow

Then in the Select the lists you will use when debugging screen, uncheck the checkbox and do not select anything like below:

sharepoint custom workflow
sharepoint custom workflow

Then the workflow will be added to the project and it looks like below:

custom workflow sharepoint
custom workflow sharepoint

Now click on F4 in the project to open the Project properties and there change the Integrated App property to True like below:

sharepoint online visual studio workflow
sharepoint online visual studio workflow

Now in the workflow search for a WriteToHistory action and click on the Message in the Properties box. There write some message as a string like below:

sharepoint online visual studio workflow
sharepoint online visual studio workflow

So this is a very simple workflow having one activity only. Now open the AppManifest.xml file and give Web to manage or full control permission like below:

how to create workflow in sharepoint 2013 using visual studio
how to create a workflow in sharepoint 2013 using visual studio

Now we will try to publish the workflow to the SharePoint online site. Right-click on the Project and click on Publish… like below:

sharepoint 2013 workflow visual studio example
sharepoint 2013 workflow visual studio example

This will open the Publish your add-in page, here click on Package the add-in which will generate the .app file like below:

sharepoint online workflow visual studio example
sharepoint online workflow visual studio example

Now we need to open the .app file using 7-zip or a similar tool like below:

sharepoint 2013 approval workflow using visual studio
sharepoint 2013 approval workflow using visual studio

Then open the WorkflowManifest.xml file and check if the below tag is there or not.

<IntegratedApp>true</IntegratedApp>

If not add it and save the file.

Now is time to upload the .app file into the App Catalog site. Once it is uploaded to the App Catalog site, We can add it to any SharePoint site, Site Contents > Add an App > Select your app and add it to your Team Site.

If it asks to trust, Trust it.

Now open any list and from the ribbon click on List Settings -> Workflow Settings and then click on Add a Workflow. On that screen, you will see two options for Available Apps.

One is for This List and another one as your App name (Here in my case it is ApprovalListWFHostWeb).

In the Workflow details Click on the <Workflow Name>(in my case the name is ListApprovalWorkflow) like below:

how to create workflow in sharepoint online using visual studio
how to create a workflow in sharepoint online using visual studio

Then give a name for the workflow and also you can select Task List & History List. Also, choose the Start Options like when you want to start the workflow.

sharepoint hosted app workflow
sharepoint hosted app workflow

Now if you will again go back to the Workflow settings page, you can see the workflow is attached to the list like below:

visual studio sharepoint online workflow
visual studio sharepoint online workflow

Now it is time to start the workflow manually for one item in the list. For this select an item from the list, then click on ECB menu -> Advanced -> Workflows.

sharepoint custom workflow visual studio
sharepoint custom workflow visual studio

Then click on the workflow app and then you can see the workflow under Start a New Workflow like below:

sharepoint 2013 custom workflow activity visual studio
sharepoint 2013 custom workflow activity visual studio

Once the workflow got started successfully, you can see it will write the message which we have added inside the WriteToHistory activity like below:

create workflow in sharepoint 2013 using visual studio
create a workflow in sharepoint 2013 using visual studio

Send Email using visual studio workflow in SharePoint

Now, let us see another visual studio sharepoint workflow example. Here, we will see how to send an email using visual studio workflow in SharePoint Online.

Follow the above step to first create a custom SharePoint workflow using visual studio.

Once you follow the above article and create your first workflow, the workflow screen will appear like below whereas as a developer you can add activities.

sharepoint email using visual studio workflow
sharepoint email using visual studio workflow

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 messages which will be written to our Workflow History List.

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

send email using visual studio workflow
send email using visual studio workflow

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

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

send email using visual studio workflow sharepoint
send email using visual studio workflow sharepoint

Here properties like Subject, Body will take string parameters. 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 workflow send email
sharepoint workflow send email

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

new System.Collections.ObjectModel.Collection() { "[email protected]"}

The full email properties will look like below:

sharepoint send email using visual studio workflow
sharepoint send email using visual studio workflow

Here for future propose, I have given Full Control Access in the 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”.

send email using visual studio workflow
send email using visual studio workflow

Once you trust it, the workflow or the app will be available in all lists and libraries. In my 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.

visual studio send email
visual studio send email

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.

sharepoint hosted app workflow
sharepoint hosted app workflow

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.

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

sharepoint workflow activity
sharepoint workflow activity

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

send email using visual studio workflow
send email using 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.

visual studio workflow
visual studio workflow

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

sharepoint workflow email
sharepoint workflow email

This is how to send email using a custom SharePoint workflow.

Custom workflow in sharepoint 2013 using visual studio – Send email to a group

Now, let us see another example of sharepoint custom workflow using visual studio.

Here we will see how to send email to a SharePoint group using SharePoint custom workflow.

Before proceeding further, follow the above steps to create a sharepoint workflow in visual studio.

Before going further for workflow, I have created a SharePoint group and I have added two users to the SharePoint group.

Now in the workflow when I will send the email, the email should go to all the users presented inside the group. The SharePoint groups look like below:

sharepoint custom workflow visual studio
sharepoint custom workflow visual studio

Once you follow the above article and create your first workflow, the workflow screen will appear like below where as a developer you can add activities.

sharepoint online workflow email activity
sharepoint online workflow email 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 messages which will be written to our Workflow History List.

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

send email using SharePoint visual studio workflow
send email using SharePoint visual studio workflow

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

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

sharepoint 2013 custom workflow activity visual studio
sharepoint 2013 custom workflow activity visual studio

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

We can send an email to a SharePoint group. But If I will simply put the group name in To as a string like below, it will through me an error.

creating sharepoint workflow in visual studio
creating sharepoint workflow in visual studio

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

new System.Collections.ObjectModel.Collection() { "MyCustomSharePointGroup"}

The full email properties will look like below:

custom workflow in sharepoint 2013 using visual studio
custom workflow in sharepoint 2013 using visual studio

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“.

creating a custom workflow in sharepoint
creating a custom workflow in sharepoint

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

Now Open the SharePoint 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.

create custom workflow in sharepoint online
create custom workflow in sharepoint online

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.

how to create a custom workflow in sharepoint 2013
how to create a custom workflow in sharepoint 2013

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. Click on the … -> Advanced -> Workflows like below:

sharepoint online workflow email activity
sharepoint online workflow email activity

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

send email to sharepoint group using visual studio workflow
send email to sharepoint group using 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.

create custom workflow sharepoint 2013
create custom workflow sharepoint 2013

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

create custom workflow activity sharepoint 2013 visual studio
create a custom workflow activity sharepoint 2013 visual studio

This is how to send email to SharePoint group in visual studio workflow in SharePoint hosted add-in SharePoint online.

Call Web API using httpsend workflow activity in SharePoint Online

Let us see, how we can call a web API from a visual studio workflow by using a httpsend activity in SharePoint Online.

Follow the below step to call web API using httpsend workflow activity in SharePoint Online Visual Studio workflow.

Once the workflow is ready, First, we will create two variables.

  • varGetResponseContent (DynamicValue)
  • varResult (String)

If you will see the workflow variables it should look like below:

call web api from sharepoint
call web api from sharepoint

In the workflow, we can access the Web API Url through “HttpSend” workflow activity. Drag and drop a “HttpSend” activity from the Toolbox into the workflow designer interface.

Then in the HttpSend activity properties, choose the below things:

  • Method– GET
  • RequestHeader:
  • Accept (Header Name): “application/json;odata=verbose” (Header Value)
  • Content-Type (Header Name): “application/json;odata=verbose” (Header Value)

The RequestHeader looks like below:

call web api from sharepoint online
call web api from sharepoint online

Uri: Put the Web API URL. In my case below is the URL:

https://mysharepointwebapi.azurewebsites.net/api/Default/MyAction?testParameter=Bhawana

ResponseContent: varGetResponseContent (Give the DynamicVariable which we have created in the above step).

The properties of HttpSend Activity looks like below:

call web api from sharepoint online workflow
call web api from sharepoint online workflow

Now next we will try to retrieve the value which we are getting from the web API.

Now add a GetDynamicValueProperty <T> from the toolbox like below:

httpsend activity sharepoint 2013 workflow
httpsend activity sharepoint 2013 workflow

First, choose the Type as String in the GetDynamicValueProperty <T> activity like below:

httpsend activity sharepoint online workflow
httpsend activity sharepoint online workflow

Now in the GetDynamicValueProperty<T> properties, windows choose the below properties:

  • Result: varResult (This is the string variable we have created at the beginning)
  • Source: varGetResponseContent (Same variable which we have used in ResponseContent in HttpSend activity)
call web api from visual studio 2015 workflow httpsend activitity
sharepoint workflow activity

Now we can add a WriteToHistory activity to write the variable value in the SharePoint Workflow History list. It looks like below:

httpsend workflow activity
sharepoint workflow activity

The whole workflow looks like below:

call web api from sharepoint
call web api from sharepoint

Now our workflow development is ready. We can now deploy and see the result.

Right-click on the project and click on Deploy. Once it is deployed successfully, open any list and from the ribbon, click on Workflow Settings -> Add a workflow.

In the Add workflow page, click on the Workflow Add-in app and then Select the workflow like below:

call web api from sharepoint online
call web api from sharepoint online

Then give a name for the workflow, choose the Task list and History list, and check when you want to start the workflow, like below:

httpsend workflow activity
call web api from visual studio workflow httpsend activity sharepoint

Once the workflow is attached to a list, select an item and click on Workflow in the List tab in the ribbon. This will open the page to start a workflow. Click on the workflow link to start the workflow.

sharepoint workflow activity
httpsend activity sharepoint

Once the workflow is completed, you can see the status like below:

call web api from visual studio 2017 workflow httpsend activity
sharepoint httpsend activity

If you will check the details you can see the result like below:

sharepoint workflow activity
web api SharePoint online

You may like the following SharePoint workflow articles:

In this tutorial, we saw how to develop a visual studio sharepoint workflow and saw a few examples of visual studio sharepoint workflow. I hope it fix the below queries:

  • sharepoint custom workflow visual studio
  • sharepoint 2013 custom workflow activity visual studio
  • creating sharepoint workflow in visual studio
  • custom workflow in sharepoint 2013 using visual studio
  • creating a custom workflow in sharepoint
  • create custom workflow in sharepoint online
  • how to create a custom workflow in sharepoint 2013
  • how do i create a custom workflow in sharepoint online
  • create custom workflow sharepoint 2013
  • create custom workflow activity sharepoint 2013 visual studio
>