Aidan Garnish

Collaboration Not Competition

SharePoint Custom Workflow Visual Studio Solution

There are loads of articles and blog posts about creating SharePoint 2007 custom workflows using Visual Studio but most of them only tell part of the story or have certain steps missing. Nick Swan has probably come closest with his post for Visual Studio 2005 but even with a post as comprehensive as this it is clear from the comments that it is something people still struggle with. Rather than rehash all of those posts I thought it would be more useful to provide an example Visual Studio 2008 solution that is already set up and ready to go.

This article does not aim to be a comprehensive guide to setting up a custom workflow. It is aimed at a .net developer audience who are already familiar with SharePoint and InfoPath development but who just need a bit of help bring all the bits of SharePoint custom workflow development together into a working solution.

This solution was created using STSDev v1.4 which gives a good starting point for a sequential workflow solution and automatically creates a .wsp file whenever the project is built. If you are not familiar with STSDev projects the .wsp can be found in the Deployment folder of the project.

In the project I have included the more common generic elements for a custom SharePoint workflow:

  • An InfoPath initiation form that allows the user to provide information to the workflow task. In this case the workflow task title is set and a property is stored to be displayed later on the task form.
  • An InfoPath task form that displays information from the initiation form and allows the user to approve or reject the the item.
  • The sequential workflow itself - wiring this up is often a place where people miss something or make mistakes.
  • A WorkflowTestInitForm.cs file that is used to make the information typed into the initialisaton form available in code. See comments in this file to generate your own.
  • An ItemMetaData.xml file that is used by the InfoPath task form as a secondary data source so that the information supplied by the initiation form can be displayed.

The code has been commented to provide additional information and guidance on how to set up your own workflows. For the solution to work the only thing you have to change before building and deploying the .wsp is line 62 in SequentialWorkflow01.cs - the task assigned to property must be set to a user that is valid for your environment.

To open the solution just save this zip and extract in your development environment - (91.97 kb)

Creating a custom save function for InfoPath 2007 browser based forms

I recently had an issue where it was necessary to only save some of the fields on an InfoPath form back to the form library. To do this required a combination of custom code and submission to a form library using a data connection.

The steps to do this were as follows:

1. Add a Save button to the form

2. Right click the save button and select button properties, change Action dropdown to Submit and click Submit Options...

3. In the submit options select Send form data to a single destination and choose SharePoint document library from the dropdown

4. Click Add to add a new data connection and follow the wizard to set this up to submit your form to the required library

5. Now that the data connection has been set up, select Perform custom action using Code and click the Edit Code button - this will create a submit method in the code behind file

6. In the submit method place the code to do the custom "stuff" and then submit the form to the data connection. The code will look something like:

public void FormEvents_Submit(object sender, SubmitEventArgs e)

           //remove values from the form that you don't want to be saved
           XPathNavigator xPnName = MainDataSource.CreateNavigator().SelectSingleNode("/my:myFields/my:Name", NamespaceManager);

           //submit the form using the data connection

           DataConnections["Main submit"].Execute();

           //set e.CancalableArgs to be false once form has successfully saved

           e.CancelableArgs.Cancel = false;           

To give the form a unique filename and allow updates to saved forms do the following:

1. Create a new xml node called dtNow, give it a default value of now() and uncheck the Update this value when the result of the formula is recalculated box.

2. Go to Data and Data Connections and Modify the submit data connection created above.

3. In the Filename field add concat(userName(), dtNow) and check the Allow overwrite if the file exists checkbox.

Finally, convert the data connection used to submit the form to be centrally managed as described here

Using centrally managed SharePoint data connection files with InfoPath 2007

When deploying InfoPath forms with data connections between environments it is possible to use centrally managed data connection files to make the process a bit smoother.

To set up a centrally managed data connection do the following:

  • From your InfoPath form select Data Connections... from the Data menu
  • Select the data connection you want to make centrally managed and click the Convert button
  • A .udcx file will be created in a site collection data connections library you select so it may be necessary to set up a data connections library first
  • Navigate to the site collection data connections library and save the .udcx file locally
  • Go to SharePoint Central Administration - Application Management - Manage Data Connection Files
  • Upload the .udcx file to the central data connection files library
  • Go back to the InfoPath form and remove the existing data connections.
  • Recreate the data connections using Search for connections on a Microsoft Office SharePoint Server
  • Create the connection using the .udcx file that was saved to the site collection data connection library but make sure that you click on Connection Options... and select Centrally Managed
  • Configure the controls on the form to use the data connection as normal

The form will now use the centrally managed data connection. To deploy to another environment (eg. UAT, Production) you will need to upload the .udcx file to the relevant SharePoint Central Administraion after altering it to use the connection properties relevant to the new environment. To update the .udcx file open it in a text editor and alter the following parameters (example is for a connection to a SharePoint list):


Where ListId is the SharePoint list id and WebUrl is the url of the site collection that holds the SharePoint list.

When you deploy your InfoPath form it will reference the centrally managed .udcx file allowing it to seamlessly connect with data in the new environment.

Hiding and showing optional sections in InfoPath 2007

It is possible to add logic to InfoPath forms so that certain fields are displayed based on the value of another field. For example - if a user fills in their age on a form as being 17 or greater then the question - Do you hold a full driving license? - becomes visible. In the UK this question is not relevant if the user is less than 17.

To do this using InfoPath 2007 you will need to create a form with the two questions - How old are you? and - Do you hold a current driving license? Add a two row, two column table to your form. Add the How old are you? question in the top left box. In the top right box add a text box that takes a whole number, call it Age and set it with a default of 0.

Next, merge the two cells in the bottom row and add an optional section control. Insert a table with one row and two columns to the optional control. Add the Do you hold a current driving license? question to the table. In the right hand table cell add a drop down list box that has options of select..., Yes and No.

Set the section properties to display the section by default. You should now have something that looks like this when previewed:


Next right click on the optional section and select 'Section properties...' Select 'Display' and then 'Conditional Formatting...'

Click Add - set the condition to be Age is less than or equal to 16 and check the 'Hide this control' option.

Preview the form again and you should see:

Enter an age greater than 17 to display the optional question:

Change the age to less than 17 and the question will be hidden again.

Creating an InfoPath installer file with Visual Studio 2008

Having created an InfoPath form project using Visual Studio 2008 and adding some code behind to the form I wanted to deploy it to SharePoint. The easiest way to do this should have been to use the publishing wizard to create an installer file. However, there seems to be a bug in Visual Studio 2008 that prevents you from selecting this option in the wizard. The message displayed is that you need VS 2003 or 2005 installed to create the installer file.

There is a way around this by using the REGFORM.exe tool found in C:\Program Files\Microsoft Office\Office12. The tool allows you to create a .js file that marks the .xsn as fully trusted so that it can be added to the managed forms collection on your MOSS farm.

First you need to go to VS and the Publish option in the Build menu. Instead of selecting the create installer option publish to a network location and create the .xsn on your file system.

Next, open up a command prompt and navigate to C:\Program Files\Microsoft Office\Office12\regform.exe.

Supply the necessary parameters - these are, the /U the URN for the form which can be found in VS, File - Form Properties - ID, /T Yes to mark the form as fully trusted (this is necessary to deploy it to SharePoint) and finally the path to the .xsn file.

You should end up with something like - regform.exe /U urn:schemas-microsoft-com:office:infopath:MyForm:-myXSD-2008-08-07T13-36-16 /T Yes C:\Forms\MyForm

Running the command will create a .bak file and a .js file in the same folder as the .xsn. Run the .js file and then open SharePoint central administration. Go to the Applications tab and under the InfoPath forms services section select Manage Form Templates.

This Microsoft article explains the rest of the process to make the form available in a site collection in detail

If you run the .js in your development environment you will need to unregister the form before you will be able to open it again from Visual Studio. To do this open the .js file in Notepad and change var fInstall = true; to var fInstall = false; - Run the .js file again and this time the message prompts will tell you the form is being unregistered. You can now run the form from Visual Studio again.