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

Workflow activity snippet for Visual Studio


Topic Add a snippet for workflow activity in Visual Studio
Output A workflow activity snippet

Step Description
1Create an xml file and name it spwfactivity.snippet.
2Insert this code :
<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets  xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
    <CodeSnippet Format="1.0.0">
        <Header>
            <Title>spwfactivity</Title>
            <Shortcut>spwfactivity</Shortcut>
            <Description>Sharepoint Workflow Activity Snippet</Description>
            <Author>Tzarad</Author>
            <SnippetTypes>
                <SnippetType>Expansion</SnippetType>
            </SnippetTypes>
        </Header>
        <Snippet>
            <Declarations>
                <Literal>
                    <ID>classname</ID>
                    <ToolTip>Class Name</ToolTip>
                    <Default>Activity1</Default>
                </Literal>
                <Literal>
                    <ID>property</ID>
                    <ToolTip>Property name</ToolTip>
                    <Default>MyProperty</Default>
                </Literal>
            </Declarations>
            <Code Language="csharp"><![CDATA[public partial class $classname$: Activity
    {

        #region Fields
        public static DependencyProperty __ContextProperty = DependencyProperty.Register("__Context", typeof(WorkflowContext), typeof($classname$));
        public static DependencyProperty __ListItemProperty = DependencyProperty.Register("__ListItem", typeof(Int32), typeof($classname$));
        public static DependencyProperty __ListIdProperty = DependencyProperty.Register("__ListId", typeof(string), typeof($classname$));
        #endregion

        #region Properties


        [ValidationOption(ValidationOption.Required)]
        public WorkflowContext __Context
        {
            get { return ((WorkflowContext)(base.GetValue($classname$.__ContextProperty))); }
            set { base.SetValue($classname$.__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

        public $classname$()
        {
            InitializeComponent();
        }

        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;
        }

    private void ProcessActivity(ISharePointService service, SPList _list, SPListItem _item){
    //Code logic here

    }
    }]]>
            </Code>
        </Snippet>
    </CodeSnippet>
</CodeSnippets>
3Save the file.
4In Visual Studio 2010, select Tools -> Code Snippets Manager -> Add the file

No comments:

Post a Comment

by Category