Creating SharePoint 2013 Workflow Apps with Visual Studio – Visual Studio Magazine

Creating SharePoint 2013 Workflow Apps with Visual Studio – Visual Studio Magazine

Tips and Tricks
SharePoint 2013 workflows for automating common business processes can be deployed as SharePoint apps built using Visual Studio.  
Modern organizations have well-defined business processes that can be programmatically automated through SharePoint. Consider the leave-approval process. An employee asking for an annual leave needs to get approval from an immediate manager, then it goes to a department head for approval, then an HR manager needs to approve it to complete the process. Of course, there might be other conditions in a real-world scenario, such as annual leave requests needing at least a three-week’s advance notice prior to the start date. For casual, short-term leave there might be no need for department head approval.
These types of business processes can be automated through a workflow engine with custom logics applied. In this article, I’ll develop a workflow in SharePoint 2013 and deploy it as an app using Visual Studio.
Workflows in SharePoint 2013
SharePoint 2013 workflows are powered by Windows Workflow Foundation 4. The activities defined in Windows Workflow Foundation are used in SharePoint 2013 to implement the logic and flows of the business processes.
Workflows are created in one of three ways:
There are also many third-party custom workflow engines that can be used to create workflows in SharePoint.
SharePoint has three types of workflows:
Prior to SharePoint 2013, SharePoint workflows used the same application pool to run workflows. This resulted in many performance issues with the entire farm when there are long-running workflows. SharePoint 2013 is designed with a new workflow execution engine hosted outside SharePoint — you’ll need to install the Workflow Manager in the server farm in order to run those workflows.
SharePoint 2013 also supports SharePoint 2010 workflows that are built on Windows Workflow Foundation 3, with SharePoint workflow interop. Microsoft provides this to make the workflow migration procedure easier.
Visual Studio Workflow Authoring Types
Visual Studio workflows can be divided into three types by the means of authoring:
A sequential workflow executes according to a particular order — it might have some decision points, but once it moves forward, it cannot backtrack.
A flowchart workflow provides the ability to move around to different sections according to the conditions specified in the logic.
A state machine workflow maintains state and allows the workflow to stay in many states according to the events happening around it. It differs from a fowchart because it doesn’t follow a specific path of execution.
Prerequisites
The following environment configurations are needed to start building workflows for SharePoint 2013.
If you are deploying a workflow for Office 365, it already comes with the necessary features activated. All you need is a Visual Studio version newer than the ones mentioned in these prerequisites.
Sample Scenario
I’ll show how to create a basic workflow using Visual Studio. Imagine a scenario for budget approval. Certain processes are set in order to manage the approval depending on the budget amount. Here are the two scenarios:
This SharePoint app will provide the budget approval functionality, and it will be deployed to a development site hosted on Office 365. The app will contain four components:
Creating the Visual Studio Solution
To start, create a new project with the App for SharePoint template under the Office/SharePoint section and name it BudgetApprovalWorkflow. Next, give a SharePoint developer site URL for debugging the app and select SharePoint-hosted as the hosting option. You might be asked to enter the credentials to the developer site. In my case I’m deploying the app to a SharePoint site on Office 365.
Right-click on the project in Solution Explorer and add a new List in the Office/SharePoint section of the Add New Item window. Name it BudgetRequest and select the Default (Custom List) template.
Add two columns to the list definition named BudgetAmount (Currency-Required) and ApprovalStatus (Single Line of Text). The list definition should look like Figure 1.  
Right-click on the project in Solution Explorer and add a new Workflow in the Office/SharePoint section of the Add New Item window. Name it BudgetApprovalWorkflow. Define it as a List Workflow in the SharePoint Customization Wizard window and click Next. Select the BudgetRequest list as the list to associate with and select the <Create New> option for the history list and task list as shown in Figure 2.   
Click Next and select “The workflow starts automatically when an item is created.” Click Finish to add the workflow. You’ll notice a WorkflowHistoryList and WorkflowTaskList also added to the Solution Explorer, as shown in Figure 3. The task list will create tasks for the approval process and history list will log messages for the workflow.  
Implementing Workflow Logic
The workflow designer window should appear open with one Sequence activity on it. First, you should get the value of the BudgetAmount column. Go to Toolbox window and drop a LookupSPListItem inside the Sequence activity. It will display an error, “Value for a required activity argument ‘ListId’ was not supplied.” Select LookupSPListItem and focus on the Properties window. Set the following values: ItemId: (current item), ListId: (current list) and DisplayName: LookupCurrentSPListItem.
Next, click the Get Properties link in the LookupCurrentSPListItem activity. Clicking that will add a new activity to the design canvas named GetDynamicValueProperties, as seen in Figure 4.
It will also change the Result property of the LookupCurrentSPListItem to dv_0.
Now click on the “Define…” text in the Properties property of the GetDynamicValueProperties activity. This will open the Properties window. Select “List Item of BudgetRequest” as the Entity Type. Select BudgetAmount for the path property and click the Populate Variables link. This will add a variable named BudgetAmount and assign the property value to it as shown in Figure 5. Then click OK.
So now you have a C#.NET variable named BudgetAmount that will have the list item value of the BudgetAmount field.
Now, select If activity from the Toolbox and drop it as the last activity. It will display a warning, saying condition is not specified for the If activity. In the Condition property of the If element, type BudgetAmount <= 100. This will do a check for the variable value to be less than or equal to 100.
Now you need to update the status column straight away as Approved by default if the value is less than or equal to 100. Drop an UpdateListItem activity from the toolbox to the Then section of the If activity. Set the properties of that activity as follows: ItemId: (current item), ListId: (current list) and DisplayName: ApprovedByDefaultUpdate.
Also, click on the “…” button related to ListItemPropertiesDynamicValue property of the ApprovedByDefaultUpdate activity in order to set the fields that needed to be changed.
Select the entity type as “List Item of BudgetRequest” and select ApprovalStatus for path and set its value to “Approved By Default” with the double quotes in it, as shown in Figure 6.
The flow of the workflow should look like Figure 7.
Now that you’re done with the logic for budget amounts less than or equal to $100USD, you’ll implement the logic for the Else branch of the If activity. This branch has a task approval that might take some time for approvers to approve. Therefore, at the beginning of the Else branch, you’ll update the approval status for “Pending approval from approvers.”
Right-click on the ApprovedByDefaultUpdate activity inside the Then branch and select copy option. Then right-click on the area with Drop activity here inside the Else branch and paste it. Now, select it and change the following properties:
Other properties do not need to be changed as the activity was copied and pasted.
The next step is to create a task to be approved by the Approvers SharePoint group. Select a Single Task activity from Toolbox and drop it below the PendingApprovalUpdate activity, as shown in Figure 8.
Select the SingleTask activity and change its properties as follows (see Figure 9):
This will rename the activity to ApproversApproval. Also note that this will set the Outcome property value to outcome_0. This is an integer value where value 0 represents approved and 1 represents rejected.

Printable Format
Microsoft is always soliciting feedback to help guide its development tooling efforts, most recently asking developers to weigh in with their requests to Santa for new debugging functionality in the form of a “debug-tastic” gift.
As we have reported before, Microsoft’s super-popular Visual Studio Code is the No. 1 code editor for multiple programming languages, and Google’s Go is on that list.
Microsoft’s C# programming language led the race to be named “programming language of the year” for 2022 by the TIOBE Index before being overtaken at the very end, but it’s expected to attain the throne this year.
What better way to learn about cutting-edge AI than creating a “copilot” assistant that can act as a mixologist, suggesting new cocktail recipes based on available ingredients?
New features and functionality boost productivity, C++ game development and .NET/C# (.NET MAUI).
Subscribe on YouTube
> More Webcasts
Problems? Questions? Feedback? E-mail us.

source


Comments

Leave a Reply

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