Walkthrough: Creating a Workflow Application Using Exchange Server

This content is no longer actively maintained. It is provided as is, for anyone who may still be using these technologies, with no warranties or claims of accuracy with regard to the most recent product version or service release.

In this walkthrough, you will be guided through the process of creating a team application that uses a Microsoft Exchange 2000 public folder, a Microsoft® Outlook® form, and workflow to move a purchase order through a simplified approval process. The workflow created in this example uses values in an Outlook form to determine how the order is sent through the various states in the process. In addition, it uses script to notify users automatically that the order has been submitted and reviewed.

Introduction

Does your department require an application for tracking reports or other items in Exchange? Would you like to automate reminders and notifications based on items in an Exchange folder? You can create such a team application quickly using the Workflow Designer for Exchange 2000 Server.

Using the Workflow Designer for Exchange Server, you can add a workflow process to any items in Exchange folders. A workflow process automates control of items in a folder by moving them through a series of states depending on the transitions and conditions you specify. In addition, you can enhance your workflow application by scripting your own functions that run when the transitions are performed. Your workflow process can track any type of item available in Outlook, such as messages, forms, or tasks, as well as any file, such as documents (.doc), spreadsheets (.xls), or text files (.txt).

The user interface for a workflow process determines how the user submits the item that moves through the process. When designing the user interface for your application, you have a wide variety of options. You can integrate Microsoft®Visual Basic® applications, Web pages, and any other front-end that can access the Exchange folder objects.

By completing this walkthrough, you can create a team application that uses a public folder, an Outlook form, and a workflow to move a purchase order through a simplified approval process.

Because this walkthrough is designed to highlight how to create a simple workflow process, not the user interface for the application, the user interface provided in this example is a simple Outlook form intended only to show how to automate your workflow. The example application created in this walkthrough is not designed to be a real-world application.

Preparing Exchange for a Team Application

To follow along with this walkthrough, you must have access to an Exchange 2000 server, the Workflow Designer for Exchange Server, and the appropriate permissions on the server. For more information about preparing Exchange for a workflow process, see the documentation in the Exchange SDK download, available from the Microsoft® Developer Network (MSDN®) Exchange Server Developer Center at http://msdn.microsoft.com/exchange/.

Creating a team application requires the following components:

  • Server: Microsoft Windows 2000 with Exchange 2000 Server as the back-end for the application.
  • Development tool: Workflow Designer for Exchange Server as the tool for creating the workflow process. This tool can reside on either a client computer with access to the server or the server itself.

Before you can add workflow to your folder, you must have the following:

  • A public folder in Exchange 2000 Server. For example, you could add a folder called PurchaseOrders to Public Folders.
  • Workflow design permissions on the Exchange 2000 Server. By creating the folder in Outlook, you automatically have the owner permissions required. For additional information about setting the permissions required for creating workflow processes, see the documentation in the Exchange SDK download, available from the MSDN Exchange Server Developer Center at http://msdn.microsoft.com/exchange/.
  • A set of items your team wants to track. Using this walkthrough, you can create an Outlook form that shows simple controls that make it possible for users to enter and to track Exchange items. In addition to Outlook, you can use .asp pages, a Visual Basic form, or data access pages as the user interface for your folder and workflow.
  • A plan for the business process that tracks an item. Using this walkthrough, you can create a simplified workflow process for a purchase order form in Outlook.

To enable Script Execute Permissions

  1. Open the Exchange System Manager on the Exchange 2000 Server.
  2. From the Servers menu, select your computer | Protocols | HTTP, and then Exchange Virtual Server.
  3. Right-click Public, and select Properties.
  4. In the Access tab, select Scripts in the Execute Permissions.

To add the yourself to the two COM+ Workflow Roles

  1. Open the Component Services on the Exchange 2000 Server.
  2. From the Component Services menu, select Computers | My Computer | COM+ Applications | Workflow Event Sink, and then Roles.
  3. Add yourself to the Can Register Workflow and Privileged Workflow Authors roles.

Creating the User Interface in Outlook

You can use a workflow process created using the Workflow Designer for Exchange Server to track any type of item available in Exchange 2000 Server. This walkthrough demonstrates how to create a workflow team application using a simplified purchase order implemented as an Outlook Post form. The application created in this example is designed only to illustrate how you can integrate a form with a workflow, and it lacks many of the features a real purchase order would require. In this example, the user interface is created first, because the workflow and its script require the values obtained through controls on the user interface form.

The first step is to create a public folder that will be used to post the purchase orders and that will be where you will create the workflow project. For example, you could add a folder called PurchaseOrders to Public Folders.

To add a public folder using Microsoft Outlook

  1. Start Outlook, and display the folder list if it is not shown already by selecting Folder List from the View menu.
  2. Expand the Public Folders node, and then expand All Public Folders.
  3. Right-click All Public Folders, and select New Folder.
  4. Enter a name for the new folder, such as PurchaseOrders.
  5. Navigate to the folder by selecting the folder you just created.

Sometimes it is not convenient to set up Outlook simply to create folders. You also can use Outlook Web Access by using Microsoft® Internet Explorer to navigate to the Exchange Server.

To add a public folder using Internet Explorer

  1. Start Internet Explorer.
  2. In the Address Bar, type http://server name/Public to display Outlook Web Access.
  3. The public folders will be displayed in the Folder List on the left.
  4. Right-click the Public Folders node, and select the New Folder option. The currently logged on user will be set as the folder owner automatically. If you create a sub folder under an existing folder, both the currently logged in user and the owner of the main folder will have owner permissions on the subfolder.

When you have a folder created, it is a good idea to create a Network Place for the folder or at least for the http://MyServer/Public location.

To create the user interface for this walkthrough, you create two different views of the Post form in Outlook:

  • **Edit Compose Page view   **This view is used by the person submitting the purchase order to input data in the expense fields. It appears when the report is created or composed. You are shown this view automatically when you are designing a form. The text boxes on the form provide values to the script used in the workflow.
  • **Edit Read Page view   **This view is used by a manager to approve or reject the purchase order. It contains an approval and rejection combo box in addition to the expense fields.

By completing this walkthrough, you create and publish an Outlook form and set it as the default form for the folder. The fields in this form are used by the workflow process described in Planning the Workflow later in this walkthrough.

To create the Edit Compose Page view of the Outlook form

  1. In Outlook, open the public folder to which you want to add the workflow.
  2. From the Tools menu, select Forms, and then select Design a Form.
  3. In the Design Form dialog box, select Post, and then select Open.
  4. In the form, resize the message area to create space for the purchase order fields.
  5. Right-click the form, and then click Control Toolbox.
  6. Add the following label controls to the form.
    Control Properties
    Office supply Caption: Office supply
    PC software Caption: PC software
    PC hardware Caption: PC hardware
    Misc. Caption: Misc
    Total Expense Caption: Total Expense
  7. Add the following controls to the form to enter and display the expense values. To modify the associated properties, right-click the control and then click Properties. On the Value tab, click the New button to specify a user-defined field that the workflow script can reference.
    Control Properties
    Office supply textbox Name: Office supply
    Type: Currency
    PC software textbox Name: PC software
    Type: Currency
    PC hardware textbox Name: PC hardware
    Type: Currency
    Misc. textbox Name: Misc
    Type: Currency
    Total Expense label (A label box is used because the user cannot change the value.) Name: TotalExpense
    Type: Currency

    Caption: (Null)
    Select Set the initial value of this field to, and enter this formula for the initial value: [Office supply] + [PC software] + [PC hardware] + [Misc].

    Click Calculate this formula automatically.

    Submit check box Name: Submit
    Type: Yes/No
    Format: True/False
    Check Set the initial value to this field to, and enter No for the initial value.

Edit Compose Page view of the Outlook form

Aa166226.deformdesignview(en-us,office.10).gif

To create the Edit Read Page view of the Outlook form

  1. In the Edit Compose Page view of the form, select and copy all of the controls you added to the form.
  2. Above the tabs for the form, click Edit Read Page.
  3. Resize the message area to create space for the controls, and then paste them in place.
  4. Add a combo box control to the form, right-click the combo box control, and then click Properties. Click New to specify a user-defined field, and set the properties shown in the following table.
    Property Value
    Name Approved
    Type Text
    Format Text
    List type Dropdown
    Possible values Approve;Reject;Pending
    Initial value Pending
  5. Add a label with the caption Approved? next to the combo box control.

To make the form available to the workflow, you must publish it. In addition, you can specify it as the default form for the folder. You must have editor, publishing editor, or owner permissions to add forms to a public folder.

**Note   **Do not enable workflow on a folder before publishing the form. If you do, you might get a permissions error even though you are the owner of the folder.

To publish the form and specify it as the default form for the folder

  1. On the Tools menu, select Forms, and then select Publish Form As.

  2. In the Publish Form As dialog box, enter a name for the form, and then select Publish.

    **Note   **The name you provide is the name that the user sees when choosing a form to post in the folder. For the purchase order example, you can name it "PurchaseOrders."

  3. Close the form, and select No when prompted to save your changes (because the form is published already, you are not required to save your changes).

  4. In the Folderlist, right-click the folder to which you want to add workflow, and then click Properties.

  5. In the When posting to this folder, use combo box, select the form you created, and then click OK.

Your form will appear when a user decides to post to that folder.

Planning the Workflow

To learn about using the Workflow Designer for Exchange Server, you can use this walkthrough to create a workflow process that includes a series of states through which a purchase order item moves and to define the transitions that move the item.

The Workflow Process

The workflow process in this example is based on the following scenario. An employee creates a purchase order form in a PurchaseOrders folder located in the Public Folder. When the order is created, the item enters the Draft state. To move into the Submit to Manager state, the employee must select a Submit check box and save the order. Otherwise, the employee can modify and save the item as many times as required while the Submit check box is not selected. When the Submit check box is selected, the order is moved automatically to the Submit to Manager state. When the order is moved to the Submit to Manager state, a manager can either approve or reject the order.

If the manager selects "Approve" in the Approved check box, then the purchase order moves into the Approved state. If the manager selects "Reject," then the purchase order returns to the Draft state.

Items in the Approved state or Draft state can be deleted. Items in the Submit to Manager state cannot be deleted, because a Delete transition is not specified for it.

Each workflow state specifies a step in the workflow process. Details about the specifications for the states, transitions, events, and script used to implement the workflow process in this walkthrough are provided in the procedures in the Creating a Workflow Process section later in this walkthrough.

The purchase order workflow example includes the following states:

  • Draft
  • Submit to Manager
  • Approved

Transitions are used to transition an item from one state to another. Each state typically has at least two transitions. One transition moves the item into the state, and one transition moves the item out of the state. Each transition has a condition associated with it that is used in determining which state is transitioned to.

States can have any number of transitions. For example, the Submit to Manager state has four transitions: Manager Approved, Manager Rejected, Manager Pending, and Expiry. Transitions are triggered by a change to the item in the folder. When triggered, the condition is evaluated. If the condition evaluates to True, then the script associated with the transition is executed. In this example, the workflow for the purchase order has the following transitions:

  • **Create Draft   **The condition for this transition is evaluated after the item in the folder is created. The value of the Submit check box control must be False.
  • **Create and Submit   **The condition for this transition is evaluated after the item in the folder is created. The value of the Submit check box control must be True.
  • **Edit   **This transition occurs when the item in the folder is edited in draft state.
  • **Manager Submission   **The condition for this transition is evaluated after the item in the folder is saved. The value of the Submit check box control must be True.
  • **Manager Approved   **The condition for this transition is evaluated after the item in the folder is read while in the Submit to Manager state. In this example, the value of the Approved combo box control on the Outlook form must be Approve.
  • **Manager Rejected   **The condition for this transition is evaluated after the item in the folder is read while in the Submit to Manager state. In this example, the value of the Approved combo box control on the Outlook form must be Reject.
  • **Manager Pending   **The condition for this transition is evaluated after the item in the folder is read while in the Submit to Manager state. In this example, the value of the Approved combo box control on the Outlook form must be Pending.
  • **Expiry   **This transition occurs after the report has been in the Submit to Manager state for a user-defined period of time without being opened.
  • **Delete1 and Delete2   **These transitions make it possible for the report to be deleted while in either the Draft or Approved state. Items in the Submit to Manager state cannot be deleted.

The Workflow Scripts

In your script associated with each transition, you can call user-defined functions. This example uses the following custom functions, along with a set of standard functions that are called from the custom functions. The script used for each function is provided in the Creating the Script Procedures section later in this walkthrough.

Custom Functions

  • SubmitPurchaseOrder   Called from the Manager Submission transition when a purchase order is submitted to a manager. Sends an e-mail message to the approval authority containing a link to the posted item awaiting approval.
  • ApprovePurchaseOrder   Called from the Manager Approved transition and Manager Rejected transition when the manager either approves or rejects a purchase order. Checks the value on the Outlook form for approved/rejected status and sends notification e-mail to the person who submitted the form. If the item is rejected, it also resets the Submit property value and returns it to the Draft state.
  • ExpirePurchaseOrder   Called from the Expiry transition when a purchase order remains submitted to a manager for seven days. Sends a reminder message to the approval authority that the purchase order must be approved.

Standard Functions

  • AddFieldToCurrentMsg   Called from the Create Draft and Create and Submit transitions. Dynamically adds a new property to the item. This is used to add the Approval Authority property, returned from the GetUserManager script, and the SubmittedBy property created in the WorkflowSessionSender script to the item.
  • ClearPermissions   Clears all previously set permissions. This is used to change the permissions for the person who submitted the form to read-only after the item has been submitted for approval.
  • GetPathAndSubject   Returns the path to the public folder and the subject of the current item. This makes it possible for the SubmitPurchaseOrder and ApprovePurchaseOrder functions to add a link to the specific purchase order item in the e-mail message body.
  • GetUserManager   Returns application-defined data. In this example, it returns the e-mail alias of the user's manager.
  • SendMessage   Sends an e-mail message in response to workflow transitions.
  • SetCurrentMessageField   Sets the value of an existing property on an item. This is used to reset the Submit property value when a purchase order is rejected.
  • SetPermissions   Sets permissions making it possible for only the approval authority to make changes to the item and sets read-only permissions to all others.

Creating a Workflow Process

To create a workflow process, you open the Workflow Designer for Exchange Server and specify the states, transitions, and events. Creating a workflow includes the following main tasks:

  • Adding a workflow to a public folder.
  • Adding states and transitions that the item will move through.
  • Adding events that determine how the item is handled and specify conditions for special handling.
  • Adding script to automate handling of items, sending notifications, or calling other functions you want to use. Activating the workflow on the folder.

In the sections that follow, the generic procedure describing how to accomplish the designated task is followed by specific instructions for creating the purchase order example.

Creating the Workflow Process

After planning your workflow, you can create the workflow on a public folder. The Workflow Designer for Exchange Server works directly on the folder, so you must create the folder before opening the Workflow Designer for Exchange Server. The Workflow Designer for Exchange Server also automatically generates a new empty workflow process, containing only starting and ending shapes.

To create a project and workflow process

  1. From the Start menu, select Programs, select Microsoft Office XP Developer, and then Microsoft Development Environment.

  2. From the File menu, select New, and then select Project. The New Project dialog box is displayed.

  3. Select Office Developer Projects.

  4. Under Templates:, select the Exchange Workflow Project icon.

  5. In the New Project dialog box, enter a name for the new project and a folder location in the form of a URL, for example http://myserver/public/myfolder. The folder location will default to the last folder opened.

    **Note   **You cannot browse to an http location unless you have created a network place.

  6. Click OK. An empty workflow process is created, and the workflow diagram is displayed on the Design Surface.

    **Caution   **Each Exchange folder location can contain only one Exchange Workflow Project. Each project can contain multiple workflow processes.

Adding States to the Workflow Diagram

You can add states to the workflow diagram representing each stage through which an item might pass during processing. In this example, the states used in the purchase order workflow diagram are: Draft, Submit to Manager, and Approved.

To add a workflow state

  1. Select the workflow process from the Solution Explorer.
  2. From the View menu, select Open, or double-click the workflow process.
  3. Select the State tool in the Toolbox, and click the design surface. The State shape will be created at the location of the click. Properties, such as name and caption, can be set for the state shape in the Property Page or Properties Grid.
  4. To connect this new state with existing states, you must create a new workflow transition.

To create the workflow diagram

Add three states with the following properties.

  • State Name: Draft
    Property Value
    Name Draft
    Caption Draft
  • State Name: Submit to Manager
    Property Value
    Name SubmitToManager
    Caption Submit to manager
    ExpiresIn 7 days
  • State Name: Approved
    Property Value
    Name Approved
    Caption Approved

Adding Transitions to States

You can add events to states, such as Enter or Edit, and add transitions, such as Submit or Reject, to establish the relationships between the states in the workflow and to provide a place to add script for automating the workflow. In addition, workflow transitions can be used to evaluate conditions and to trigger scripts that automate your workflow process.

To add a transition to a state in the Workflow Designer for Exchange Server

  1. From the View menu, select Open, or double-click the workflow process.

  2. Select the Transition tool, click the shape at the start of the transition, and then drag to the shape at the end of the transition. The transition is created between the two shapes. Properties, such as name, caption, and type of transition, can be set in the Property Page or Properties Grid. By default, the transition will be a Change transition type. That means a change to an item will cause the transition.

    If you have multiple states to connect, you can turn on sticky mode. Press CTRL, click the transition shape, and then drag to create transitions between the states. Press ESC to turn off sticky mode.

    **Note   **Sticky mode can be turned on for any of the workflow tools.

    **Caution   **If you drag one transition directly over anther transition, it might appear that they have merged into one transition, however both transition still exist.

To complete the purchase order example, add the transitions, conditions, and script specified in the following procedure. This script calls user-defined functions specified in the Creating Script Procedures section later in this walkthrough.

If a state has multiple transitions exiting, the next state of the item might vary dependent on the order by which the transitions are evaluated. You can control the order by which transitions are evaluated by setting the evaluation order. To set the evaluation order for transitions exiting a state, select the state on the Design Surface, and then right-click and select Properties. On the Properties dialog box, select the Transitions tab.

To create a functional workflow for the purchase order example

Add the following transitions to the workflow diagram.

  • Transition Name: CreateDraft
    Property Value
    Caption Create Draft
    Beginning State Start1
    Ending State Draft
    Type Create
  • Transition Name: CreateAndSubmit
    Property Value
    Caption Create and Submit
    Beginning State Start1
    Ending State SubmitToManager
    Type Create
  • Transition Name: Edit
    Property Value
    Caption Edit
    Beginning State Draft
    Ending State Draft
    Type Change
  • Transition Name: ManagerSubmission
    Property Value
    Caption Manager Submission
    Beginning State Draft
    Ending State SubmitToManager
    Type Change
  • Transition Name: Delete1
    Property Value
    Caption Delete1
    Beginning State Draft
    Ending State End1
    Type Delete
  • Transition Name: ManagerRejected
    Property Value
    Caption Manager Rejected
    Beginning State SubmitToManager
    Ending State Draft
    Type Change
  • Transition Name: ManagerPending
    Property Value
    Caption Manager Pending
    Beginning State SubmitToManager
    Ending State SubmitToManager
    Type Change
  • Transition Name: ManagerApproved
    Property Value
    Caption Manager Approved
    Beginning State SubmitToManager
    Ending State Approved
    Type Change
  • Transition Name: Expiry
    Property Value
    Caption Expiry
    Beginning State SubmitToManager
    Ending State SubmitToManager
    Type Expiry
  • Transition Name: Delete2
    Property Value
    Caption Delete2
    Beginning State Approved
    Ending State End1
    Type Delete

Creating the Script Procedures

Each workflow transition has two associated script functions — a validation script procedure and a transition script procedure. When a workflow transition is triggered, the workflow engine invokes the script engine and executes the validation function. If this validation function returns True, the associated transition and transition script procedure are executed.

To Access the Script Editor and add script

  1. From the View menu, select Code, or double click the transition in the Design Surface where you want to add code.

  2. Create a procedure, including Sub and End Sub tags, or a function, including Function and End Function tags.

    Note   Success entries must be enabled to use the Audit trail for debugging. For details, see the To enable script debugging and success entries procedure later in this walkthrough.

Many of the procedures and functions in these examples refer to and rely on each other. When using the example script, be sure to verify that all parameters and dependent functions exist in the workflow script.

  1. Add the following script event handlers to each transition:
    • Transition Name: CreateDraft
      Property Value
      Event OnCreate
      Script AddFieldToCurrentMsg "SubmittedBy", WorkflowSession.Sender AddFieldToCurrentMsg "ApprovalAuthority", ""
    • Transition Name: CreateAndSubmit
      Property Value
      Event OnCreateValidate
      Script CreateandSubmit_OnCreateValidate = CBOOL(WorkflowSession.Fields("Submit").Value)
      Property Value
      Event OnCreate
      Script AddFieldToCurrentMsg "SubmittedBy", WorkflowSession.Sender AddFieldToCurrentMsg "ApprovalAuthority", "" SubmitPurchaseOrder
    • Transition Name: ManagerSubmission
      Property Value
      Event OnChangeValidate
      Script ManagerSubmission_OnChangeValidate = CBOOL(WorkflowSession.Fields("Submit").Value)
      Property Value
      Event OnChange
      Script SubmitPurchaseOrder
    • Transition Name: ManagerApproved
      Property Value
      Event OnChangeValidate
      Script ManagerApproved_OnChangeValidate = CBOOL(WorkflowSession.FIelds("Approved").Value = "Approve")
      Property Value
      Event OnChange
      Script ApprovePurchaseOrder(TRUE)
    • Transition Name: ManagerRejected
      Property Value
      Event OnChangeValidate
      Script ManagerRejected_OnChangeValidate = CBOOL(WorkflowSession.Fields("Approved").Value = "Reject")
      Property Value
      Event OnChange
      Script ApprovePurchaseOrder(FALSE)
    • Transition Name: Expiry
      Property Value
      Event OnExpire
      Script ExpirePurchaseOrder
  2. Set the evaluation order for transitions as follows:
    • State Name: Start1
      Order Transition
      1 CreateAndSubmit
      2 CreateDraft
    • State Name: Draft
      Order Transition
      1 ManagerSubmission
      2 Delete1
      3 Edit
    • State Name: SubmitToManager
      Order Transition
      1 ManagerApproved
      2 ManagerRejected
      3 Expiry
      4 ManagerPending

AddFieldToCurrentMsg

To use this code, create the AddFieldToCurrentMsg script in the Script Editor. This script is called during the Create Draft and Manager Submission transitions, adding the SubmittedBy and ApprovalAuthority properties to the SubmitPurchaseOrder script.

Sub AddFieldToCurrentMsg (strName, Value)
   Dim FieldType
   FieldType = 8 ' BSTR
   WorkflowSession.Fields.Append CStr(strName),FieldType, , , CStr(Value)
   WorkflowSession.Fields.Update
End Sub

ApprovePurchaseOrder

To use this code, create the ApprovePurchaseOrder script in the Script Editor.

The ApprovePurchaseOrder is dependent on the GetUserManager (approval authority), GetPathAndSubject, SendMessage, and SetCurrentMessageField scripts. If the parameter is True, then the purchase order is approved, and the e-mail message indicates that it has been approved. If it is False, the e-mail message indicates that the report has been rejected.

Sub ApprovePurchaseOrder(bApproved)
   Dim strSMTPAddress
   Dim strBody
   Dim strSubject
   Dim strMessageSubject
   Dim strApproved  
   Dim strOutlookURL

strSMTPAddress = WorkflowSession.Fields("urn:schemas:mailheader:from").Value
strMessageSubject = WorkflowSession.Fields("urn:schemas:mailheader:subject").Value
   If bApproved = True Then
      strApproved = "APPROVED"
   Else
      strApproved = "REJECTED"
   End If

   strSubject = strApproved & " -- Purchase Order " & strMessageSubject

   strSubject = "Your purchase order has been "  & strApproved & " (" & strMessageSubject & ")"

   strOutlookURL = GetPathAndSubject
   strBody = "Your purchase order has been " & strApproved  & " by " & WorkflowSession.Fields("ApprovalAuthority").Value & vbCRLF & vbCRLF
   strBody = strBody & "View the purchase order by clicking here. <outlook://public folders/All public Folders/"  & strOutlookURL & ">" & vbCRLF & vbCRLF
   strBody = strBody & "------------------------------------" & vbCRLF
   strBody = strBody & "Purchase Starting State: " & WorkflowSession.StateFrom & vbCRLF
   strBody = strBody & "Purchase Ending State: " & WorkflowSession.StateTo

   SendMessage strSMTPAddress, strSubject, strBody

   ' Reset submit when rejected
   If bApproved = False Then
      SetCurrentMessageField "Submit", False
   End If

End Sub

ClearPermissions

To use this code, create the ClearPermissions script in the Script Editor. This script is called when the SubmitPurchaseOrder is executed. After the purchase order is submitted, the permissions for the person who submitted the form change to read-only.

Sub ClearPermissions()
   WorkflowSession.ItemAuthors.Clear
End Sub

ExpirePurchaseOrder

To use this code, create the ExpirePurchaseOrder script in the Script Editor. This script sends e-mail to the approve authority indicating that a purchase order has been awaiting approval and has now expired.

The ExpirePurchaseOrder is dependent on the GetUserManager (approval authority) and SendMessage scripts.

Sub ExpirePurchaseOrder()
   Dim strSubject
   Dim strBody

   strSubject = "Action Required: Purchase Order Approval Request"
   strOutlookURL = GetPathAndSubject
   strBody = "There is a purchase order waiting for your approval."  & vbCRLF & vbCRLF
   strBody = strBody & "View the purchase order by clicking here.  <outlook://public folders/All public Folders/"  & strOutlookURL & ">" & vbCRLF & vbCRLF
   strBody = strBody & "----------------------------------------" & vbCRLF
   strBody = strBody & "Submitted: " & Cstr(Date) & "  " & Cstr(Time) & vbCRLF
   strBody = strBody & "Purchase ending state: " & WorkflowSession.StateTo
   strSMTPAddress = WorkflowSession.Fields("ApprovalAuthority").Value
   SendMessage strSMTPAddress, strSubject, strBody
End Sub

GetPathAndSubject

To use this code, create the GetPathAndSubject script in the Script Editor. This script makes it possible for the SubmitPurchaseOrder and ApprovePurchaseOrder functions to add a link to the purchase order in the e-mail message body.

Function GetPathAndSubject()
   Dim iPos
   Dim iPathLength
   Dim strPath
   Dim strSubject

   strPath = WorkflowSession.Fields("DAV:parentname")
   strSubject = WorkflowSession _
      .Fields("http://schemas.microsoft.com/mapi/proptag/0x0037001F") _
      .Value

   iPathLength = len(strPath)

   iPos = Instr(strPath, "backofficestorage") + 18
   iPos = Instr(iPos,strPath, "/") + 1
   iPos = Instr(iPos,strPath, "/")

   iPathLength = iPathLength - iPos
   GetPathAndSubject = Right(strPath, iPathLength) & "/~" & strSubject
   
End Function

GetUserManager

To use this code, create the GetUserManager script in the Script Editor. This script is called in the ApprovePurchaseOrder, ExpirePurchaseOrder, and SubmitPurchaseOrder scripts.

Function GetUserManager(strUserAddress) 
   Dim mgrDN

   With WorkflowSession 
      mgrDN = .GetUserProperty(strUserAddress, "manager",0) 
      GetUserManager = .GetUserProperty(mgrDN, "mail", 1) 
   End With 

End Function

SendMessage

To use this code, create the SendMessage script in the Script Editor. This script is called in the ApprovePurchaseOrder, ExpirePurchaseOrder, and SubmitPurchaseOrder scripts to send an e- mail message. The parameters used specify to whom the message should be sent, along with the subject and body of the e-mail message.

Sub SendMessage (strTo, strSubject, strBody)

   Set oMsg = CreateObject("CDO.Message")

   oMsg.To = strTo
   oMsg.From = WorkflowSession.Sender
   oMsg.Subject = strSubject
   oMsg.TextBody = strBody
   oMsg.Send

   Set oMsg = Nothing

End Sub

SetCurrentMessageField

To use this code, create the SetCurrentMessageField script in the Script Editor. This script is called in the ApprovePurchaseOrder and SubmitPurchaseOrder scripts to reset the Submit property value when moving from Approved to Rejected. The parameters specify the name of the property to set and the value to which to set it.

Sub SetCurrentMessageField(strName, Value)
   WorkflowSession.Fields(CStr(strName)).Value = Value
   WorkflowSession.Fields.Update
End Sub

SetPermissions

To use this code, create the SetPermissions script in the Script Editor. This script is called in the SubmitPurchaseOrder script; in addition, you must call ClearPermissions prior to setting permissions. The parameter specifies what user (using the user's SMTP e-mail address) should have author access to the item.

Sub SetPermissions (strSMTPAddress)
   WorkflowSession.ItemAuthors.Add strSMTPAddress, 0
End Sub

SubmitPurchaseOrder

To use this code, create the SubmitPurchaseOrder script in the Script Editor. This script sends the approval authority an e-mail message containing a link to the specific purchase order item awaiting approval. The approval authority is determined after the total expense is evaluated. If the total expense is less than $1000, the approval authority is the manager of the person who submitted the form. If the total is greater than $1000, the approval authority is the manager's manager. In both instances, the GetUserManager script is called and returns an e-mail alias for the appropriate authority.

The SubmitPurchaseOrder is dependent on the AddFieldToCurrentMsg, GetUserManager (approval authority), GetPathAndSubject, SendMessage, ClearPermissions, SetPermissions, and SetCurrentMessageField scripts.

The ClearPermissions and SetPermissions scripts are required for the manager to be able to approve or reject the purchase order.

Sub SubmitPurchaseOrder()
   Dim strSMTPAddress
   Dim strSubject
   Dim strBody
   Dim strOutlookURL

   strSubject = "Action Required: Purchase Order Approval Request"
   strOutlookURL = GetPathAndSubject
   strBody = "There is a purchase order waiting for your approval."  & vbCRLF & vbCRLF
   strBody = strBody & "View the purchase order by clicking here. <outlook://public folders/All public Folders/" & strOutlookURL & ">" & vbCRLF & vbCRLF
   strBody = strBody & "----------------------------------------" & vbCRLF
   strBody = strBody & "Submitted: " & CStr(Date) & "  " & CStr(Time) & vbCRLF
   strBody = strBody & "Purchase ending state: "  & WorkflowSession.StateTo

   strSMTPAddress = GetUserManager(WorkflowSession.Sender)

   If WorkflowSession.Fields("TotalExpense").Value > 1000 Then
      strSMTPAddress = GetUserManager(strSMTPAddress)
   End If

   SendMessage strSMTPAddress, strSubject, strBody

   SetCurrentMessageField "ApprovalAuthority", strSMTPAddress
   ClearPermissions
   SetPermissions(strSMTPAddress)

End Sub

Activating the Workflow Process

After you have completed your workflow, you can implement it on the folder. Before workflow can be enabled, a default workflow process must be set. When workflow is enabled for a folder, items in the folder use the default workflow.

**Note   **Finish your work on the forms before activating the workflow. After the workflow is activated, you might find that you no longer have permissions to save your design changes to the folder.

To set the default workflow process

  1. Select the workflow project from the Solution Explorer.

  2. From the View menu, select Property Pages. You also can right-click the project in the Solution Explorer, and select Properties from the context menu.

  3. In the Default Workflow Process: text box, enter the name of the workflow process you want to set as the default, or select the "" button to browse for other available workflow processes. When browsing for the desired workflow process, only *.wfd files will be filtered. In addition, the name of the default workflow process can be entered into the Property Grid, in the DefaultWorkflow property field.

    If the selected workflow process is not in the current folder, a URL including the location and name of the process will be entered in the text box. It is important to note that external default workflow process will not be displayed in the Solution Explorer; it is displayed only in the Properties Grid.

    **Note   **The default workflow process must reside on the same server as the Exchange Server project folder.

To enable a workflow process

  1. From the View menu, select Property Pages. You also can right-click the project in the Solution Explorer, and select Properties from the context menu.
  2. To enable workflow, select the Workflow enabled on this folder check box, or in the Property Grid, set the WorkflowEnabled property to True.
  3. Click OK.

Using the Workflow Process

When you have completed the creation of the Outlook form and the workflow process, you can test the functionality of your application.

  1. In Outlook, click the PurchaseOrder folder.

  2. Click New to display the Purchase Order form.

  3. Enter a name for the purchase order in the Subject field.

  4. Enter values for the purchase order values. The Total field will calculate the total automatically.

  5. Select the Submit check box to submit your purchase order to a manager for approval.

    An e-mail message is sent to your manager indicating that a purchase order is awaiting approval.

    Note   If an error message appears stating that you do not have permission to save the item, make sure that you have manager information specified in the Exchange address book. If your e-mail alias does not have a manager identified, an error will occur when you try to send e-mail to the manager.

  6. Edit the newly added purchase order by double-clicking it. The Purchase Order form now shows the Approved? combo box.

  7. In the Approved? combo box, click Approve, and then on the File menu, click Save.

An e-mail message is sent to you informing you that your purchase order has been approved.

See Also

Developing Office Developer Applications | Developing Workflow Applications for Exchange Server | Building a Workflow Process for Exchange Server