.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> SharePoint >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Site Workflow with While, IfElse, Code Activities in SharePoint

Posted By:Jean Paul       Posted Date: July 24, 2013    Points: 200    Category: SharePoint    URL: http://www.dotnetspark.com  

In this article we can explore using multiple activities in a Site Workflow created using Visual Studio 2010. The core activities we use here are
 

In this article we can explore using multiple activities in a Site Workflow created using Visual Studio 2010.  The core activities we use here are:
·  Code Activity to execute custom code
·  While Activity to execute inner activities upon condition is true
·  IfElse Activity to execute branching activities based on condition
·  LogToHistoryList Activity to log information to Workflow internal history list


Scenario


A typical approval scenario is being addressed here.  An Expenses list is given for approval.  The entries having Amount lesser than 100 are Approved. 


Pre-Requisites


Create a custom list named Expenses for using this example.  Add some items into the new list as shown below:



Create Workflow


Open Visual Studio 2010 and create a new Sequential Workflow Project as shown below:



In the next page choose your site collection and click the Next button to continue. 
In the appearing page choose Site Workflow as shown below:



In the last page choose the option to start workflow manually.



Now you will be getting the Workflow designer with default activity inside it.



Add Activities


Open Visual Studio Now you can drag and drop the following activities from the Toolbox:
·  1 Code Activity
·  1 While Activity & IfBranch Activity
·  2 Code Activity inside If, Else branches
·  1 LogToHistory Activity
As shown below:

 


Our idea is following:


·  Create property named CurrentItem of type object, Index of type integer
·  In the first Workflow Activated, fetch all the Expenses items
·  In the Code Activity initialize the Index field
·  Set While Activity condition until CurrentItem is not null
·  Set the If branch condition as Amount less than 100
·  Set the Else branch condition as Amount greater than or equal to 100
·  Set the Code Activity inside If branch, to update status as Approved
·  Set the Code Activity inside Else branch, to update status as Rejected
·  Use the LogToHistory activity for logging the workflow completion information


Create Entity, Properties & Fields


Please create the following class to hold the Expense Item, in order to give an Object Oriented Look and Feel.

namespace ExpenseApprovalWorkflow
{
    public class ExpenseItem
    {
        public int ID;
        public double Amount;
    }
}


Inside the Workflow class, add the following properties and fields.

namespace ExpenseApprovalWorkflow.Workflow1
{
    public sealed partial class Workflow1 : SequentialWorkflowActivity
    {
        List<ExpenseItem> Expenses = new List<ExpenseItem>();
        int Index;

        ExpenseItem CurrentItem
        {
            get
            {
                if (Index < Expenses.Count)
                    return Expenses[Index];

                return null;
            }
        }
    }
}



The Expenses list holds the ExpenseItem type items.  The Index property represents the current index of the item being processed.

Initiation Code


Back to the workflow designer, double click on the onWorkflowActivated step and add the following code.

private void onWorkflowActivated1_Invoked(object sender, ExternalDataEventArgs e)
{
    using (SPSite site = SPContext.Current.Site)
    {
        using (SPWeb web = site.OpenWeb())
        {
            var items = web.Lists["Expenses"].GetItems("ID", "Amount");

            foreach (SPListItem item in items)
            {
                ExpenseItem expenseItem = new ExpenseItem();
                expenseItem.ID = (int)item["ID"];
                expenseItem.Amount = (double)item["Amount"];

                Expenses.Add(expenseItem);
            }
        }
    }
}


The code fetches all Expenses list items from SharePoint and adds them to the Expenses field of our workflow class.


Code Activity


Double click on the Code Activity item from workflow design view (second activity).  In the appearing code window enter the following code.

private void codeActivity1_ExecuteCode(object sender, EventArgs e)
{
  Index = -1;
}


The code initializes the Index field to -1.  This field will be incremented in the while loop later.


While Activity


Now select the While activity from the workflow design view and choose Properties window.  Choose the Condition property as Code Condition and set the Inner Condition property as WhileCodeCondition.



The WhileCodeCondition represents the name of the method which evaluates the While condition and returns true / false to continue / terminate the workflow.
Paste the following code in the Workflow class view.

private void WhileCodeCondition(object sender, ConditionalEventArgs e)
{
  Index++;

  e.Result = (CurrentItem != null);
}



You can see the method name matches the property we have specified.  The method intakes the ConditionalEventArgs for specifying the condition of while activity.

IfElse Activity


You can see that there are 2 branch activities for the IfElse activity.



Select the first branch item and open the Properties window.  Here we can specify the following properties:

1.  Condition as Declarative Rule Condition
2.  ConditionName as IfCondition
3.  Expression as this.CurrentItem.Amount < 100

You can see them in the Properties window as shown below:



The ellipsis button as highlighted in red color can be used to create the Expression.  There is auto-completion enabled in the appearing editor dialog.



Now select the second branch item and open the Properties window.  Here we can specify the following properties:

1.  Condition as Declarative Rule Condition
2.  ConditionName as ElseCondition
3.  Expression as this.CurrentItem.Amount >= 100

Now our If and Else branches are ready to proceed with their Code activities.

Code Activity for If branch


Now double click on the Code Activity for the If branch.



In the appearing code view, enter the following code.

private void codeActivity2_ExecuteCode(object sender, EventArgs e)
{
    using (SPSite site = SPContext.Current.Site)
    {
        using (SPWeb web = site.OpenWeb())
        {
            var item = web.Lists["Expenses"].GetItemById(CurrentItem.ID);

            item["Status"] = "Approved";
            item.Update();
        }
    }
}


The code fetches the Expense Item by ID and updates the Status as Approved.

Code Activity for Else branch

Now double click on the Code Activity for the Else branch.


In the appearing code view, enter the following code.

private void codeActivity3_ExecuteCode(object sender, EventArgs e)
{
    using (SPSite site = SPContext.Current.Site)
    {
        using (SPWeb web = site.OpenWeb())
        {
            var item = web.Lists["Expenses"].GetItemById(CurrentItem.ID);

            item["Status"] = "Rejected";
            item.Update();
        }
    }
}


The code fetches the Expense Item by ID and updates the Status as Rejected.

LogToHistoryList Activity

This activity logs information into the History List of the site.  Select the activity and set the following property:



This list is a hidden list and is not shown in the Quick Launch or All Site Content.

To view the list you need to type the List Name in address bar.  For example:

  http://SERVER/Lists/workflow%20history/AllItems.aspx

You can see various entries in the History List as shown below:


Using the History List we can verify the workflow instances being executed and their log information for troubleshooting.

Deploy the Workflow

Now we are ready to execute our workflow.  Build the project, Right click on the solution and Deploy it.


Inside SharePoint

Back in SharePoint choose the Site Workflows and start our workflow.



Wait for a while for the execution to be completed.



Back to the Expenses list you can see the Status column being updated for the items.



You can open the Workflow History list to view the log entry as shown below:



This concludes our playing with Workflow activities.

References

http://msdn.microsoft.com/en-us/library/ms446847.aspx

Summary

In this article we have explored creating a Site Workflow using multiple activities.  To summarize with following are the activities we have explored.

·  IfElse activity
·  While activity
·  Code activity
·  LogToHistoryList activity

I hope this example should provide knowledge to address complex workflow scenarios in your programming life.  The source code contains the example we have discussed.


 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend