| Step | Description |
| 1 | In visual studio 2010, create a new Project, based on Workflow Activity Library
|
| 2 | This will create a skeleton project
|
| 3 | First we need to add the SharePoint references |
| 3a | Right click reference -> Add reference
|
| 3b | Browse to :
c:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\Isapi
|
| 3c | Add
Microsoft.SharePoint.dll microsoft.sharepoint.WorkflowActions.dll |
| 4 | Rename Activity1 in something more significant (tzRegEx in this example) |
| 5 | Right click the activity and select View Code
|
| 5a | Add the reference to SharePoint in the using section
using Microsoft.SharePoint;
using Microsoft.SharePoint.Workflow;
using Microsoft.SharePoint.WorkflowActions;
|
| 5b |
Add the field and properties for communication with he workflow
#region Fields
public static DependencyProperty __ContextProperty = DependencyProperty.Register("__Context", typeof(WorkflowContext), typeof(tzRegEx));
public static DependencyProperty __ListItemProperty = DependencyProperty.Register("__ListItem", typeof(Int32), typeof(tzRegEx));
public static DependencyProperty __ListIdProperty = DependencyProperty.Register("__ListId", typeof(string), typeof(tzRegEx));
#endregion
#region Properties
[ValidationOption(ValidationOption.Required)]
public WorkflowContext __Context
{
get { return ((WorkflowContext)(base.GetValue(tzRegEx.__ContextProperty))); }
set { base.SetValue(tzRegEx.__ContextProperty, value); }
}
[ValidationOption(ValidationOption.Required)]
public int __ListItem
{
get { return (int)base.GetValue(__ListItemProperty); }
set { base.SetValue(__ListItemProperty, value); }
}
[ValidationOption(ValidationOption.Required)]
public string __ListId
{
get { return (string)base.GetValue(__ListIdProperty); }
set { base.SetValue(__ListIdProperty, value); }
}
#endregion
|
| 5c | Override the execution method
protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
{
ISharePointService service = (ISharePointService)executionContext.GetService(typeof(ISharePointService));
try
{
using (SPWeb web = (SPWeb)(__Context.Web))
{
// get all of the information we currently have about the item
// that this workflow is running on
Guid listGuid = new Guid(__ListId);
SPList myList = web.Lists[listGuid];
SPListItem myItem = myList.GetItemById(__ListItem);
ProcessActivity(service, myList, myItem);
}
}
catch (Exception e)
{
service.LogToHistoryList(this.WorkflowInstanceId, SPWorkflowHistoryEventType.WorkflowError, 0, TimeSpan.Zero, "Error in Activity : VacationDurationCalculation", e.Message, string.Empty);
return ActivityExecutionStatus.Faulting;
}
return ActivityExecutionStatus.Closed;
}
|
| 5d | Add your business logic in a separate function
private void ProcessActivity(ISharePointService service, SPList _list, SPListItem _item)
{
//Code logic here
}
|
| Next | Create a Custom Worflow Activity with Visual Studio Part 2 |
To optimize sharepoint workflow authoring, there is HarePoint Workflow Extensions software ( http://www.harepoint.com/Products/HarePointWorkflowExtensions/ ) - about 200 new workflow activities, including free ones.
ReplyDelete