Friday, 13 June 2014

Creating a custom sequential workflow in SharePoint 2010

Nice Article by Kalyan

http://www.techbubbles.com/workflowfoundation/creating-a-custom-sequential-workflow-in-sharepoint-2010/

SharePoint supports state machine and sequential workflows. In Sequential Workflow the activities are placed in logical order with a start and an end. Sequential workflows can be used in automated processes where a manual interaction like moving documents to library. In SharePoint, Workflows can be associated with libraries, lists and sites. Site workflows are introduced in SharePoint 2010 and they exist at the site level and can act on all lists and libraries associated in the site. This post outlines the steps to create a sequential workflow in SharePoint.
Create a list and instantiate the workflow automatically when an item is inserted to a list and flow through the process.
1. Create a list in SharePoint called Credit Approval which contains the below three fields
Field Name Data Type Required
Credit Requested Amount Currency Yes
Employment History Choice (Good, Bad and none exist) Yes
Credit History Choice (Good, Bad and none exist) Yes
2. Launch the Visual Studio  2010 and create a new Sequential Workflow as shown below
image
3. Name the workflow project name and say ok. Visual Studio selects the available SharePoint site on the machine and select deploy as Farm Solution in next step
4. Provide a name to workflow and select List Workflow for the workflow template
image
5. Click next to select the list association and task list and workflow history list
image
6. Check how do you want the workflow to start?
image
7. Click Finish button and Visual Studio generates the necessary files. Add LogToHistoryListActivity activity below onWorkflowActivate activity and set the properties as shown below
image
8. Drag an If-else activity to the designer below the logWorkflowStarted activity. Add other activities as shown below
image
9. Select the code condition in properties window for CheckEmploymentHistoryActivity and add the below code
image
10. Now Deploy the project and browse the list in SharePoint and save an item to the list. As there is no user interaction workflow completes and shows as below
image
11. Click on the Completed status of the workflow to see the workflow history.
image
12. After Deploying the workflow to SharePoint then you can then associate it to list , content or site in two ways
  • Manually by using SharePoint interface
  • During the feature activation via feature receiver
13. Navigate to the list settings –> Workflow settings of the list then you need to associate a workflow as shown below
image
click on the Add a workflow link to add a new workflow as shown below
image

Custom Approval Workflow in SharePoint

Good Article by Muruges

http://intelliview.blogspot.in/2012/08/custom-approval-workflow-in-sharepoint.html

Download code from here
This workflow was created for beginner those wanted to learn the custom workflow in SharePoint from very scratch level.
Even I have created many complex workflows for my work in the past,I feel its time to introspect my knowledge on this simple workflow.
As I follow the many instruction those available in forums and blogs,I end up with confusion and if I wanted to apply my own business requirement out of this instruction,it was very tedious.
So here is the simplified version of my "Custom Approval Workflow in SharePoint using Visual Studio 2010".
Debugging is always enlighten you to apply and learn the secret of the workflow engine. Create a blank SharePoint project and add the new item from project template "Sequential Workflow".
On this design surface add the activities just like you seen here.
The workflow engine manage the following activities such as "Creating task and assigning to someone,after the task has been assigned to someone monitoring the status of Tasks(approved or rejected) and completing the task. Many times I was confused and identified the reason for it,assigning the correlation and Task Properties and Task id via "Property Window". Of course this will be useful for advanced developer[I personally feel those knows all properties of the task and workflow and how to apply these to their business requirement]. For simplicity I am going to create all properties behind the code and assigning to design surface. Required properties here I have created so far globally.
        public Guid TaskID = Guid.NewGuid();
        public SPWorkflowTaskProperties taskProp = new SPWorkflowTaskProperties();
        public SPWorkflowTaskProperties afterProp = new SPWorkflowTaskProperties();
        public SPWorkflowTaskProperties beforeProp = new SPWorkflowTaskProperties();
        public bool isComplete = false;
// This is nothing but "Task ContentType's" field "Status" GUID.
//You can find it by debugging the code line,You can find all field's guid and its value.
//HashTable tbl = afterProp.ExtendedProperites;
        public Guid idStatus = new Guid("c15b34c3-ce7d-490a-b133-3f4de8801b76");

Now we need to assign these properties to design workface.Once you created all these properties on code behind,you can see like this
Important tips to remember: Correlation token for task activities must be same throughout the workflow. So I am renaming the correlation token for my "CreateTask1" as taskToken. Assigning the Owner Activity to "Workflow1" just like below screenshot.
follow the same to all activities "OnTaskChanged1" and "CompleteTask". Secondly,We need to assign the unique GUID for task this one also common for all activities. Right click on CreateTask1 activities -> Properties and choose the "Task Id" click on the button and choose our own created "TaskID" for the task id. like shown here. And do the same for "Task Properties" too.


Here I am creating a method called "createTaskAndAssign".
private void createTaskAndAssign(object sender, EventArgs e)
        {

            taskProp.Title = "Type the title";
//Later you can try the dynamic title text may be workflow item's title using workflowproperties.
            taskProp.AssignedTo = "local\\administrator";
            taskProp.Description = "Please review";
            taskProp.DueDate = DateTime.Now.AddDays(2);

        }
Assign the above method to "MethodInvoking" properties of the "Create Task1" activity. For whileActivity you can manage your condition using "Code Declaration" or "Declarative Rule Condition. I have used "Code condition" you just need to assign the method name[CheckTaskStatus]to "Code Condition properties of "While Activity".
private void CheckTaskStatus(object sender, ConditionalEventArgs e)
        {
            e.Result = !isComplete; //When true - workflow ends up.
        }

Created a method for "OnTaskChange1" activity and assigned the same as did for while activity. This method will check the task content type's task status field value by using the task class's "ExtendedProperties".
 private void onTaskChangedInvoked(object sender, ExternalDataEventArgs e)
        {
            string tbl = afterProp.ExtendedProperties[idStatus].ToString();
            if (tbl.Equals("Completed"))
            {
                isComplete = true;
            }
            else
            {
                isComplete = false;
            }

        }
As soon as the boolean value set to true,the workflow calls the "Complete Task" activity and ends up its cycle. To test this demonstration on your development server,create two user for your sharepoint site and assign the task for them. And log in as different user by using new user check the "Task" list on their log do approve or reject and see the workflow status.

Reference:

http://intelliview.blogspot.in/2012/08/custom-approval-workflow-in-sharepoint.html

SharePoint Foundation 2010 Visual Studio Approval Workflow

Good Article explains step by step by Adil

http://dotnetadil.wordpress.com/2012/05/29/sharepoint-foundation-2010-visual-studio-approval-workflow/

  1. The scenario is that a document library has a column of type option named WorkFlowLevel. Level -0 means that the document is approved automatically by the uploader and Level -1 means the document can only be approved/reject by a person who is in the Approver groups.

    Column

    Approver groups
  2. The final workflow is as below:-
  3. To create such solutions fire up the Visual Studio and select a Sequential Workflow template, and set the solution settings as below




  4. First code is to define the OnWorkflowActivated event .
  5. Then connect this method to the activity on the designer
  6. Next go to the Toolbox, under the Window Workflow v3.0 tab drag and drop the ifElse activity just below the previous activity and then define the code to execute.
  7. Associate the above code to the left if branch (if the condition return true then this side will fire, if it false the right side will automatically fire)
  8. If true, the left side will execute a code (see below) that will email a document approval request to all the member of the Approver groups
  9. Drop the send email activity and associate the above code.
  10. Now we are going to use the while loop that will loop until the Approve Status is equal to Approved or Reject.
  11. Associate the while loop with the code below
  12. Inside the while loop drop the OnWorkflowItemChanged activity and then define the code before associating the two.

  13. When the document have been approved or rejected, the loop will break where based on the status we will send the email to the document creator. Drop the ifElse activity just below the while loop.
  14. Below is the email code for sendApprovedEmail
  15. One last part of the workflow is the right hand side, where the document does not require any approval. The approval status is automatically set to Approved.
  16. For that, just drag and drop the Code activity from the Toolbox inside the else activity and associate the ExecuteCode.

  17. To approve a document, the user needs to have at least Design permission level. Even though the workflow can be triggered by user that has permission other than Design which by right the code should be elevated. Here we are not elevating anything since unlike SharePoint Designer workflow which runs under the user
    who triggers it, a Visual Studio workflow will always run using the System Account (that means Full Control).
  18. Oh, by the way the link in the email will open the below page (Item Display Form).


    Reference

    http://dotnetadil.wordpress.com/2012/05/29/sharepoint-foundation-2010-visual-studio-approval-workflow/