Export (0) Print
Expand All
Expand Minimize

Building Simple Custom Approval Workflows with InfoPath 2007 Forms

SharePoint 2007

Summary: Learn how to build a simple custom approval workflow by using Microsoft Office InfoPath 2007 forms.

Office Visual How To

Applies to: Microsoft Office SharePoint Server 2007, Microsoft Office InfoPath 2007

Patrick Tisseghem, U2U

July 2007

Overview

The interaction with users involved in a workflow in Microsoft Office SharePoint Server 2007 can be accomplished by using Microsoft Office InfoPath 2007 forms. The benefit of using Office InfoPath 2007 forms instead of ASPX pages is that the former can also be displayed by the 2007 Microsoft Office system clients. In this Office Visual How To, I'll work out a small custom workflow that shows the possible role of InfoPath 2007 forms.

Configure It

You can display four types of Office InfoPath 2007 forms in SharePoint Server 2007:

  • Association forms are displayed when the workflow template is associated with a list, a document library, or a content type. Use association forms to capture general parameter settings from the administrator.

  • Initiation (or instantiation) forms are displayed when a workflow instance starts, either as the result of a manual action by the user or triggered from an event (such as the addition of a document in the library).

  • Edit task forms are displayed when users are editing tasks assigned to them.

  • Modification forms are displayed when the predefined path is changed during the workflow.

This how-to discusses the first three types of forms.

Both the association and the initiation form must work with a main data source that is defined within the same namespace.

All forms must be published to a network location with the alternate access path left blank.

Code It

The following code examples show how to interact programmatically with the InfoPath 2007 forms that are displayed to the user during the various stages of the workflow life cycle.

Reading the Data from the Association and the Initiation Forms

In the workflow code-behind classes, a variable named workflowProperties of type Microsoft.SharePoint.Workflow.SPWorkflowActivationProperties is created and populated with Office SharePoint Server and workflow contextual data. The InitiationData property is exposed at this level, giving you access to the data entered in both the association and the initiation form. This data is typically processed during the activation of the workflow. The data is made available as an XML string that can be processed through the use of a System.Xml.XmlDocument instance and the execution of XPath statements, as shown in the following code example.

public SPWorkflowActivationProperties workflowProperties = 
   new SPWorkflowActivationProperties();

private void onWorkflowActivated1_Invoked
    (object sender, ExternalDataEventArgs e)
{
  XmlDocument doc = new XmlDocument();
  doc.LoadXml(workflowProperties.InitiationData);
  XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
  nsmgr.AddNamespace("my", "http://schemas.microsoft.com/office/infopath
/2003/myXSD/2007-05-08T04:21:20");
  this.manager = doc.SelectSingleNode

       ("/my:MyFields/my:ManagerName", nsmgr).InnerText;
  this.comments = doc.SelectSingleNode
       ("/my:MyFields/my:Comments", nsmgr).InnerText;
}

Reading the Data from the Edit Task Form

Data entered by the users in the edit task form is retrieved through the ExtendedProperties hash table that stores key/value pairs. The following code shows how the value of the TaskStatus field (one that is set when clicking the approve or reject button in the InfoPath form) and the comments entered by the approver in the form are retrieved.

private void onTaskChanged1_Invoked
   (object sender, ExternalDataEventArgs e)
{
  string taskstatus = 
      onTaskChanged1_AfterProperties1.ExtendedProperties
      ["TaskStatus"].ToString();
  this.taskDone = ((taskstatus == "Approved") || 
                   (taskstatus == "Rejected"));
  this.workflowProperties.Item["Comments by Approver"] = 
      taskstatus.ToUpper() + " : " +                    
      this.onTaskChanged1_AfterProperties1.ExtendedProperties
      ["ApproverComments"];
  this.workflowProperties.Item.Update();
  }

Deploy It

A custom workflow built with Microsoft Visual Studio 2005 is compiled into a .NET assembly and deployed in the global assembly cache. The workflow template is made available at the level of the site collection through a Feature. The InfoPath 2007 templates that are part of the workflow are published to a subfolder of the workflow Feature folder. You define this subfolder in the feature.xml file via the Property element, with the Key attribute set to RegisterForms.

<Feature  Id="{C6A3DB5C-B708-450d-A910-D268532101AC}"
 Title="Custom Timesheet Approval Feature"
 Description="This feature is a workflow that ..."
 Version="12.0.0.0" Scope="Site"
 ReceiverAssembly="Microsoft.Office.Workflow.Feature, 
   Version=12.0.0.0, Culture=neutral, 
   PublicKeyToken=71e9bce111e9429c" ReceiverClass=
"Microsoft.Office.Workflow.Feature.WorkflowFeatureReceiver" 
       xmlns="http://schemas.microsoft.com/sharepoint/">
 <ElementManifests>
    <ElementManifest Location="workflow.xml" />
 </ElementManifests>
 <Properties>
    <Property Key="GloballyAvailable" Value="true" />
<!-- Value for RegisterForms key indicates the path to 
the forms relative to feature file location -->
<!-- if you do not have forms, use *.xsn -->
    <Property Key="RegisterForms" Value="Forms\*.xsn" />
 </Properties>
</Feature>

In the elementmanifest file (the workflow.xml in the project), each of the Microsoft InfoPath 2007 templates is registered and connected to the proper phase in the workflow lifecycle in the MetaData section of the XML.

<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
 <Workflow Name="Custom Timesheet Approval"
  Description="This workflow allows users to approve 
timesheets."
  Id="{1D01E1F5-CC10-4568-9CC4-6CA4306C9F83}"
  CodeBesideClass="CustomApproval.Workflow1" 
  CodeBesideAssembly="CustomApproval, Version=1.0.0.0, 
Culture=neutral, PublicKeyToken=06edd8cb1926bc98"
  TaskListContentTypeId=
    "0x01080100C9C9515DE4E24001905074F980F93160"
  AssociationUrl="_layouts/CstWrkflIP.aspx"
  InstantiationUrl="_layouts/IniWrkflIP.aspx"
  ModificationUrl="_layouts/ModWrkflIP.aspx"
  StatusUrl="_layouts/WrkStat.aspx">
  <Categories/>
   <!-- Tags to specify InfoPath forms for the workflow; delete 
tags for forms that you do not have -->
   <MetaData>
     <Association_FormURN>urn:schemas-microsoft-
com:office:infopath:CustomApprovalAssoc:-myXSD-2007-05-08T04-21-
20</Association_FormURN>
     <Instantiation_FormURN>urn:schemas-microsoft-
com:office:infopath:CustomApprovalInit:-myXSD-2007-05-08T04-21-
20</Instantiation_FormURN>
    <Task0_FormURN>urn:schemas-microsoft-
com:office:infopath:CustomApprovalTask:-myXSD-2007-05-08T05-07-
25</Task0_FormURN>
    <AssociateOnActivation>false</AssociateOnActivation>
   </MetaData>
  </Workflow>
</Elements>
See It

2007InfoPath Building Custom Approval Workflows

Watch the Video

Length: 10:00 | Size: 12.2 MB | Type: WMV file

Explore It

Community Additions

ADD
Show:
© 2014 Microsoft