Professional Documents
Culture Documents
5. Designing the workflow by using Microsoft Visual Studio 2005 Designer for Windows Workflow
Foundation.
The workflow initiation form is displayed to the user when the workflow is first activated. To create the
workflow initiation InfoPath form, do the following:
1. Open Office InfoPath 2007. In the Getting Started dialog box, click Design a Form Template.
2. In the Design a Form Template dialog box, click Blank, select the Enable browser-compatible
features only check box, and then click OK.
a. Type Assign Task To:, press Enter, and then drag a text box control onto the form from the
Controls pane. Right-click the text box control, and then click Text Box Properties. For
Field name, type assignee. Click Apply. On the Size tab, set the width to 300 px, and then
click OK.
b. On the form, press the right-arrow key once, and then press Enter twice. Type Instructions:,
press Enter, and then drag another text box control onto the form. Right-click the text box
control, and then click Text Box Properties. For Field name, type instructions. Click
Apply. On the Size tab, set the width to 300 px, and then click OK.
c. On the form, press the right-arrow key once, and then press Enter twice. Type Comments:,
press Enter, and then drag another text box control onto the form. Right-click the text box
control, and then click Text Box Properties. For Field name, type comments. Click Apply.
On the Size tab, set the width to 300 px, and then click OK.
d. On the form, press the right-arrow key once, and then press Enter twice. Drag a button
control onto the form from the Controls pane. Right-click the button control, and then click
Button Properties. On the General tab, for Label, type Submit, and then click OK.
Note:
The text fields are named assignee, instructions, and comments. You reference these fields by these
names in the section of this article titled "Adding Code to Respond to Workflow Activation," when you
add code to the workflow to work with the initiation form data.
b. In the Data Source pane, right-click MyFields, and then click Properties.
c. In the Field or Group Properties dialog box, for Name, type InitForm.
d. Click OK to save the change.
Later in this article, you extract the schema (.xsd) file for this form and use it as the basis for the
class referenced in the workflow code that processes the initiation form data. The root element of
the schema file is named the same as the form fields collection. The class generated from the
schema file has the same name as the schema file root element.
Specifying a unique name for the fields collection, rather than using the default name of myFields,
helps ensure that the class that is generated from the form schema file also has a unique name.
This is especially important when programming a workflow that deserializes multiple forms.
Note:
One rule submits the form information to the hosting environment (in this case, Office SharePoint
Server); the other rule closes the form when the user clicks Submit.
a. Right-click the Submit button control on the form, and then click Button Properties.
b. In the Button Properties dialog box, on the General tab, click Rules.
d. In the Rule dialog box, click Add Action. In the Action drop-down list, select Submit using
a data connection, and then click Add.
e. In the Data Connection Wizard, click Create a new connection to and Submit data, and
then click Next.
f. Under How do you want to submit your data, click To the hosting environment, and
then click Next. Click Finish.
k. In the Action drop-down list, select Close the form, and then click OK.
b. In the Form Options dialog box, in the Category list, click Security and Trust.
c. Under Security Level, clear the Automatically determine security level check box, and
then click Domain.
d. Click OK to save the changes and close the Form Options dialog box.
a. On the File menu, click Save As. In the Save As dialog box, for File name, type
InitForm.xsn. Save the form to any location.
c. In the Publishing Wizard, select To a network location, and then click Next.
d. Click Browse. In the Browse dialog box, navigate to the folder where you want to publish the
form. For File name, type InitForm, and then click OK.
e. In the Publishing Wizard, for Form template name type InitForm, and then click Next.
f. Clear the alternate path to the form from the text box, and then click Next.
Note:
Do not specify an alternate path to the form. If you specify an alternate path, it causes an error that
prevents publishing the form to the server.
10. Generate a new class file, based on the form schema (.xsd) file:
a. On the File menu, click Save As Source Files. Browse to the location where you want to save
the form source files, and then click OK.
InfoPath saves a collection of form source files, including the schema file, to the specified
location. The form schema file is always named myschema.xsd.
Next, you use the Microsoft .NET Framework 2.0 command-line tool, xsd.exe, to generate a
new class file from the form schema.
c. In Visual Studio 2005, open a Visual Studio Command window, navigate to the location of the
myschema.xsd file, and then run the following command:
Copy Code
d. Replace language with either VB or CS, depending which language you will write the workflow
in. This command generates a new class file based on the form schema. The file is named the
same as the schema file, that is, myschema.cs or myschema.vb, and is located in the current
directory. The class in the file is named the same as the root element of the schema, which
was named the same as the form fields collection.
Note:
If the form is still open in InfoPath, xsd returns an error stating that it cannot access the file because it is
being used by another process.
1. Open Office InfoPath 2007. In the Getting Started dialog box, click Design a Form Template.
2. In the Design a Form Template dialog box, click Blank, select the Enable browser-compatible
features only check box, and then click OK.
4. Add controls:
a. On the form, type Comments:, press Enter, and then drag a text box control onto the form
from the Controls pane. Right-click the text box control, and then click Text Box Properties.
For Field name, type comments. Click Apply. On the Size tab, set the width to 300 px, and
then click OK.
b. On the form, press the right-arrow key once, and then press Enter twice. Drag a check box
control onto the form. Right-click the check box control, and then click Check Box
Properties. For Field name, type isFinished, and then click OK.
c. Next to the check box, select the text Field 2. Type Complete Task, to replace the text Field
2. Press Enter twice.
d. Drag a button control onto the form. Right-click the button control, and then click Button
Properties. On the General tab, for Label, type OK. Click Apply. On the Size tab, set the
width to 80 px, and then click OK.
Note:
Xml
Copy Code
<z:row xmlns:z="#RowsetSchema"
ows_comments=""
/>
7. Add the workflow task schema to the task edit form as a secondary data source:
a. In Office InfoPath 2007, on the Design Tasks pane, select Data Source, and then click
Manage Data Connections.
c. In the Data Connection Wizard, click Create a new connection to and Receive data, and
then click Next.
e. Click Browse, navigate to the folder where you saved ItemMetadata.xml, select it, and then
click Open. Click Next.
f. Click Include the data as a resource file in the form template or template part, and
then click Next.
g. Type ItemMetadata as the data connection name, and ensure that the Automatically
retrieve data when form is opened check box is selected. Click Finish. Click Close.
After you include the ItemMetadata.xml file as a resource file in the form template, the file is
no longer needed in the workflow solution.
Note:
ItemMetadata.xml is required as a secondary data source for task forms. If you do not add
ItemMetadata.xml as a secondary data source, the form generates an error upon opening.
8. Data-bind the Comments text box control to the comments elements in the workflow task schema:
a. On the form, right-click the text box control, and then click Text Box Properties.
b. In the Text Box Properties dialog box, on the Data tab, in the Default Value section, click
the formula button.
d. In the Select a Field or Group dialog box, in the Data source list, select ItemMetadata.
The Comments text box control is now bound to the comments workflow task property. When
Office SharePoint Server 2007 loads the task form, the form displays the task comments in the
text box.
b. In the Form Options dialog box, in the Category list, click Security and Trust.
c. Under Security Level, clear the Automatically determine security level check box, and
then click Domain.
d. Click OK to save the changes and close the Form Options dialog box.
a. On the File menu, click Save As. In the Save As dialog box, for File name, type
TaskForm.xsn. Save the form to any location.
c. In the Publishing Wizard, select To a network location, and then click Next.
d. Click Browse. In the Browse dialog box, navigate to the folder where you want to publish the
form. For File name, type TaskForm, and then click OK.
e. In the Publishing Wizard, for Form template name type TaskForm, and then click Next.
f. Clear the alternate path to the form from the text box, and then click Next.
Note:
Do not specify an alternate path to the form. If you specify an alternate path, it causes an error that
prevents publishing the form to the server.
3. In the New Project dialog box, in the Project types pane, select Visual C# or Visual Basic for the
project type, and then click SharePoint.
Visual Studio creates a new solution containing the workflow library project.
a. In Solution Explorer, right-click the InfoPathFormWorkflowLibrary project, click Add, and then
click Existing Item.
b. In the Add Existing Item dialog box, navigate to the folder that contains the InitForm.cs or
InitForm.vb file that you created previously, select the file, and then click Add.
6. Add the InitForm.xsn and TaskForm.xsn files that you created previously (when you published the
initiation and task edit forms) to the InfoPathFormWorkflowLibrary project:
a. In Solution Explorer, right-click the InfoPathFormWorkflowLibrary project, click Add, and then
click Existing Item.
b. In the Add Existing Item dialog box, navigate to the folder that contains the InitForm.xsn
and TaskForm.xsn files that you created previously, select the files, and then click Add.
The form template files are added to the project.
In Solution Explorer, expand the InfoPathFormWorkflowLibrary project node, and then double-click
the Workflow1.cs file.
The initial workflow outline is displayed in the Workflow Designer, showing the OnWorkflowActivated activity
added by Visual Studio.
The OnWorkflowActivated activity runs when the workflow is first activated. It is a good place to process the
information specified by the user via the workflow initiation form.
To add a method that is called when the OnWorkflowActivated activity is run, set the Invoked property of
the onWorkflowActivated1 activity:
Visual Studio opens the Workflow1.cs or Workflow1.vb file in the code editor with the generated
onWorkflowActivated method visible.
3. In the Workflow1.cs or Workflow1.vb file, add the following Imports or using statements below the
Imports or using statements generated by Visual Studio.
Visual Basic
Copy Code
Imports System.Xml
Imports Microsoft.SharePoint.Workflow
C#
Copy Code
using Microsoft.SharePoint.Workflow;
Visual Basic
Copy Code
Private assignee As String = String.Empty
Private instructions As String = String.Empty
Private comments As String = String.Empty
Private isFinished As Boolean = False
Public workflowId As Guid
C#
Copy Code
private String assignee = default(String);
private String instructions = default(String);
private String comments = default(String);
private bool isFinished = false;
5. Add the following code to the onWorkflowActivated method to parse the incoming workflow
initiation data.
Visual Basic
Copy Code
workflowId = workflowProperties.WorkflowId
assignee = InitForm.assignee
instructions = InitForm.instructions
comments = InitForm.comments
C#
Copy Code
workflowId = workflowProperties.WorkflowId;
assignee = initform.assignee;
instructions = initform.instructions;
comments = initform.comments;
Designing the Workflow
3. Drag a CreateTask activity, a WhileActivity activity, and a CompleteTask activity, in that order,
to the workflow outline.
If the SharePoint workflow activities are not visible in the Visual Studio toolbox, you can load them:
In workflow design mode, right-click the Toolbox, and then click Choose Items.
On the Activities tab, browse to the %programfiles%/Common Files/Microsoft Shared/web
server extensions/12/ISAPI folder, and select the Microsoft.SharePoint.WorkflowActions.dll file, and
then click OK.
Setting Workflow Activity Properties
The Workflow Designer displays red exclamation marks next to each of the workflow activities that you
added in the previous step. For the task-related activities, this is because you have not set their correlation
token property yet. A correlation token is a unique identifier that enables mapping between the objects in a
workflow and the environment hosting the Windows Workflow Foundation (WF) workflow runtime.
2. In the Workflow1.cs or Workflow1.vb file, add the following lines of code to the body of the
Workflow1 class, just below the line that Visual Studio generated that defines the
workflowProperties variable.
Visual Basic
Copy Code
Public taskId As Guid = Guid.Empty
Public taskProperties As SPWorkflowTaskProperties = _
New SPWorkflowTaskProperties()
C#
Copy Code
public Guid taskId = default(System.Guid);
public SPWorkflowTaskProperties taskProperties =
new SPWorkflowTaskProperties();
3. Switch to the workflow design view, right-click the createTask1 activity, and then click
Properties.
4. In the Properties pane, type taskToken as the value of the CorrelationToken property, and then
press Enter.
5. Click the plus sign to display the OwnerActivityName property. Set its value by expanding the
property value dropdown list and clicking Workflow1.
6. Click the TaskId property, and then click the ellipses (…) button.
7. In the Property Bind dialog box, click the taskId property that you created in Step 2, and then
click OK.
9. In the Property Bind dialog box, click the taskProperties property that you created in Step 2,
and then click OK.
11. In the Properties pane, click the CorrelationToken property. Set its value by expanding the
property value dropdown list and clicking taskToken.
12. Click the TaskId property, and then click the ellipses (….) button.
13. In the Bind dialog box, click the taskId property that you created in Step 2, and then click OK.
14. Click the AfterProperties property, and then click the ellipses (….) button.
15. In the Property Bind dialog box, click the taskProperties property that you created in Step 2,
and then click OK.
17. In the Properties pane, click the CorrelationToken property. Set its value by expanding the
property value dropdown list and clicking taskToken.
18. Click the TaskId property, and then click the ellipses (….) button.
19. In the Property Bind dialog box, click the taskId property that you created in Step 2, and then
click OK.
At this point, the taskId variable used for the TaskId property of the task-related activities has a default
GUID value of all zeros. The title property of the taskProperties variable is not set. To initialize these
variables, do the following:
Visual Studio switches to the Workflow1.cs or Workflow1.vb file and displays the body of the
createTask1_MethodInvoking method. This method is called prior to the createTask1 activity
being started.
Visual Basic
Copy Code
taskId = Guid.NewGuid()
taskProperties.Title = "Task for " + workflowProperties.Item.Name
taskProperties.Description =
"A task from the InfoPath forms workflow."
taskProperties.ExtendedProperties("Assignee") = assignee
taskProperties.ExtendedProperties("Comments") = comments
taskProperties.ExtendedProperties("Instructions") = instructions
C#
Copy Code
taskId = Guid.NewGuid();
taskProperties.Title = "Task for " + workflowProperties.Item.Name;
taskProperties.Description =
"A task from the InfoPath forms workflow.";
taskProperties.ExtendedProperties["Assignee"] = assignee;
taskProperties.ExtendedProperties["Comments"] = comments;
taskProperties.ExtendedProperties["Instructions"] = instructions;
3. The next step is to set the properties of the While activity. The While activity causes the activities
inside it to loop as long as the condition it evaluates resolves to true. In this example, it is used to
loop around the TaskChanged activity until the user explicitly completes the task.
a. Switch to the workflow design view, right-click the whileActivity1 activity, and then click
Properties.
b. In the Properties pane, type Code Condition as the value of the Condition property.
This tells the While activity that it is calling a method to determine to end the loop. A plus
sign is displayed to the left of the Condition property.
c. Click the plus sign, type notFinished as the value of the Condition subproperty, and then
press Enter.
Visual Studio switches back to code view and displays the code for the generated notFinished
method.
Visual Basic
Copy Code
e.Result = Not isFinished
C#
Copy Code
e.Result = !isFinished;
6. This code checks the value of the isFinished variable. The value of the isFinished variable is set
depending on whether the user checked the Complete Task check box on the workflow task edit
form. To set the value of the isFinished variable, add a method that is called when the
TaskChanged activity is run:
7. Switch to the workflow design view, right-click the onTaskChanged1 activity, and then click
Properties.
8. In the Properties pane, type onTaskChanged as the value of the Invoked property, and then
press Enter.
Visual Studio switches to code view and displays the code for the generated onTaskChanged
method.
Visual Basic
Copy Code
isFinished = Boolean.Parse( _
taskProperties.ExtendedProperties("isFinished").ToString())
C#
Copy Code
isFinished = bool.Parse(
taskProperties.ExtendedProperties["isFinished"].ToString());
Each time the user edits the task, the onTaskChanged1 activity handles the task changed event. It
invokes the onTaskChanged method, which examines the task properties and sets the isFinished variable
to represent whether the user marked the task as complete. The while1 activity then invokes the
notFinished method, which sets the result of the event to the opposite of the isFinished variable. If
isFinished is equal to false, the event result is set to true, and the while1 activity waits for task changes.
If isFinished is equal to true, the event result is set to false, the while1 activity completes, and the
workflow continues to the next activity. The next activity is the CompleteTask activity, which sets the task
as complete.
Signing the Workflow Library Assembly with a Strong Name
For the workflow library assembly to be installed in the Global Assembly Cache, it must be signed with a
strong name. A strong name consists of the assembly's identity—its simple text name, version number, and
culture information (if provided)—plus a public key and a digital signature. To assign a strong name to the
assembly in Visual Studio:
2. On the Project Properties page, on the Signing tab, select the Sign the assembly check box.
4. In the Create Strong Name Key dialog box, type Keyfile as the key file name, and then clear the
Protect my key file with a password check box.
When Visual Studio created the InfoPathFormWorkflowLibrary project, it created two deployment-related
files: Feature.xml and Workflow.xml. Feature.xml defines the workflow as an installable SharePoint feature.
Workflow.xml defines the actual workflow. Initially, these files contain only instructions for how to insert the
necessary code to define the workflow feature and the workflow. To modify the Feature.xml and
Workflow.xml files to work with the InfoPathFormWorkflowLibrary workflow:
1. Open the Feature.xml file and replace its contents with the following markup.
Xml
Copy Code
<?xml version="1.0" encoding="utf-8"?>
<!-- _lcid="1033" _version="12.0.3111" _dal="1" -->
<!-- _LocalBinding -->
<Feature Id="GUID"
Title="InfoPath Form Workflow"
Description="This feature is a workflow that demonstrates using
InfoPath forms."
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 the feature file location -->
<Property Key="RegisterForms" Value="Forms\*.xsn" />
</Properties>
</Feature>
2. Replace the GUID placeholder text in the <Feature Id> attribute with a unique GUID. You can
generate a unique GUID from within Visual Studio:
a. On the Tools menu, click Create GUID to launch the Create GUID tool.
b. In the Create GUID tool, click Registry Format, and then click Copy to copy the GUID to the
Clipboard.
c. Paste the GUID into the markup, replacing the GUID placeholder text. Do not include the
opening and closing curly braces.
Xml
Copy Code
<?xml version="1.0" encoding="utf-8" ?>
<!-- _lcid="1033" _version="12.0.3015" _dal="1" -->
<!-- _LocalBinding -->
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
<Workflow
Name="InfoPath Form Workflow"
Description="This workflow demonstrates using InfoPath forms."
Id="GUID"
CodeBesideClass="InfoPathFormWorkflowLibrary.Workflow1"
CodeBesideAssembly="InfoPathFormWorkflowLibrary,
Version=1.0.0.0, Culture=neutral, PublicKeyToken=publicKeyToken"
InstantiationUrl="_layouts/IniWrkflIP.aspx"
ModificationUrl="_layouts/ModWrkflIP.aspx"
StatusUrl="_layouts/WrkStat.aspx"
TaskListContentTypeId="0x01080100C9C9515DE4E24001905074F980F93160" >
<Categories/>
<MetaData>
<Instantiation_FormURN>
InitFormID
</Instantiation_FormURN>
<Task0_FormURN>
TaskFormID
</Task0_FormURN>
</MetaData>
</Workflow>
</Elements>
a. Use the process described in Step 2 of the section "Deploying the Workflow" to replace the
GUID placeholder text in the <Workflow Id> attribute with a unique GUID.
b. Use the Strong Name (sn.exe) tool that is included with Visual Studio to determine the public
key token of the InfoPathFormWorkflowLibrary assembly. Open a Visual Studio Command
Window and navigate to the folder that contains the assembly. Run sn.exe with the -T option
against the assembly, like this:
Copy Code
sn.exe -T InfoPathFormWorkflowLibrary.dll
c. Notice the returned public key token and then replace the publicKeyToken placeholder text
used with the CodeBesideAssembly attribute with the token returned by sn.exe.
d. Replace the InitFormID and TaskFormID placeholders used with the Instantiation_FormURN
and Task0_FormURN MetaData nodes with the InitForm and TaskForm form IDs determined
previously from within InfoPath.
4. Edit the contents of the Install.bat file, replacing the text generated by Visual Studio with the
following:
Copy Code
echo Copying the feature...
::Note: Uncomment these lines if you've modified your deployment xml files
or IP forms
stsadm -o deactivatefeature -filename InfoPathFormWorkflowLibrary
\feature.xml -url http://localhost
stsadm -o uninstallfeature -filename InfoPathFormWorkflowLibrary
\feature.xml
b. When deploying a release mode build of the workflow, change the build folder in the path used
with the gacutil.exe -if command from Debug to Release.
5. Build the InfoPathFormWorkflowLibrary project and then run the Install.bat file to deploy the
workflow. After the deployment succeeds, you can associate the workflow with a SharePoint list,
library, or content type and then test it. For more information about how to associate a workflow
with a list, library, or content type, refer to the Add or Change a Workflow for a List, Library, or
Content type [ http://office.microsoft.com/en-us/sharepointserver/HA101720071033.aspx ] on
Microsoft Office Online.