Showing posts with label Workflow. Show all posts
Showing posts with label Workflow. Show all posts

Sunday, October 16, 2011

Create InfoPath Form Instance through Workflow

Issue

Have you ever wanted to have an InfoPath form generated by a workflow? Well I thought it would be a simple task but I had to do some extra things to make it happen correctly.

I recently wanted to be able to generate an InfoPath form based on an event on a SharePoint Designer workflow. So I published my InfoPath form to a Form Library, added an action to create a list item and then ran the workflow. I believed this would work because I have created Word templates, associated them to content types and the word file would be created just fine with the appropriate template. The net result in this situation does not work. Instead the InfoPath form template (.xsn file) is created as a file instance. The InfoPath xml that I had expected was not there.

Solution

I found this article (http://www.bizsupportonline.net/blog/2009/06/create-infopath-forms-sharepoint-designer-workflow/) which I reviewed. My solution is a little bit different because I was building this solution in SharePoint Online in Office365 and I was not able to implement this exact solution. However I was given some clues and came up with the following.

My solution was to:

  • Publish the InfoPath form the same way I did before.
  • Then I created an instance of the InfoPath form xml and saved it to my desktop.
  • Then went to the form library configuration, advanced settings and turned on “Allow management of content types”.
  • Then clicked on the default Form Content Type.
  • Then clicked on Advanced Settings for the Form Content.
  • Then I selected Upload a new document template and selected xml template I saved locally.

The following is what I had.

image

The InfoPath form template (.xsn file) is still there behind the scenes and the form will render correctly through InfoPath form services.

There are a few limitations of this solution:

  • The user will not be able to create a new InfoPath form instance through the browser using the New Document button in the ribbon. What will happen is the InfoPath client will launched. This is not such a big deal because in my situation InfoPath forms will always be created through the workflow.
  • If the InfoPath form changes, you will have to publish the InfoPath form template (.xsn file) and then go through these steps again.

Sunday, August 7, 2011

Part 5 – Custom Visio Services Reports with SharePoint 2010 Workflow

· Part 1 - Pattern for Building Stateful Workflows with SharePoint Designer 2010
· Part 2 - Custom Action for Workflow Initiation
· Part 3 - Delays for Workflow Initiation Action
· Part 4 - Using Business Connectivity Services (BCS) with SharePoint 2010 Workflow
· Part 5 - Custom Visio Services Reports with SharePoint 2010 Workflow

Introduction

This is the last part in this workflow series. In the first part of this series I wrote extensively about how Visio Services allow us to now do visual reporting on the progress of a workflow process instance. More importantly, it allows us to provide a visual report to business users removing all the technical steps that may be needed to implement an end to end workflow.

For example, I below I have a ton steps in a workflow. When SharePoint Designer 2010 publishes this workflow a Visio workflow diagram will be generated.

clip_image002

That Visio diagram will accurately show where it is within its execution however I can guarantee that the business user does not care about all these steps. All they want to know is what the major business process steps are and what the current status is.

Solution

We can provide a perfect solution using a custom Visio report.

My solution is to create a completely different Visio diagram to the business reporting. So what I did was take this original diagram I created in this series (below) with the objective of “lighting it up” with the current status of the business process.

clip_image004

The solution is simple.

· I create a column on my list item to store the current step in the business workflow the workflow is in.

· Then during my workflow, I update this column object in the Visio diagram I want to light up.

· Then connect a list and Visio web part together to allow a user to select a running workflow and then visually see the status of the workflow.

Configuration

To achieve this I need to perform a few steps.

In the workflow I added an Update List Item action. I updated my CurrentAction column with the name of the Visio object I want to light up. In this instance it is “Process.3”. Throughout other workflows I will update the CurrentAction column with other values that correspond to different objects in the Visio diagram.

clip_image006

Next I uploaded the Visio diagram to a location in SharePoint; simple enough.

Next I created a web part page. On that web part page I placed the list where the workflow is running.

Next I added a Visio web part and pointed to the Visio diagram that I had just uploaded. Below is a screenshot.

clip_image008

Next I need to connect the list web part to the Visio web part. I need to do this so that when a user selects a list item, the Visio diagram on the right will highlight the correct Visio object. On the Visio web part I made a connection to the Review list web part. Notice I select “Get Shapes to Highlight From”.

clip_image010

Next in the Configuration Connection screen, I select the CurrentAction column (which is the column I that I update in the workflow) and then select Shape Names. This is how the “Process.3” value is sent from the selected list item to the Visio diagram to be highlighted.

clip_image011

The net result is the following. As you can see a Visio object is now highlighted based on the item that I selected on the left hand side. Really simple and really effective!!!

clip_image013

Highlighting Multiple Items

In the example I just showed you, I am only highlighting the point where the process is in its execution. However, let’s say you want to highlight all the steps the workflow has gone through. Well that is simple too.

In this case I created a different column called “Actions” where I capture a comma delimited string of all the times I want to be highlighted. In the workflow I have the following. Notice in the String Builder window I concatenate the previous value in the column with the new value “,Process.3”.

clip_image015

Other than that, the configuration is 100% identical other than selecting the “Actions” column when connecting the web parts. The final result is the following – all of the Visio object names I concatenated together are highlighted.

clip_image016

Conclusions

This is such a powerful capability. I can create custom visual reports on my business process with no code.

I truly hope this workflow series will be helpful for you when starting to build process automation in SharePoint 2010.

Part 4 – Using Business Connectivity Services (BCS) with SharePoint 2010 Workflow

· Part 1 - Pattern for Building Stateful Workflows with SharePoint Designer 2010
· Part 2 - Custom Action for Workflow Initiation
· Part 3 - Delays for Workflow Initiation Action
· Part 4 - Using Business Connectivity Services (BCS) with SharePoint 2010 Workflow
· Part 5 - Custom Visio Services Reports with SharePoint 2010 Workflow

Introduction

If you have been reading this blog series closely I mentioned that I will be discussing how we can do reporting for our workflow solutions in SharePoint 2010.

As you may know when you configure a workflow in SharePoint there is a Workflow History list that must be associated which will stored information about the execution of a workflow instance. I have typically viewed this as more of a log. It is not something to be used for reporting. Here is a quick reference about it and I suggest you read it - http://technet.microsoft.com/en-us/library/ee662522.aspx

The reasons I do not use it for reporting is because:

· This list can potentially store a lot of data from a lot of different workflows.

· It will be purged after a period time. Even though you can disable a job to ensure logs are not deleted, doing this would disable the job for every workflow across an entire web application which is not good practice.

· Personally I like it for its logging capability but I would not use it for a reporting capability.

Solution

My approach will be to store data in a SQL Server table using Business Connectivity Services (BCS). With SharePoint 2010 it is so easy to support this from a workflow without doing a ton of code. Now I can do inserts and updates operations to reporting tables and then build reports off of them using the either Excel Services, SSRS, etc.

I am not going to go over the steps on how to create a SQL Table or how to create an external content type using BCS. What I will cover is how to use the external content type from a workflow and discuss one tricky configuration you may not know about.

For this solution you need to:

· Create any sort of SQL tables you need.

· Create external content types to connect to those tables.

· Create external lists on the site to use the external content types.

· Finally I will cover how the workflow will call the external list as a proxy to make transactions against the SQL tables that you will use for reporting.

Insert a Record from a Workflow

There is really nothing special to this at all. Add a Create List Item action into my workflow that references the external list. In my case, the external list I created was called “Review Actions Table”.

clip_image001

Note that I created a variable called ReviewActionID, where I store the SQL generated ID of the new item that was created. If you need to update this SQL record as part of a different workflow process, I recommend updating this value into a column on the item where the workflow is running against.

Updating a Record

Now this is the tricky part. Let’s say later on in the same workflow process you want to update the same SQL record you just created. To accomplish this just add an update action like the following.

clip_image002

The tricky part is the configuration of the update – look at the screen shot below. Notice how I have to select the BDC Identity Field to get the primary key value to perform the update. If you do not do this the update will fail (sorry do not have the error log handy).

Initially I had set it to use the ID Field on the external content type thinking it would have the generated key from the SQL table as part of the original insert I did early. However that was not the case.

Note you do not need to create this BDC Identity column; it is auto-generated for you.

clip_image003

One more note, I had to do this for a SQL Server table with an auto-generated primary key. I have not tested this out for other scenarios like a web service or another DB platform.

Finally let’s say I need to update an external list item on a record that was created prior to the workflow being started. Add an Update List Item action to the external list like the following. Notice here for the Field I use the ID column from the external content type. We do not have to do what we did earlier.

clip_image004

Conclusions

Why do I get so excited about this? Now I am able to start dumping data out into SQL server to do reporting with SharePoint 2010 Business Intelligence solutions. I can create reports with Excel Services or SSRS. In this example I used SSRS using Report Builder 3.0 to create reports about the execution of my business process.

For instance I created an SSRS report that shows the average duration of steps in the business process. I embedded the reporting into SharePoint and I was off and running.

clip_image006

I then created another report that shows the duration by business process instance.

clip_image008

I even created another report that shows how employees perform.

clip_image009

I just embedded these reports into SharePoint 2010 and I was done. Now I can provide some real business insight into process execution.

Part 3 – Delays for Workflow Initiation Action

· Part 1 - Pattern for Building Stateful Workflows with SharePoint Designer 2010
· Part 2 - Custom Action for Workflow Initiation
· Part 3 - Delays for Workflow Initiation Action
· Part 4 - Using Business Connectivity Services (BCS) with SharePoint 2010 Workflow
· Part 5 - Custom Visio Services Reports with SharePoint 2010 Workflow

Introduction

In the previous part of this blog series I provided an approach for chaining together a bunch of sequential workflows together so that I can create a “state like” workflow without having to use Visual Studio. However one thing I did not go over was a few configurations you will have to do to actually start working with data.

One important issue I would run into with my approach was that workflows would start to trip over each other. For instance I have workflow A and workflow B associated to a list. In this case, workflow A would start workflow B. Every once and a while the workflows would fail for no apparent reason. Digging through the logs I would find errors like this which would not tell me much.

OWSTIMER.EXE (0x0A00) 0x0968 SharePoint Foundation Workflow Infrastructure 88xr Unexpected WinWF Internal Error, terminating workflow Id# 13531682-5bef-4194-aaf6-8cf32b31345d

OWSTIMER.EXE (0x0A00) 0x0968 SharePoint Foundation Workflow Infrastructure 98d4 Unexpected System.ArgumentException: listItemKey at Microsoft.SharePoint.WorkflowActions.WaitForDocumentUnlockActivity.Initialize(WorkflowContext context, Guid listID, SPItemKey listItemKey) at Microsoft.SharePoint.WorkflowActions.WithKey.UpdateItemActivity.Execute(ActivityExecutionContext provider) at System.Workflow.ComponentModel.ActivityExecutor`1.Execute(T activity, ActivityExecutionContext executionContext) at System.Workflow.ComponentModel.ActivityExecutor`1.Execute(Activity activity, ActivityExecutionContext executionContext) at System.Workflow.ComponentModel.ActivityExecutorOperation.Run(IWorkflowCoreRuntime workflowCoreRuntime) at System.Workflow.Runtime.Scheduler.Run()

Solution

The solution is simple; if you are going to use the approach that I outlined earlier in this series you will need to introduce some delays into your workflows.

For instance in Workflow A, I put in the following delay before my custom action to initiate a workflow.

image

Then in Workflow B, I put another delay.

clip_image001

This way I am ensured that if there are any lagging transactions are completed against the list item that both workflows are working on.

You will also have to configure the interval workflow timer job in Central Admin. The default is 5 minutes and I upped it to one minute in my development environment.

clip_image003

There is no perfect configuration however you will need to make the correct configuration to make sure everything works well. You need to introduce enough delays and configure the workflow service to make sure you do not get these locking issues.

Saturday, July 30, 2011

Part 2 – Custom Action for Workflow Initiation

· Part 1 - Pattern for Building Stateful Workflows with SharePoint Designer 2010
· Part 2 - Custom Action for Workflow Initiation
· Part 3 - Delays for Workflow Initiation Action
· Part 4 - Using Business Connectivity Services (BCS) with SharePoint 2010 Workflow
· Part 5 - Custom Visio Services Reports with SharePoint 2010 Workflow

Part 2

Building the Custom Action

In the previous part of this blog I explained my approach and in this part I am going to show you how I created two custom actions that is needed to support my approach. I am first going to show you the code solution I created and then dive a bit into why I made the solution as is.

I can tell you honestly that I really wanted this solution to be a SharePoint 2010 Sandbox solution however I ran into a few challenges that prevented me from doing that. This forced me to build a full trust solution with Visual Studio 2010. There are basically two actions I needed to create. The first one is an action that can initiate another workflow. The second action I needed checks to see if 1 to N workflows are running on a list item, and if so, stop the running of that workflow.

It is pretty well documented on how to create a custom action, deploy it and then use that action in SharePoint Designer. The following are some good references:

· http://blog.voltje.be/?p=144

· http://msmvps.com/blogs/sundar_narasiman/archive/2010/12/26/develop-custom-workflow-activity-for-sharepoint-2010-workflow.aspx

Workflow Helper Class

First thing I did was create a simple helper class where I stored logic to initiate the workflow and to check to see if there are any existing workflows running. The ItemHasActiveWorkflows method is really straight forward as it is searching workflows that are running for the specific item. If it finds a workflow is running, it will return false. Why is this important? Well because a document or list item can only have one of a particular type running at a time. But since we broke the larger business process up into five smaller ones, we need to check to see if any of the five are running on the item. If any one of them is running, we need to block any new workflows from being started. This method will take a comma delimited list of workflow names that must be checked.

The second method is StartWorkflow which will initiate a workflow on a specified item. This code is very straight forward.

public class WorkflowHelper
{

private string siteCollectionUrl;
private string webUrl;
private string listName;
private int itemID;
private string workflowName;

public WorkflowHelper(string siteCollectionUrl, string webUrl, string listName, int itemID)
{
this.siteCollectionUrl = siteCollectionUrl;
this.webUrl = webUrl;
this.listName = listName;
this.itemID = itemID;
}

public WorkflowHelper(string siteCollectionUrl, string webUrl, string listName, int itemID, string workflowName) : this(siteCollectionUrl, webUrl, listName, itemID)
{
this.workflowName = workflowName;
}

public bool ItemHasActiveWorkflows(string workflowNames)
{
string[] seperator = new string[] {","};
string[] workflowNamesSplit = workflowNames.Split(seperator, StringSplitOptions.RemoveEmptyEntries);

//Get the site collection
using (SPSite site = new SPSite(siteCollectionUrl))
{
//Get the web
using (SPWeb web = site.OpenWeb(webUrl))
{
SPList list = web.Lists[listName];
SPListItem workflowItem = list.GetItemById(itemID);

//Loop over the workflows to look for
foreach (string workflowName in workflowNamesSplit)
{
//Loop over the workflows on the item
foreach (SPWorkflow workflow in workflowItem.Workflows)
{
//Check the state of the workflows to see if any are running
if (workflow.InternalState == SPWorkflowState.Running ||
workflow.InternalState == SPWorkflowState.Expiring ||
workflow.InternalState == SPWorkflowState.Faulting)
{
if (list.WorkflowAssociations[workflow.AssociationId].Name.ToLower() == workflowName.Trim().ToLower())
{
return true;
}
}
}
}
}
}

return false;
}

public void StartWorkflow()
{
//Open the site collection. Cannot go across site collections in
//sandbox so easier to just pull of workflow context
using (SPSite site = new SPSite(siteCollectionUrl))
{
//Just open web because we cannot go across site collections
//with a sandbox solution.
using (SPWeb web = site.OpenWeb(webUrl))
{
//Get the manager that will initiate the workflow
SPWorkflowManager workflowMgr = site.WorkflowManager;

//Get the available workflows on the list
SPWorkflowAssociationCollection workflowAssocs = web.Lists[listName].WorkflowAssociations;

//Get the list item that the workflow will be initiated on
SPListItem workflowItem = web.Lists[listName].GetItemById(itemID);

//Get the workflow that has been associated
SPWorkflowAssociation workflowAssoc = workflowAssocs.GetAssociationByName(workflowName, System.Threading.Thread.CurrentThread.CurrentCulture);

//Start the workflow
workflowMgr.StartWorkflow(workflowItem, workflowAssoc, workflowAssoc.AssociationData, true);
}
}
}
}


Start Workflow Action



Next I created an action that would start a second workflow. I created the following code.




public class InitWorkflowAction : Activity
{

#region Properties

public static DependencyProperty SiteCollectionUrlProperty = DependencyProperty.Register("SiteCollectionUrl", typeof(string), typeof(InitWorkflowAction));

[Description("Site Collection URL")]
[Category("Custom Workflow")]
[Browsable(true)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public string SiteCollectionUrl
{
get
{
return ((string)(base.GetValue(InitWorkflowAction.SiteCollectionUrlProperty)));
}
set
{
base.SetValue(InitWorkflowAction.SiteCollectionUrlProperty, value);
}
}

public static DependencyProperty WebUrlProperty = DependencyProperty.Register("WebUrl", typeof(string), typeof(InitWorkflowAction));

[Description("Web Url")]
[Category("Custom Workflow")]
[Browsable(true)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public string WebUrl
{
get
{
return ((string)(base.GetValue(InitWorkflowAction.WebUrlProperty)));
}
set
{
base.SetValue(InitWorkflowAction.WebUrlProperty, value);
}
}

public static DependencyProperty ListNameProperty = DependencyProperty.Register("ListName", typeof(string), typeof(InitWorkflowAction));

[Description("List Name")]
[Category("Custom Workflow")]
[Browsable(true)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public string ListName
{
get
{
return ((string)(base.GetValue(InitWorkflowAction.ListNameProperty)));
}
set
{
base.SetValue(InitWorkflowAction.ListNameProperty, value);
}
}

public static DependencyProperty WorkflowNameProperty = DependencyProperty.Register("WorkflowName", typeof(string), typeof(InitWorkflowAction));

[Description("Workflow Name")]
[Category("Custom Workflow")]
[Browsable(true)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public string WorkflowName
{
get
{
return ((string)(base.GetValue(InitWorkflowAction.WorkflowNameProperty)));
}
set
{
base.SetValue(InitWorkflowAction.WorkflowNameProperty, value);
}
}

public static DependencyProperty ItemIDProperty = DependencyProperty.Register("ItemID", typeof(int), typeof(InitWorkflowAction));

[Description("Item ID")]
[Category("Custom Workflow")]
[Browsable(true)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public int ItemID
{
get
{
return ((int)(base.GetValue(InitWorkflowAction.ItemIDProperty)));
}
set
{
base.SetValue(InitWorkflowAction.ItemIDProperty, value);
}
}


#endregion

#region Methods

protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
{
//WorkflowHelper workflowHelper = new WorkflowHelper(this.SiteCollectionUrl, this.WebUrl, this.ListName, this.ItemID, this.WorkflowName);
//workflowHelper.StartWorkflow();

WorkflowHelper workflowHelper = new WorkflowHelper(this.SiteCollectionUrl, this.WebUrl, this.ListName, this.ItemID, this.WorkflowName);
Thread thread = new Thread(new ThreadStart(workflowHelper.StartWorkflow));
thread.Start();

return ActivityExecutionStatus.Closed;
}

#endregion

}


With the StartWorkflow method you see the code is straight forward but there some threading. You may be wondering why I have implemented threading? Well I was getting the below error message in the SharePoint logs when the workflow was initiated on the same thread. I did not find any information on why this was occurring but on a hunch I thought the workflow service was competing for the same resources. My solution was to move the call to StartWorkflow into a different thread and the problem was resolved.



w3wp.exe (0x2BC4) 0x0A2C SharePoint Foundation Workflow Infrastructure xmfh Medium Workflow Compile Failed: Invalid token for impersonation - it cannot be duplicated.



w3wp.exe (0x2BC4) 0x0A2C SharePoint Foundation Workflow Infrastructure 72fs Unexpected RunWorkflow: Microsoft.SharePoint.SPException: Invalid token for impersonation - it cannot be duplicated. at Microsoft.SharePoint.Workflow.SPNoCodeXomlCompiler.SubCompiler.DoCompile(WorkflowCompilerParameters parameters, String xomlSource, String assemblyName, CompilationPacket& packet, DirectoryInfo& tempDir) at Microsoft.SharePoint.Workflow.SPNoCodeXomlCompiler.CompileBytes(Byte[] xomlBytes, Byte[] rulesBytes, Boolean doTestCompilation, String assemblyName, SPWeb web, Boolean forceNewAppDomain) at Microsoft.SharePoint.Workflow.SPNoCodeXomlCompiler.LoadXomlAssembly(SPWorkflowAssociation association, SPWeb web) at Microsoft.SharePoint.Workflow.SPWinOeHostServices.LoadDeclarativeAssembly(SPWorkflowAssociation association) at Microsoft.SharePoint.Workflow.SPWinOeHostServi...



w3wp.exe (0x2BC4) 0x0A2C SharePoint Foundation Workflow Infrastructure 72fs Unexpected ...ces.CreateInstance(SPWorkflow workflow) at Microsoft.SharePoint.Workflow.SPWinOeEngine.RunWorkflow(SPWorkflowHostService host, SPWorkflow workflow, Collection`1 events, TimeSpan timeOut) at Microsoft.SharePoint.Workflow.SPWorkflowManager.RunWorkflowElev(SPWorkflow workflow, Collection`1 events, SPWorkflowRunOptionsInternal runOptions)



0x0A2C SharePoint Foundation Workflow Infrastructure 98d8 Unexpected Microsoft.SharePoint.SPException: Invalid token for impersonation - it cannot be duplicated. at Microsoft.SharePoint.Workflow.SPNoCodeXomlCompiler.SubCompiler.DoCompile(WorkflowCompilerParameters parameters, String xomlSource, String assemblyName, CompilationPacket& packet, DirectoryInfo& tempDir) at Microsoft.SharePoint.Workflow.SPNoCodeXomlCompiler.CompileBytes(Byte[] xomlBytes, Byte[] rulesBytes, Boolean doTestCompilation, String assemblyName, SPWeb web, Boolean forceNewAppDomain) at Microsoft.SharePoint.Workflow.SPNoCodeXomlCompiler.LoadXomlAssembly(SPWorkflowAssociation association, SPWeb web) at Microsoft.SharePoint.Workflow.SPWinOeHostServices.LoadDeclarativeAssembly(SPWorkflowAssociation association) at Microsoft.SharePoint.Workflow.SPWinOeHostServices.CreateIns...



w3wp.exe (0x2BC4) 0x0A2C SharePoint Foundation Workflow Infrastructure 98d8 Unexpected ...tance(SPWorkflow workflow) at Microsoft.SharePoint.Workflow.SPWinOeEngine.RunWorkflow(SPWorkflowHostService host, SPWorkflow workflow, Collection`1 events, TimeSpan timeOut) at Microsoft.SharePoint.Workflow.SPWorkflowManager.RunWorkflowElev(SPWorkflow workflow, Collection`1 events, SPWorkflowRunOptionsInternal runOptions)



Check Active Workflows Action



Below is the code that will check to see if there is an action running on a workflow item. The code is straight forward and calls my helper class that I created earlier.




public class CheckActiveWorkflowsAction : Activity
{

#region Properties

public static DependencyProperty SiteCollectionUrlProperty = DependencyProperty.Register("SiteCollectionUrl", typeof(string), typeof(CheckActiveWorkflowsAction));

[Description("Site Collection URL")]
[Category("Custom Workflow")]
[Browsable(true)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public string SiteCollectionUrl
{
get
{
return ((string)(base.GetValue(CheckActiveWorkflowsAction.SiteCollectionUrlProperty)));
}
set
{
base.SetValue(CheckActiveWorkflowsAction.SiteCollectionUrlProperty, value);
}
}

public static DependencyProperty WebUrlProperty = DependencyProperty.Register("WebUrl", typeof(string), typeof(CheckActiveWorkflowsAction));

[Description("Web Url")]
[Category("Custom Workflow")]
[Browsable(true)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public string WebUrl
{
get
{
return ((string)(base.GetValue(CheckActiveWorkflowsAction.WebUrlProperty)));
}
set
{
base.SetValue(CheckActiveWorkflowsAction.WebUrlProperty, value);
}
}

public static DependencyProperty ListNameProperty = DependencyProperty.Register("ListName", typeof(string), typeof(CheckActiveWorkflowsAction));

[Description("List Name")]
[Category("Custom Workflow")]
[Browsable(true)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public string ListName
{
get
{
return ((string)(base.GetValue(CheckActiveWorkflowsAction.ListNameProperty)));
}
set
{
base.SetValue(CheckActiveWorkflowsAction.ListNameProperty, value);
}
}

public static DependencyProperty ItemIDProperty = DependencyProperty.Register("ItemID", typeof(int), typeof(CheckActiveWorkflowsAction));

[Description("Item ID")]
[Category("Custom Workflow")]
[Browsable(true)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public int ItemID
{
get
{
return ((int)(base.GetValue(CheckActiveWorkflowsAction.ItemIDProperty)));
}
set
{
base.SetValue(CheckActiveWorkflowsAction.ItemIDProperty, value);
}
}

public static DependencyProperty HasActiveProperty = DependencyProperty.Register("HasActive", typeof(bool), typeof(CheckActiveWorkflowsAction));

[Description("Has Active")]
[Category("Custom Workflow")]
[Browsable(true)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public bool HasActive
{
get
{
return ((bool)(base.GetValue(CheckActiveWorkflowsAction.HasActiveProperty)));
}
set
{
base.SetValue(CheckActiveWorkflowsAction.HasActiveProperty, value);
}
}

public static DependencyProperty WorkflowNamesProperty = DependencyProperty.Register("WorkflowNames", typeof(string), typeof(CheckActiveWorkflowsAction));

[Description("Workfow Names")]
[Category("Custom Workflow")]
[Browsable(true)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public string WorkflowNames
{
get
{
return ((string)(base.GetValue(CheckActiveWorkflowsAction.WorkflowNamesProperty)));
}
set
{
base.SetValue(CheckActiveWorkflowsAction.WorkflowNamesProperty, value);
}
}

#endregion

#region Methods

protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
{
WorkflowHelper workflowHelper = new WorkflowHelper(this.SiteCollectionUrl, this.WebUrl, this.ListName, this.ItemID);
this.HasActive = workflowHelper.ItemHasActiveWorkflows(this.WorkflowNames);

return ActivityExecutionStatus.Closed;
}

#endregion
}


Deployment



The deployment of this solution was pretty easy with Visual Studio 2010.



· I created an empty SharePoint 2010 project.



· Added the above code.



· Added a Site Collection level Feature.



· Added a SharePoint mapped folder called “Workflow”



· Create a new file called InitWorkflowAction.actions in the Workflow folder.



The InitWorkflowAction.actions file describes my custom actions and makes them available to design tools such as SharePoint Designer 2010 to create a declarative workflow. Information below is very straight forward. It defines the sentence users will fill in and what type of data will be consumed or returned from the methods I created earlier.




<WorkflowInfo>
<Actions Sequential="then" Parallel="and">
<Action Name="Start Workflow"
Assembly="My.Activities, Version=1.0.0.0, Culture=neutral, PublicKeyToken=7cb64b163bd9c1db"
ClassName="My.Activities.InitWorkflowAction"
AppliesTo="all"
Category="Custom Workflow">
<RuleDesigner Sentence="Initiate workflow %4 on item %5 in list %3 in Web %2 in Site Collection %1">
<FieldBind Field="SiteCollectionUrl"
Text="Site Collection Url"
Id="1"
DesignerType="TextBox" />
<FieldBind Field="WebUrl"
Text="Web Url"
Id="2"
DesignerType="TextBox" />
<FieldBind Field="ListName"
Text="List Name"
Id="3"
DesignerType="TextBox" />
<FieldBind Field="WorkflowName"
Text="Workflow Name"
Id="4"
DesignerType="TextBox" />
<FieldBind Field="ItemID"
Text="Item ID"
Id="5"
DesignerType="Integer" />
</RuleDesigner>
<Parameters>
<Parameter Name="SiteCollectionUrl"
Type="System.String, mscorlib"
Direction="In"
DesignerType="TextBox"
Description="Site Collection Url"/>
<Parameter Name="WebUrl"
Type="System.String, mscorlib"
Direction="In"
DesignerType="TextBox"
Description="Web Url"/>
<Parameter Name="ListName"
Type="System.String, mscorlib"
Direction="In"
DesignerType="TextBox"
Description="List Name"/>
<Parameter Name="WorkflowName"
Type="System.String, mscorlib"
Direction="In"
DesignerType="TextBox"
Description="Workflow Name"/>
<Parameter Name="ItemID"
Type="System.Int32, mscorlib"
Direction="In"
DesignerType="Integer"
Description="Item ID"/>
</Parameters>
</Action>
<Action Name="Check Active Workflow"
Assembly="My.Activities, Version=1.0.0.0, Culture=neutral, PublicKeyToken=7cb64b163bd9c1db"
ClassName="My.Activities.CheckActiveWorkflowsAction"
AppliesTo="all"
Category="Custom Workflow">
<RuleDesigner Sentence="Check if workflows %6 on item item %4 in list %3 in Web %2 in Site Collection %1 has a running workflow (Output to %5)">
<FieldBind Field="SiteCollectionUrl"
Text="Site Collection Url"
Id="1"
DesignerType="TextBox" />
<FieldBind Field="WebUrl"
Text="Web Url"
Id="2"
DesignerType="TextBox" />
<FieldBind Field="ListName"
Text="List Name"
Id="3"
DesignerType="TextBox" />
<FieldBind Field="ItemID"
Text="Item ID"
Id="4"
DesignerType="Integer" />
<FieldBind Field="HasActive"
Text="Has Active"
Id="5"
DesignerType="ParameterNames" />
<FieldBind Field="WorkflowNames"
Text="Comma delimited list of workflow names"
Id="6"
DesignerType="TextBox" />
</RuleDesigner>
<Parameters>
<Parameter Name="SiteCollectionUrl"
Type="System.String, mscorlib"
Direction="In"
DesignerType="TextBox"
Description="Site Collection Url"/>
<Parameter Name="WebUrl"
Type="System.String, mscorlib"
Direction="In"
DesignerType="TextBox"
Description="Web Url"/>
<Parameter Name="ListName"
Type="System.String, mscorlib"
Direction="In"
DesignerType="TextBox"
Description="List Name"/>
<Parameter Name="ItemID"
Type="System.Int32, mscorlib"
Direction="In"
DesignerType="Integer"
Description="Item ID"/>
<Parameter Name="HasActive"
Type="System.Boolean, mscorlib"
Direction="In"
DesignerType="ParameterNames"
Description="Has Active"/>
<Parameter Name="WorkflowNames"
Type="System.String, mscorlib"
Direction="In"
DesignerType="TextBox"
Description="Comma delimited list of workflow names"/>
</Parameters>
</Action>
</Actions>
</WorkflowInfo>


Using Custom Actions in SharePoint Designer 2010



Now I am going to show you how I used the custom actions in my workflow. Earlier in this series you saw that I had created five different SharePoint Visio 2010 workflow diagrams. I am going to skip documenting the steps on how to bring the Visio diagrams into SharePoint Designer as that is a pretty straight forward process.



Below is a screenshot of the Initiate Process workflow. The first Action is my custom action that checks to see if there is already a workflow running for the custom list item. You will notice that the sentence displayed here corresponds with the sentence in the InitWorkflowAction.actions file. Also note the first parameter is a comma delimited list of all the other workflows (Review,Manager Review,Resubmit Request,Complete Request).



Next I take the value returned from my custom action and check to see if it Yes or No. If it is No, that means there are no running workflows and that I should initiate the Review Workflow. The third sentence in the diagram below captures this. I will initiate the Review workflow on the current item and send a notification email. Otherwise the workflow will not be started and the user will be notified.



image



Note that for the configuration of this workflow, it is the only one that supports allowing the workflow to be started manually. All of the other workflows I have created have no checkboxes checked for the Start Options section. This reduces the potential for user errors when starting the workflow for the item. The reason why is that we always want users to start with the Initiate Process workflow and let the other workflows be started based on business rules.



clip_image002



Next I have another workflow called Review that corresponds to the Visio diagram in the previous part of this series. Notice again I simply wire into the custom action StartWorkflow to start Manager Review, Complete Review or Resubmit Request.



clip_image004



Below is the Manager Review workflow. As you can see if the Manager approves the Complete Request workflow will be called otherwise the Resubmit Request workflow will be called.



clip_image006



Next is the Resubmit Request workflow. The workflow checks to see if the user cancels the workflow. If they do, the item will be updated and an email will be sent. Otherwise, the item will go back to the Review workflow (for re-review).



clip_image008



Finally the Complete Request workflow is below. I have not done much with it at this time but more can be added.



clip_image010



So now once I validate and deploy my workflow I can initiate it on an item. Below is a screenshot of a user initiate the process for an item. Notice only the Initiate Process workflow is available.



clip_image012



Once I have started the workflow I come back to the workflow status screen. Here you see the Review workflow is In Progress. Notice that the Initiate Process workflow is still available. Since the workflow is in progress I want to make sure that I cannot initiate the entire workflow again while it is current running.



clip_image014



Below is a screenshot of the completed workflow.



clip_image016



Now you can see I was able to chain together several workflows that as a whole make up a larger business process. Pretty neat.



Sandbox Solutions



I alluded earlier that I really wanted to make this a sandbox solution. Why? Because I believe that you should always develop for the Sandbox first because it quick to deploy solutions, it is secure, ensures good performance and works well with Office365.



Here are some good resources on how to deploy actions into the SharePoint 2010 Sandbox:



· http://msdn.microsoft.com/en-us/library/ff798499.aspx



· http://msdn.microsoft.com/en-us/library/ff630175.aspx



· http://msdn.microsoft.com/en-us/library/ff798389.aspx



· http://www.wictorwilen.se/Post/Sandboxed-workflow-activities-in-SharePoint-2010.aspx



The process is a little different and the code above would change a tiny bit so that it is scoped to a Site Collection only. However fundamentally there is not much difference.



When I deployed the custom actions and created the workflows I would get errors initiating the workflows. I went into the logs and found the following.



SPUCWorkerProcessProxy.exe (0x1E70) 0x1EC0 SharePoint Foundation Workflow Infrastructure 72er Medium System.NullReferenceException: Object reference not set to an instance of an object. at Microsoft.SharePoint.Library.SPRequest.ClearAllVars(String bstrUrl) at Microsoft.SharePoint.SPListItem.PrepareItemForUpdate(SPWeb web, Boolean bMigration, Boolean& bAdd, Boolean& bPublish, Object& objAttachmentNames, Object& objAttachmentContents, Int32& parentFolderId) at Microsoft.SharePoint.SPListItem.UpdateInternal(Boolean bSystem, Boolean bPreserveItemVersion, Guid newGuidOnAdd, Boolean bMigration, Boolean bPublish, Boolean bNoVersion, Boolean bCheckOut, Boolean bCheckin, Boolean suppressAfterEvents, String filename) at Microsoft.SharePoint.SPListItem.Update() at Microsoft.SharePoint.Workflow.SPWinOEWSSService.CommitUpdateListItem(Transaction txn, Object[] transData)



SPUCWorkerProcessProxy.exe (0x1E70) 0x1EC0 SharePoint Foundation Workflow Infrastructure 72fe High Error in commiting pending workflow batch items: System.NullReferenceException: Object reference not set to an instance of an object. at Microsoft.SharePoint.Library.SPRequest.ClearAllVars(String bstrUrl) at Microsoft.SharePoint.SPListItem.PrepareItemForUpdate(SPWeb web, Boolean bMigration, Boolean& bAdd, Boolean& bPublish, Object& objAttachmentNames, Object& objAttachmentContents, Int32& parentFolderId) at Microsoft.SharePoint.SPListItem.UpdateInternal(Boolean bSystem, Boolean bPreserveItemVersion, Guid newGuidOnAdd, Boolean bMigration, Boolean bPublish, Boolean bNoVersion, Boolean bCheckOut, Boolean bCheckin, Boolean suppressAfterEvents, String filename) at Microsoft.SharePoint.SPListItem.Update() at Microsoft.SharePoint.Workflow.SPWinOEWSSService.CommitUpdateListItem...



SPUCWorkerProcessProxy.exe (0x1E70) 0x1EC0 SharePoint Foundation Workflow Infrastructure 72fe High ...(Transaction txn, Object[] transData) at Microsoft.SharePoint.Workflow.SPPendingWorkBatch.Commit(Transaction transaction, ICollection items)



SPUCWorkerProcessProxy.exe (0x1E70) 0x1EC0 SharePoint Foundation Workflow Infrastructure 88xr Unexpected WinWF Internal Error, terminating workflow Id# 8d547a01-bdb8-44ac-b066-586c12367dc0



The information that was provided was not very good. Here are some observations I made.



· Note that the error is in SPUCWorkerProcessProxy.exe and not w3wp.exe. This basically means the entire workflow is running inside the Sandbox service because it has a reference to an action that is deployed there.



· I would only get this error the very first time the workflow run. From then on, it worked every time flawlessly. However if I stopped and restarted the Sandbox service manually, I would get the error again.



· I would also not get the error if Workflow A started a very simple Workflow B. For instance if Workflow B was sending an email, no issues. However if Workflow B had an approval process there would be an error.



After doing some research I came to the conclusion that this error occurs based on a combination of a few things.



1. When you build a workflow in SharePoint Designer 2010 it is a declarative workflow, not a complied workflow. The workflow will be compiled the first time the workflow is initiated. This is why it always takes a long time to start a workflow the very first time.



2. Sandbox solutions have a threshold of 20 seconds before it is determined the process too intensive and will stop the execution.



This combination would push me over the 20 seconds rule the very first time running the workflow. The solution would be to write some sort of script that could manually go through each workflow and initiating them so they would be compiled and ready to go when the user comes to manually start the workflow. I did not find that to be an acceptable solution because the Sandbox cache will be remove items from memory if they are not used often. This is why I did a full trust solution.



Next Part



Hopefully this gave you a pretty good idea of how simple it is to achieve this vision I have. Now in the next part of this series I am going to extend the solution a little bit to support my reporting requirements using Visio Services.

Part 1 – Pattern for Building Stateful Workflows with SharePoint Designer 2010

· Part 1 - Pattern for Building Stateful Workflows with SharePoint Designer 2010
· Part 2 - Custom Action for Workflow Initiation
· Part 3 - Delays for Workflow Initiation Action
· Part 4 - Using Business Connectivity Services (BCS) with SharePoint 2010 Workflow
· Part 5 - Custom Visio Services Reports with SharePoint 2010 Workflow


Introduction
In the SharePoint 2007 days moderately complex workflow could not be achieved with SharePoint Designer 2007. Individuals would then try to build those workflows using Visual Studio 2007 and then run into a whole new set of complexities. In many cases individuals wanted to use visual tools to build automation inside of SharePoint 2007 only to find out that there were limitations. The end result was developers reverting back to adding event handlers to lists or writing web parts with a custom database.


With SharePoint 2010 workflow tools have matured allowing us to do more complex business process automation. Most notably:


1. Visio 2010 is now a tool that can be used to visually design workflows definitions that can be pulled into SharePoint. It also provides the ability to do reporting and data visualization where graphics can be mapped to data. Then based on the value of that data, conditional logic can be applied to change visual element in Visio (like change something from green to red, or change the shape completely).


2. SharePoint Designer 2010 workflow design tool has significantly improved tremendously. There is Visio integration, InfoPath form integration (initiation, association and task forms), better workflow designer, more workflow fields to interact with, etc. It is almost night and day versus the previous version of SharePoint Designer.


3. Visual Studio 2010 has changed tremendously providing workflow project templates which drastically reduce the level of effort to create workflow process.


4. Workflow definitions can be moved between tools fairly easily only a few limitations.


5. Workflow can be easily redeployed between dev, test and production.


6. Workflow can now run at the site level instead of only being associated to a list item.


One Big Hurdle Left


At the end of the day this is all well and good but there still some holes. Specifically you cannot do state workflows in Visio and SharePoint Designer 2010. SharePoint Designer only supports sequential works. This basically means the flow goes top to down and cannot loop back within the workflow. Not having this capability can be limiting even for a simple workflow process.


The Resolution


Have some assumptions you must take think about with this solution:


1. Let me state first that my solution is pretty clean and simple. But I really only recommend this as a solution for those simple to moderately complex workflows where you just want to be able to achieve stateful workflows without having to do a bunch of custom code.


2. Much of this solution is based on leveraging the OOB capabilities of SharePoint 2010.


3. There is a lot room for improvement of this solution so please use this as a framework to start trying to build some stateful workflows. I will NOT be supporting this solution moving forward. This is just a demonstration of showing you how you can achieve a great solution by using the SharePoint 2010 toolset.


After do some brainstorming the following two things dawned on me:


1. State is relative to the business process instance data; not the business process definition itself. What does that mean? It means you do not have to create a single process definition to manage an entire business process state. Still does not make sense? What I am saying is you do not have to create a single workflow diagram to automate a business process. Instead you can create multiple workflows and chain them together. All of these workflows will manage a single stateful item (record); like a SharePoint list item or document. I like this idea because in actuality state workflows are chained together sequential processes when it comes down to it. My goal is to come up with a pattern that will allow me to daisy chain together many sequential workflows that manage the state of a single item to go through a workflow. Doing this this will allow me to achieve my vision of building workflow in SharePoint Designer 2010 and Visio 2010.


2. The workflow diagram never looks the same as the automation workflow diagram. Visual workflow designer tools are positioned to allow business users to take their process diagrams (box, diamond and arrow diagrams) and quickly transform it into an executable that can run in some system. That is not really easy. The reality is that business person’s workflow diagram never, ever looks like the automation workflow diagram created in a workflow tool. This is because there will be tons of steps added into the diagram to support the actual automation. Using Visio Services in SharePoint 2010, I will be able to come up with a visual report.


I believe I have actually come up with a pretty simple solution change solves these challenges. The following are the elements of this solution.


· Use Visio 2010 to build up the initial workflow definition and then use the SharePoint Designer 2010 to connect the workflow to SharePoint 2010.
· Create custom workflow activity with Visual Studio 2010 that can initiate another workflow. Basically we need to create a simple .NET function that can take some parameters and then initiate a workflow. This will allow for us to chain workflows together.
· Use Visio 2010 for reporting by using its new capability to map to data. We can create a picture that the business user can understand and have it “light up” and report to a user about the current state of a business process execution.


Ok – Show Me What You Are Talking About


Is this still too complicated? Have I gotten a little nerdy on the workflow? Let’s put this into context. Below is a pretty simple review workflow by most standards.


· There is a review step.
· If the item being reviewed is greater than $20K, a manager needs to review it.
· If a request is rejected by the reviewer or manager, the request should go back to the originator for resubmission.
· Only the requestor has the ability to cancel the request.


image


So you may say what is the big deal? Why did you write all that esoteric stuff about workflow for something as simple as this? Really? Look at the diagram below, the stuff I highlighted in red could not be achieved in a tool that only supports sequential workflow. Because there is a loop back to the initiator to allow them to resubmit this immediately becomes a state workflow.


image


As well, notice anything else about either of these diagrams? They are simple and what the business users understands. There is nothing in these diagrams that show emails being sent, task items being created, permissions being set, item metadata being updated, data being sent to a database for reporting, etc. If I were to take this diagram and try to model all those steps I can assure you it will become a clutter mess!!!


Solution Design


I am excited to say my solution solves both of these problems!!! First I will be able to break this up into a bunch of connected workflows. Second I will be able to take the original diagram we draw and light it up based on where the workflow is.


So how does the solution work? I will get to the real “how does this work in SharePoint 2010” shortly. First we need to break this workflow into multiple workflows. In the screenshot below you will see that I have broken the business process into five somewhat smaller workflows.


· Initiate – This process will have the responsibility of starting the workflow and performing any activities need to get it going.
· Review – This is the part where the main reviewer reviews the request.
· Manager Review – Workflow that manages the workflow for the manager to review.
· Resubmit – Consolidates the logic for either resubmitting the request or canceling it.
· Complete – Will handle any operations needed to complete the workflow.


image


When I was designing this solution I was able to test approach with many other more sophisticated workflow patterns (i.e. complex parallel processing or a spider diagram). I was always able to break up the workflow into a set of sequential workflows that could be coordinated with each other.


Building the SharePoint Workflows with Visio 2010


Many of the readers of this blog are aware that they can build SharePoint workflow in Visio 2010. For this solution I will use it to initially build the workflows above however I will not use it for the long term. One limitation is that I will have to build two custom actions in Visual Studio 2010. Custom actions are not supported in Visio 2010 but they are supported in SharePoint Designer 2010.


Initiate Process


Below is the initiate process. Not much going on here other than an email is being sent.


image


However there are several other things we will need to add when we move this into SharePoint Designer. For instance check to see if any of the other processes are currently running. We have to use a custom action to achieve this which cannot be modeled in Visio.


Review Process


Below is the review process. As you can see I have added some steps to update the state of the Review list item and send an email. Like before, I could not add the actions to initiate the other workflows but I have put in placeholders.


image


Manager Review


Next I have the manager review. Again I have added some steps to update the review item state, send some emails and put in a placeholder to initiate other workflows.


image


Resubmit Process


Below is the resubmit process. Again I have added actions that send emails and update the state of the record.


image


Complete Workflow


Finally we have the complete workflow; pretty self-explanatory.
image


What’s Next?


Well what we now need to do is bring these workflows into SharePoint Designer 2010 and use some custom actions that can initiate each workflow. In the next part of the series I am going to focus on how these custom actions were built.