In this recipe, we will recreate the previous recipe to use InfoPath forms for initiation form.
This recipe assumes that you are familiar with creating InfoPath forms using InfoPath designer 2010. Your SharePoint server should be configured to use form services. Refer to MSDN at: http://technet.microsoft.com/en-us/library/cc262263.aspx for more information on configuring the form services.
myschema.xsd
. There are situations when you use controls like people picker on the form, the export option may also create another XSD file named BuiltInActiveXControls.xsd
.
Xsd.exe myschema.xsd /c /l:cs
myschema.cs
. To generate the VB.Net class file, change the switch /l:cs
to /l:vb
. In case you had BuiltInActiveXControls.xsd
file, provide that too in the preceding command. The command in this case would be as follows:
Xsd.exe myschema.xsd BuiltInActiveXControls.xsd /c /l:cs
Sample.txt
and Elements.xml
. The Elements.xml
is used by the project feature to deploy the contents of the module. Delete the Samples.txt
file as it is just a placeholder to show that files can be deployed using modules. Visual Studio automatically makes the necessary changes to Elements.xml
whenever you add and delete files from the module. Elements.xml
file in the module. The following code shows the Elements.xml
file contents:<?xml version="1.0" encoding="utf-8"?> <Elements xmlns="http://schemas.microsoft.com/sharepoint/"> <Module Name="IPForms"> <File Path="IPFormsInitForm.xsn" Url="IPForms/InitForm.xsn" /> </Module> </Elements>
RegisterForms
and provide the path of your module that contains the InfoPath forms for the value of the property. This will register the forms in the module when the feature is activated. Your end result of the manifest changes should be similar to the following code:<?xml version="1.0" encoding="utf-8" ?> <Feature xmlns="http://schemas.microsoft.com/sharepoint/"> <Properties> <Property Key="GloballyAvailable" Value="true" /> <Property Key="RegisterForms" Value="IPForms*.xsn" /> </Properties> </Feature>
Receiver Assembly
and Receiver Class
properties to the following values:Receiver Assembly = Microsoft.Office.Workflow.Feature, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c Receiver Class = Microsoft.Office.Workflow.Feature.WorkflowFeatureReceiver
The WorkflowFeatureReceiver
class processes the manifest file changes that we did in the previous step. This class will manage the registration of the InfoPath files for use with Workflow
. Until now we created the form and we created the necessary source files that can be used in the workflow and also we made changes to the feature to register the forms for workflow. The last step is to associate this form with the workflow. To do this, we will open the Elements.xml
file of the workflow and add the URN (Uniform Resource Name) of the InfoPath form. Elements.xml
file of the workflow and add the Instantiation_FormURN
property under MetaData
element and also make changes to InstantiationUrl
property. InstantiationUrl
refers to an ASPX page that hosts the InfoPath form. Your elements.xml
file should look like following:<?xml version="1.0" encoding="utf-8" ?> <Elements xmlns="http://schemas.microsoft.com/sharepoint/"> <Workflow Name="InfoPathInitiation - Workflow1" Description="My SharePoint Workflow" Id="d1b973a0-9f8b-4abd-befa-0113641cfb34" CodeBesideClass="InfoPathInitiation.Workflow1.Workflow1" CodeBesideAssembly="$assemblyname$" InstantiationUrl="_layouts/IniWrkflIP.aspx"> <Categories/> <MetaData> <AssociationCategories>Site</AssociationCategories> <Instantiation_FormURN>urn:schemas-microsoft-com:office:infopath:InitForm:-myXSD-2011-07-02T14-40-31</Instantiation_FormURN> <StatusPageUrl>_layouts/WrkStat.aspx</StatusPageUrl> </MetaData> </Workflow> </Elements>
XmlSerializer
to deserialize the string initiation data (in the XML format) from our form to .NET
object. The myFields
object is in the myschema.cs
file that we created via the XSD tool. Make changes to the CheckCreditAmount
method as follows:private void CheckCreditAmount(object sender, ConditionalEventArgs e) { string sInitData = workflowProperties.InitiationData; XmlSerializer ser = new XmlSerializer(typeof(myFields)); XmlTextReader reader = new XmlTextReader( new System.IO.StringReader(sInitData)); myFields ipInitForm = (myFields)ser.Deserialize(reader); string sCreditRequested = ipInitForm.txtCreditRequested; double initAmount = 0; double.TryParse(sCreditRequested, out initAmount); if (initAmount <= 1000) e.Result = true; else e.Result = false; }
As usual, we get the initiation data from the WorkflowProperties
object like we did in the previous recipe. The difference in here is that we get XML string for the initiation data as InfoPath forms are XML files. To read values from this XML string, we cast the string into a .NET
class that was generated from the form's schema definition file. This is the reason why we used the XSD generator to create the .NET
class for our initiation form. The XSD generator when created the .NET
class from the schema file, each of the control in the form is mapped to a property. Hence we can just use the dot notation to read the values of the form's controls. The XmlSerializer
is used in the CheckCreditAmount
method to cast the XML string to the .NET
class.
To make use of the InfoPath forms in the workflow, we need to deploy it to the form server of the SharePoint and make it workflow enabled. This is the reason, why we used a feature to deploy our InfoPath form. Just using feature would not deploy it to the forms server. We need to instruct the feature to do that task, for this purpose, we made use of the event receiver. We made use of the built-in event receiver Microsoft.Office.Workflow.Feature.WorkflowFeatureReceiver
for this task.
Apart from deploying InfoPath forms to the form server, we need to hook this form to the workflow so it opens it at the initiation stage. Since InfoPath forms are XML files, it needs to be hosted on some page. SharePoint provides IniWrkflIP.aspx
that resides in the _layout
root directory in the SharePoint server. This ASPX page can host InfoPath forms. Apart from that we need to notify the workflow what form to open. This is done providing the form's URN (Uniform Resource Name). We made these changes to the workflow's Elements.xml
file.
18.227.190.93