concept workflow in category sharePoint

This is an excerpt from Manning's book SharePoint 2010 Workflows in Action.
Workflows can bring value to a company by highlighting where in the business process the flow of work is currently executing. Workflows can also help a company automate their business processes. Consider again the expense report example. If your business process allows for all food expenses less than $100 to be automatically approved and sent to accounts payable for reimbursement, the workflow could manage this business logic and automatically approve the expense, without having to directly involve your manager.
Workflows are also good at managing parallel processes or multiple instances of work running at the same time, for example, in a manufacturing company. A car manufacturer could have a workflow for the engine construction, and another for the frame, and another for the interior. Then a parent workflow could manage all of the child workflows and start another process as soon as a dependent workflow finishes.
It’s easy to see that your investing in workflows would help to manage and automate your company’s business processes. Minimizing human dependencies in business processes always saves a company’s money. Because human costs are always the most expensive investment a company makes, let’s make the people in our organizations work as efficiently and effectively as possible. That’s what makes workflows such a great investment.
Workflows that track features for in-development software are highly desired. Clients often have software requirements written in a SharePoint-hosted document. A workflow is helpful in this scenario because it tracks the progress of the requirements document from start to finish. Additionally, the workflow enforces the final approval of that document before it’s given to the team. An example demonstrated in this chapter facilitates the development of this requirements document by automatically assigning tasks, tracking versions, and centralizing the document management. We accomplish this using one of the out-of-the-box workflows called the Three-state workflow.
This chapter will take you through the planning process for developing your first workflow and preparing a document library for a workflow in SharePoint. After we lay the foundation, we’ll walk through the steps for implementing and then maintaining the Three-state workflow. This establishes the foundation that other chapters will build on. We will demonstrate much of the basic nomenclature and many of the SharePoint workflow concepts. The Three-state workflow used in the requirements document example will serve as an excellent starting point for a developer new to workflows.
With the workflow added to the library, it’s time to start the workflow on a document. To start the Three-state workflow in this library, you’ll need to upload a document to your library and initiate the workflow on that document. You can either upload a document manually or you can create a new document from the Word client and save it to the library. Since you’ll be able to set the column data created in the previous section from within the Word client, you’ll create a new document from Word and start the workflow manually off that new document.
First, navigate to your Shared Documents document library to which you added the workflow in the beginning of section 2.1.3. You need to add a new document in this library and set metadata on that document. Follow the steps shown in table 2.4 to create and assign metadata to a new document.
Table 2.4. Adding a new document into a document library
Action
Steps
Result
Create a new document
- In Shared Documents, click on the New Document icon.
A new Word document will open. You’ll notice that the columns you added earlier are present at the top of the document in the Document Properties section (figure 2.8.). If the properties are not immediately noticeable, click on File > Info > Properties and select Show Document Panel from the dropdown. Set Assigned to and Status columns
- Enter Create in the Status column, add a user into the Assigned To column, and enter text in the body of the document.
The columns should be set similarly to those in figure 2.8. Save the document and provide a unique document file name NOTE: When you save the document, you’ll see a save window that has been updated for SharePoint and Office 2010. This window shows the location to which the file will be saved. This location will be defaulted to the document library from which you clicked the New Document link. You also have many other options, like Adding Tags, Thumbnails, and Map a Network Drive.
- In the Save dialog popup, change the filename to My First Workflow.
- Click the Save button.
After saving the document, you should be taken back to your SharePoint library, where you’ll see your new document (figure 2.9). Figure 2.8. A workflow can be started on a document in a document library. From the Word client, you can publish a document into a library along with metadata that is needed by the workflow (Assigned To column).
![]()
Figure 2.9. After the document is saved, it will appear in the browser showing the columns specified from the Word client.
![]()
Notice that the status of the document is set to Create and the value of the Assigned To column also is set. Now that you have your document, you need to initiate the workflow because you indicated that this workflow should be started manually. If you had set it to start automatically, it would have started as soon as you saved the document to this library.
Click on the arrow next to the name of the file and a context menu will drop down (figure 2.10). These are all of the actions available for this document but, for this scenario, you are only interested in the Workflows option. Click on Workflows.
Figure 2.10. The dropdown context menu gives the user quick access to all of the actions that can be performed on this document including starting a workflow on the document.
![]()
After you click on Workflows, you’ll see a screen with all of the available workflows that can be chosen for this library. Figure 2.11 shows the three defaults that come with SharePoint as well as the one you created earlier, My First Workflow.
Figure 2.11. Having clicked on Workflows, you’ll notice you can start a workflow or view any running or completed workflows.
![]()
Click on the My First Workflow link. You’ll briefly see a processing screen as the workflow starts. Then, SharePoint will return to your document library where you’ll see a new column called My First Workflow (figure 2.12).
This column will hold the status of the workflow. Because it started, it’s In Progress. You’ll also notice that the Status field in figure 2.12 has been grayed out indicating a workflow has updated this field.
SPWorkflow workflow = splistitem.Workflows[1];
Console.WriteLine("Titles of Workflow's Tasks:");
foreach (SPWorkflowTask task in workflow.Tasks)
{
Console.WriteLine(task["Title"].ToString());
}

This is an excerpt from Manning's book SharePoint 2007: Developer’s Guide to Business Data Catalog.
Tagging documents with metadata in SharePoint is central to how the many pieces of the pie (such as search, workflow, records management, and so on) work better. You can tag documents with free text fields and predefined numerical values, and you can even look up a list of values from another SharePoint list. If you were using a database and ASP.NET application to manage customers and orders, you would want to tag them by using a lookup field to live business data, and not have to duplicate that business data in a source such as a SharePoint list. Thankfully, this is where the Business Data column comes to the rescue. Figure 1.3 is a snapshot from Word 2007 that displays the Document Information panel (DIP) with a Business Data column in use.
Once the workflow is complete, like many custom items in SharePoint, you package the workflow as a feature. This provides flexibility in where the workflow can be deployed, and also allows site owners to activate or deactivate the workflow if required. A web solution package (WSP) can also be created, allowing the easy deployment of all the code files from development to staging, and from staging to production. This is a huge benefit when comparing Visual Studio to SharePoint Designer workflows, as SPD workflows are written to the content database and not to the file system.
Authoring the workflow in Visual Studio involves initially dragging activities onto the design surface, as well as setting properties such as the ID of each object and, more importantly, a correlation token. The correlation token is passed through the workflow from activity to activity, keeping track of where the workflow is in the process. Figure 8.7 displays the New Order State Activity. Within the activity is an OnWorkflowActivated event object that initializes the correlation token.
Once the workflow activities are in place and the properties are set, you need to write the code to perform the tasks required by the workflow.
The routine shown in listing 8.9 is the start of the workflow. As a new order is placed, the SharePoint object model is used to obtain the column values of the current SharePoint list item.
Listing 8.9. startOrderWorkflow method
private void startOrderWorkflow(object sender, ExternalDataEventArgs e) { strOrderTitle = workflowProperties.Item["Title"].ToString(); strCustomerEmailAddress = workflowProperties.Item["Customer: EmailAddress"].ToString(); SPListItem spListItem = workflowProperties.Item; BusinessDataField productField = (BusinessDataField)spListItem.Fields["Product"]; string idProductFieldName = string.Format("{0}_ID", productField.EntityName); string productIDEncoded = spListItem.GetFormattedValue(idProductFieldName); object[] productID = EntityInstanceIdEncoder.DecodeEntityInstanceId(productIDEncoded); ProductID = int.Parse(productID[0].ToString()); BusinessDataField customerField = (BusinessDataField)spListItem.Fields["Customer"]; string idCustomerFieldName = string.Format("{0}_ID", customerField.EntityName); string customerIDEncoded = spListItem.GetFormattedValue(idCustomerFieldName); object[] customerID = EntityInstanceIdEncoder.DecodeEntityInstanceId(customerIDEncoded); CustomerID = int.Parse(customerID[0].ToString()); strCustomerTitle = GetEntityColumnValue(customerField, CustomerID, "Title"); strCustomerFirstName = GetEntityColumnValue(customerField, CustomerID, "FirstName"); strCustomerLastName = GetEntityColumnValue(customerField, CustomerID, "LastName"); strCustomerCompanyName = GetEntityColumnValue(customerField, CustomerID, "CompanyName"); strCustomerAddressLine1 = GetEntityColumnValue(customerField, CustomerID, "AddressLine1"); strCustomerAddressLine2 = GetEntityColumnValue(customerField, CustomerID, "AddressLine2"); strCustomerCity = GetEntityColumnValue(customerField, CustomerID, "City"); strCustomerStateProvince = GetEntityColumnValue(customerField, CustomerID, "StateProvince"); strCustomerCountryRegion = GetEntityColumnValue(customerField, CustomerID, "CountryRegion"); strCustomerPostalCode = GetEntityColumnValue(customerField, CustomerID, "PostalCode"); StringBuilder stringBuilder = new StringBuilder(); stringBuilder.AppendLine(string.Format("{0} {1} {2}", strCustomerTitle, strCustomerFirstName,strCustomerLastName)); stringBuilder.AppendLine(","); if (strCustomerCompanyName != null) stringBuilder.AppendLine(strCustomerCompanyName); stringBuilder.AppendLine(","); if (strCustomerAddressLine1 != null) stringBuilder.AppendLine(strCustomerAddressLine1); stringBuilder.AppendLine(","); if (strCustomerAddressLine2 != null) stringBuilder.AppendLine(strCustomerAddressLine2); stringBuilder.AppendLine(","); if (strCustomerCity != null) stringBuilder.AppendLine(strCustomerCity); stringBuilder.AppendLine(","); if (strCustomerStateProvince != null) stringBuilder.AppendLine(strCustomerStateProvince); stringBuilder.AppendLine(","); if (strCustomerCountryRegion != null) stringBuilder.AppendLine(strCustomerCountryRegion); stringBuilder.AppendLine(","); if (strCustomerPostalCode != null) stringBuilder.AppendLine(strCustomerPostalCode); strCustomerFullAddress = stringBuilder.ToString(); }This particular workflow uses the Business Data object model to build a data table that’s populated by the products stored in the database. This data table is then referenced in situations such as checking stock levels. Listing 8.10 displays the code required to obtain line-of-business data from the database for use as part of the workflow.
Listing 8.10. Obtaining the LOB system data for the workflow
public DataTable GetEntityDataAsDataTable(BusinessDataField businessDataField, int parameter) { // Get the BDC Instance from the Business Data Field Parameter string instance = businessDataField.SystemInstanceName; // Get the BDC Entity from the Business Data Field Parameter string entity = businessDataField.EntityName; // Call method to set the SQL Provider Context SetProviderToServerContext(); // Impersonate the Current Windows user WindowsIdentity.GetCurrent().Impersonate(); NamedLobSystemInstanceDictionary sysInstances = ApplicationRegistry.GetLobSystemInstances(); LobSystemInstance lobSystemInstance = sysInstances[instance]; // Get desired Entity from the LobSystems Entities Entity lobEntity = lobSystemInstance.GetEntities()[entity]; // Use the Specific Finder to get out Entity Instance IEntityInstance entityInstance = lobEntity.FindSpecific(parameter, lobSystemInstance); return entityInstance.EntityAsFormattedDataTable; }