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.

Friday, July 29, 2011

Office365 SLAs and Service Descriptions

Do you need to get your head wrapped around Office365 and what it provides? Honestly the best resources to read are the Service Level Agreements. For me, I focus a lot on SharePoint. When I read these documents I get a clear picture of what is exactly provided in SharePoint Online Services in the Office365 cloud.