Introduction

This blog is mainly focused on SharePoint
I will use this to store and share all the tips and tricks about SharePoint and the related products
List of all posts

SharePoint 2010: Create a Custom Worflow Activity with Visual Studio Part 1


Topic Create a SharePoint workflow activity with Visual Studio 2010
Output a Custom Activity

Step Description
1In visual studio 2010, create a new Project, based on Workflow Activity Library
2This will create a skeleton project
3First we need to add the SharePoint references
3aRight click reference -> Add reference
3bBrowse to :
c:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\Isapi

3cAdd
Microsoft.SharePoint.dll microsoft.sharepoint.WorkflowActions.dll
4Rename Activity1 in something more significant (tzRegEx in this example)
5Right click the activity and select View Code
5aAdd 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
5cOverride 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;

            }
5dAdd your business logic in a separate function
private void ProcessActivity(ISharePointService service, SPList _list, SPListItem _item)
            {
                //Code logic here

            }
NextCreate a Custom Worflow Activity with Visual Studio Part 2

1 comment:

  1. 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

by Category