Search This Blog

Friday, June 17, 2016

Sequential Workflow

In this example we are trying to create a Workflow which on activation updates the null Address column of the Manager list. 

Step 1: Create Sequential Workflow Project
For the time being, we can start with a Sequential Workflow. Start Visual Studio and create a new project from the template SharePoint > Sequential Workflow.


In the next screen select the option Site Workflow as shown below:


In the next screen, leave the default option saying the user manually starts the Workflow. Click the Finish button to create the project.

Note: If user select a List Workflow rather than site workflow it active another option like automatically created a workflow when item created.

You will get the following screen once the project is created.

Step 2: Create Activity
We need to create an Activity to perform our job.

What is an Activity?

A Workflow consists of a series of Activities. We can add Activities using the Toolbox. There are different types of Activities like Code Activity, SendEmail, etc. For our example we are using the more functional Code Activity.

Drag and drop a Code Activity from the toolbox. You can locate this from the v3.0 group inside Toolbox.


Step 3: Add code for the Activity
Now we need to add code for this Activity. Double click on the codeActivity1 item shown above. Place the following code in the appearing code view.
private void codeActivity1_ExecuteCode(object sender, EventArgs e)
{
    using (SPWeb web = SPContext.Current.Web)
    {
        SPList list = web.Lists["Manager"];
        foreach (SPListItem item in list.Items)
        {
            if (item["Address"] == null)
            {
                item["Address"] = "PLEASE SET THE ADDRESS!";

                item.Update();
            }
        }
    }
}
OR 
private void codeActivity1_ExecuteCode(object sender, EventArgs e)
{
 try
            {
            SPSite site = new SPSite(@"http://sqlsharep:410/");
            SPWeb web = site.AllWebs["ep"];
            SPList list = web.Lists["Manager"];

                SPSite oSite = new SPSite("http://sqlsharep:410/sites/ep");
                SPWeb oWeb = oSite.OpenWeb();

                oWeb.AllowUnsafeUpdates = true;

                SPList list = oWeb.Lists["Manager"];
                foreach (SPListItem item in list.Items)
                {
                    if (item["Address"] == null)
                    {

                        item["Address"] = "Please set the Address!";
                        item.Update();
                    }
                }

            }
            catch (Exception ex) { }
        
   }
OR
private void codeActivity1_ExecuteCode(object sender, EventArgs e)
{
 try
            {
            SPSite site = new SPSite(@"http://sqlsharep:410/");
            SPWeb web = site.AllWebs["ep"];
            oWeb.AllowUnsafeUpdates = true;

                SPList list = oWeb.Lists["Manager"];
                foreach (SPListItem item in list.Items)
                {
                    if (item["Address"] == null)
                    {

                        item["Address"] = "Please set the Address!";
                        item.Update();
                    }
                }

            }
            catch (Exception ex) { }
        
   }

Step 4: Build and Deploy the Solution
Now we are ready to build and deploy the solution. Right click on the solution and use the Build and Deploy command.  

Step 5: Execute the Workflow inside SharePoint
Now we are ready to test the Workflow inside SharePoint. As the Workflow was created as a Site Workflow it will be accessible for all the Lists and Libraries. You can click the Lists link inside the site.


Now click on the Site Workflows link. You will get the following screen.


Before executing the Workflow, you need to create a Manager item with Address not assigned.
Click on the highlighted button and your workflow gets executed. Wait for a while and you can see the invalid manager record is updated with the message.


This concludes our article on Workflow using Visual Studio.

Note

For debugging the Workflow, you can set a breakpoint and use the Debug command of Visual Studio. When the Workflow is executed, the breakpoint will get hit.

No comments:

Post a Comment