Creating a Custom Site Workflow Activity Using C# in SharePoint

Zeus-Custom-site-workflow-blogpost
 
In this tutorial, I will show you how to create a custom site workflow activity using C#.
Before anything else, let’s gather what you need:

  • Supported editions of Microsoft Windows and SharePoint.
  • Visual Studio

First, create a project to hold and test the custom workflow activity.
To create a site workflow custom activity project:

    1. Look for the New Project dialog box by clicking New on the File menu, and then click New Project.
    2. Expand the SharePoint node under either Visual C# or Visual Basic, and then click the 2010 node.
    3. In the Templates pane, select Sequential Workflow.
    4. In the Name box, type AnnouncementBackup and then click OK. The SharePoint Customization Wizard appears.
    5. In the “What is the local site you want to use for debugging?” page, click Next to accept the default site. (This step also sets the trust level for the solution as farm solution, the only available option for workflow projects.)
    6. In the Specify the workflow name for debugging page, accept the default name (AnnouncementBackup – Workflow1). Change the workflow template type to Site Workflow and then click Next.
    7. Click Finish to accept the remaining default settings. Adding a Custom Workflow Activity Class

 


Next, add a class to the project to contain the code for the custom workflow activity.
To add a custom workflow activity class:

      1. Click Add New Item on the Project menu to display the Add New Item dialog box.
      2. In the Installed Templates tree view, click the Code node and then click Class in the list of project item templates. Use the default name Class1.
      3. Replace all of the code in Class1 with the following:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
namespace AnnouncementBackup
{
    // This custom activity will back up all of the announcements in
    // the Announcements list on the SharePoint site.
    public class Class1 : System.Workflow.ComponentModel.Activity
        {
        public Class1()
        { }
        // Triggers when the activity is executed.
        protected override System.Workflow.ComponentModel.ActivityExecutionStatus Execute(System.Workflow.ComponentModel.ActivityExecutionContext executionContext)
        {
            try
            {
                // Get a reference to the SharePoint site.
                SPSite site = new SPSite("http://" + System.Environment.MachineName);
                SPWeb web = site.OpenWeb("/");
                // Reference the original Announcements list.
                SPList aList = web.GetList("/Lists/Announcements");
                // If the Announcements Backup list already exists, delete it.
                try
                {
                    SPList bList = web.GetList("/Lists/Announcements Backup");
                    bList.Delete();
                }
                catch
                { }
                // Create a new backup Announcements list and reference it.
                Guid newAnnID = web.Lists.Add("Announcements Backup", "A backup Announcements list.", SPListTemplateType.Announcements);
                SPList bakList = web.Lists[newAnnID];
                // Copy announcements from original to backup Announcements list.
                foreach (SPListItem item in aList.Items)
                {
                    SPListItem newAnnItem = bakList.Items.Add();
                    foreach (SPField field in aList.Fields)
                    {
                        if (!field.ReadOnlyField)
                            newAnnItem[field.Id] = item[field.Id];
                    }
                    newAnnItem.Update();
                }
                // Put the Backup Announcements list on the QuickLaunch bar.
                bakList.OnQuickLaunch = true;
                bakList.Update();
            }
            catch (Exception errx)
            {
                System.Diagnostics.Debug.WriteLine("Error: " + errx.ToString());
            }
            return base.Execute(executionContext);
        }
	}
}

      1. Save the project and then click Build Solution on the Build menu.
      2. Class1 appears as a custom action in the Toolbox under the SharePoint Workflow tab in the Toolbox. Adding the Custom Activity to the Site Workflow

 


Next, it’s time to add an activity to the Workflow to contain the custom code.
To add a custom activity to the site Workflow:

      1. Open Workflow1 in the workflow designer in design view.
      2. Click and drag Class1 from the Toolbox and drop it under the on WorkflowActivated1 activity.
      3. Save the project. Testing the Site Workflow Custom Activity

Now you have created a custom site workflow activity using C#. Congratulations!

Social Media

Get The Latest Updates

Subscribe To Our Weekly Newsletter

No spam, Just News and Updates.

Visit Other Pages

On Key

Related Posts

Sales Executive

We celebrate the diversity of our teams and Tech One Global is a place where everyone can be themselves and are empowered to do their

Accounts Executive

Join Sri Lanka’s Great Place to Work Awarded organization, Tech One Lanka! We are an organization that celebrates the diversity of our teams, where everyone

Business Analyst

Join Sri Lanka’s Great Place to Work Awarded organization, Tech One Lanka! We are an organization that celebrates the diversity of our teams, where everyone

Presales Consultant – Data and AI

Join Sri Lanka’s Great Place to Work Awarded organization, Tech One Lanka! We are an organization that celebrates the diversity of our teams, where everyone

Twitter
Facebook
LinkedIn