Thursday, December 15, 2011

Workflow – commit all pending changes

Some workflow actions don’t run immediately. They are queued in workflow batch and executed when the workflow commits. Typical batched actions are “Set Field in Current Item” and “Update List Item”. If you need commit pending changes, you can use the following action.

using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Workflow.Activities;
using System.Workflow.ComponentModel;
using System.Workflow.ComponentModel.Compiler;
using Microsoft.SharePoint.Workflow;
using Microsoft.SharePoint.WorkflowActions;

namespace Sample.SharePoint.WorkflowActions
{
[PersistOnClose]
public partial class CommitActivity : Activity
{
public CommitActivity()
{
InitializeComponent();
}

protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
{
return ActivityExecutionStatus.Closed;
}

protected override ActivityExecutionStatus HandleFault(ActivityExecutionContext executionContext, Exception exception)
{
try
{
ISharePointService service = (ISharePointService)executionContext.GetService(typeof(ISharePointService));
if (service == null)
{
Trace.WriteLine("CommitActivity.HandleFault: ISharePointService is null");
}
else
{
service.LogToHistoryList(base.WorkflowInstanceId, SPWorkflowHistoryEventType.WorkflowComment, 0, TimeSpan.MinValue, "Error", "Commit pending changes error: " + exception.Message, string.Empty);
}
}
catch (Exception ex)
{
Trace.WriteLine("CommitActivity.HandleFault: " + ex.Message);
}
return base.HandleFault(executionContext, exception);
}

public static DependencyProperty __ContextProperty = DependencyProperty.Register("__Context", typeof(WorkflowContext), typeof(CommitActivity));
public static DependencyProperty __ListIdProperty = DependencyProperty.Register("__ListId", typeof(string), typeof(CommitActivity));
public static DependencyProperty __ListItemProperty = DependencyProperty.Register("__ListItem", typeof(SPItemKey), typeof(CommitActivity));

[ValidationOption(ValidationOption.Required), DesignerSerializationVisibility(DesignerSerializationVisibility.Visible), Browsable(true)]
public WorkflowContext __Context
{
get
{
return (WorkflowContext)base.GetValue(__ContextProperty);
}
set
{
base.SetValue(__ContextProperty, value);
}
}

[ValidationOption(ValidationOption.Required), DesignerSerializationVisibility(DesignerSerializationVisibility.Visible), Browsable(true)]
public string __ListId
{
get
{
return (string)base.GetValue(__ListIdProperty);
}
set
{
base.SetValue(__ListIdProperty, value);
}
}

[ValidationOption(ValidationOption.Required), DesignerSerializationVisibility(DesignerSerializationVisibility.Visible), Browsable(true)]
public SPItemKey __ListItem
{
get
{
return (SPItemKey)base.GetValue(__ListItemProperty);
}
set
{
base.SetValue(__ListItemProperty, value);
}
}
}
}

3 comments:

  1. Out of curiosity, and need to understand....
    Why and How does this cause things to be commited?

    ReplyDelete
  2. This activity has PersistOnClose attribute that tells to commit changes after activity has run.

    ReplyDelete
  3. And... where this code should be included in the solution?

    It's supose that extending the Activity class (and after a build I guess), a new item on the Workflow-Toobox would be displayed (that can be included after the desired commiteable action)?

    I am pretty new to Sharepoint with Workflow, so please, be patient with me, and also, with my english!

    Thanks

    ReplyDelete