Controlling start options for SharePoint workflows in Visual Studio

When you build a reusable SharePoint workflow, it's useful to be able to control which start options are available. For example, if you only want your workflow to run once when an item is created, it makes sense to disable the "Changing an item will start this workflow" option.

SharePoint Designer provides some handy checkboxes that you can use to control your start options:










However, it's not immediately obvious how you can set these options for a Visual Studio workflow as the options aren't documented anywhere.

To control the start options for a SharePoint workflow in Visual Studio, you need to edit the feature element file that deploys your workflow. Within the feature element file, you need to add properties to the File element that deploys your Workflow.xaml file:

<?xml version="1.0" encoding="utf-8" ?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <Module Name="[Workflow Name]" Url="wfsvc/1c77e2780d9d49f197115fef9e7d9c50">
    <File Url="Workflow.xaml" 
          Type="GhostableInLibrary" 
          Path="[Workflow Name]\Workflow.xaml" 
          DoGUIDFixUp="TRUE" 
          ReplaceContent="TRUE">
      <Property Name="ContentType" Value="WorkflowServiceDefinition" />
      <Property Name="isReusable" Value="true" />
      <Property Name="RequiresInitiationForm" Value="True" />
      <Property Name="RequiresAssociationForm" Value="False" />      
      <Property Name="DisableAutoStartCreate" Value="True" />
      <Property Name="DisableAutoStartChange" Value="True" />
      <Property Name="WSPublishState" Value="3" />
      <Property Name="WSDisplayName" Value="[Workflow Title]" />
      <Property Name="WSDescription" Value="[Workflow Description]" />
      <Property Name="InitiationUrl" Value="wfsvc/....aspx" />
    </File>
    ...

</Elements>

In short:

  • To disable the "Creating a new item will start this workflow" option, add the DisableAutoStartCreate property and set the value to True.
  • To disable the "Changing an item will start this workflow" option, add the DisableAutoStartChange property and set the value to True.
  • To disable the "Allow this workflow to be manually started... " option, add the DisableManualStart property and set the value to True.
As a bonus fact, if you want to set the workflow status to the current stage name (SharePoint Designer workflows do this automatically, and it's a pretty useful feature for at-a-glance progress monitoring), add the AutosetStatusToStageName property and set the value to True.

I figured this out by creating a basic reusable workflow in SharePoint Designer, saving it as a template, then cracking open the .wsp file to look at how SPD goes about constructing workflows. If you do this, you'll find that the feature element file created by SPD specifies a property named MetaInfo on the workflow.xaml file. This property contains a bunch of property values. If you can look beyond the hideous syntax it uses, you can pick out the names of some useful properties.


<Property Name="MetaInfo" Value="8;#vti_parserversion:SR|16.0.0.4456&#xD;&#xA;
                                 WSDescription:SW|&#xD;&#xA;
                                 IsProjectMode:SW|false&#xD;&#xA;
                                 isReusable:SW|true&#xD;&#xA;
                                 WSGUID:SW|2f0c508d...&#xD;&#xA;
                                 WSDisplayName:SW|ReusableTest&#xD;&#xA;
                                 WSPublishState:IW|3&#xD;&#xA;
                                 vti_author:SR|i:0#.w|jrjlee\\jason&#xD;&#xA;
                                 RequiresAssociationForm:SW|false&#xD;&#xA;
                                 AutosetStatusToStageName:SW|true&#xD;&#xA;
                                 DisableAutoStartChange:SW|true&#xD;&#xA;
                                 RestrictToType:SW|List&#xD;&#xA;
                                 vti_foldersubfolderitemcount:IW|0&#xD;&#xA;
                                 vti_modifiedby:SR|i:0#.w|jrjlee\\jason&#xD;&#xA;
                                 FileLeafRef:SW|workflow.xaml&#xD;&#xA;
                                 ContentTypeId:SW|0x01002...&#xD;&#xA;
                                 DisableAutoStartCreate:SW|true&#xD;&#xA;
                                 RequiresInitiationForm:SW|false&#xD;&#xA;
                                 InitiationUrl:SW|&#xD;&#xA;
                                 FormField:SW|&lt;Fields /&gt;&#xD;&#xA;
                                 AssociationUrl:SW|&#xD;&#xA;
                                 DisableManualStart:SW|true&#xD;&#xA;
                                 SPDConfig.LastEditMode:SW|TextBased&#xD;&#xA;
                                 vti_folderitemcount:IW|0&#xD;&#xA;" />



Comments

Popular posts from this blog

Server-side activities have been updated

The target principal name is incorrect. Cannot generate SSPI context.

Custom Workflow Activity for Creating a SharePoint Site