Friday, 20 June 2014

Creating a SharePoint Sequential Visual Studio Workflow (1 Level)

In the below steps with screenshots, you will create sequential visual studio workflow with one level of approval.
Click here to read my post on Creating a SharePoint Sequential Visual Studio Workflow (2 or n Level) 
Whenever a document is uploaded to any list/library, a workflow has to be triggered for approval.

1.Open Visual studio (Run as administrator).
 Select sequential workflow under SharePoint 2010 template. Enter the project name

2. Enter your site address and select farm solution and click next

3. Select List workflow and click next

4. Select the list , in our case Shared Documents.

5. A workflow can be started (triggered) in 3 ways:
    a) Manually
    b) When item is created
    c) When item is updated
Select 'when an item created' and click finish

6. Now you can see the screen as below with your workflow design and toolbox at left side.

7. Use the toolbox and drag and drop Create Task, While Activity and On TaskChanged tools to your workflow design, so your final design will look as below

8. Select CrateTask1 and hit F4, it would open properties page as below.
Type the Correlation token as "TaskToken" and owner activity would be name of your workflow (Workflow1).
  

9. Click taskId, click on the small bubble that appears an t the end or double click on small yellow box. Below screen would appear and select "Bind to a new member" and hit "Create Field". click OK


10. Repeat the above step for "Task Properties" in the properties panel of "CreateTask1".

11. Select OnTaskChanged1 and press F4, Properties panel will open as below


12. Select AfterProperties and click on the bubble appearing at the end.
Select "Bind to a new member" and hit "Create Field". click OK

13. Repeat above step for "BeforeProperties"  property of the "OnTaskChanged1".

14. Select "TaskId" for the "OnTaskChanged1" by clicking the bubble,
Select "Bind to an existing member" select the createTask1_TaskId1".

15. Select "Correlation Token" and select the previously entered tasktoken from the dropdown.

  Now we are done with the workflow design part.

16. Now double click on "CreateTask1"
Add the below code in generated method.
 public bool ApproveComplete = false;
        private void createTask1_MethodInvoking(object sender, EventArgs e)
        {
            createTask1_TaskId1 = Guid.NewGuid();
            createTask1_TaskProperties1.AssignedTo = @"domian\username";
            createTask1_TaskProperties1.Title = "Please approve this task";
            createTask1_TaskProperties1.DueDate = DateTime.Now.AddDays(10.0); //Due date: 10 days from today
            createTask1_TaskProperties1.Description = "Test description";
        }
  Add the public bool ApproveComplete = false; line just above the method as shown in above code.

17. Go back to your design page and double click on "OnTaskChanged1", add the below code in the generated method
     private void onTaskChanged1_Invoked(object sender, ExternalDataEventArgs e)
        {
            if (onTaskChanged1.AfterProperties.PercentComplete == 1.0)
            {
                ApproveComplete = true;
            }
        }

18. Go to the code behind and add the below code
       private void isWhile(object sender, ConditionalEventArgs e)
        {
            e.Result = !ApproveComplete;
        }
19. Now go back to design page and select "WhileActivity1" , hit F4, Select Code Condition for "Condition", expand it and Select isWhile




20. Now your workflow is ready.! Build, Deploy and open you site .

21. Go to Shared Documents and upload some document

22. Now you can see your workflow showing in a new column with status as InProgress Since we have selected workflow to begin "when an item created"

23. Click on InProgress and it will open the WorkFlow properties. Edit the created task as shown.

24. Enter 100 for %Complete field and Save

25. Now go back to Shared Documents  and you can see the workflow is complete!

The compete code would look like below:
 public bool ApproveComplete = false;
        private void createTask1_MethodInvoking(object sender, EventArgs e)
        {
            createTask1_TaskId1 = Guid.NewGuid();
            createTask1_TaskProperties1.AssignedTo = @"domian\username";
            createTask1_TaskProperties1.Title = "Please approve this task";
            createTask1_TaskProperties1.DueDate = DateTime.Now.AddDays(10.0);
            createTask1_TaskProperties1.Description = "Test description";
        }
        private void isWhile(object sender, ConditionalEventArgs e)
        {
            e.Result = !ApproveComplete;
        }
        private void onTaskChanged1_Invoked(object sender, ExternalDataEventArgs e)
        {
            if (onTaskChanged1.AfterProperties.PercentComplete == 1.0)
            {
                ApproveComplete = true;
            }
        }

Happy Coding