Saturday 8 February 2014

Replicator

This is a continuation of my previous post Multiple Tasks. The video by Scot Hillier is probably more then enough to complete this part, so I'm just going to skim over it.

Open up the workflow you created in the previous post, add a replicator and inside of it add your taskGenerator, if you do not see your task generator in your tool box, make sure to deploy your project.

now that you've added your task generator your workflow should look like


right click on your replicator and select generate handlers. this is what you should see

using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Collections;
using System.Drawing;
using System.Linq;
using System.Workflow.ComponentModel.Compiler;
using System.Workflow.ComponentModel.Serialization;
using System.Workflow.ComponentModel;
using System.Workflow.ComponentModel.Design;
using System.Workflow.Runtime;
using System.Workflow.Activities;
using System.Workflow.Activities.Rules;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Workflow;
using Microsoft.SharePoint.WorkflowActions;

namespace SPM_WorkFlow.Workflow
{
    public sealed partial class Workflow : SequentialWorkflowActivity
    {
        public Workflow()
        {
            InitializeComponent();
        }

        public Guid workflowId = default(System.Guid);
        public SPWorkflowActivationProperties workflowProperties = new SPWorkflowActivationProperties();

        private void replicatorActivity1_ChildInitialized(object sender, ReplicatorChildEventArgs e)
        {

        }

        private void replicatorActivity1_ChildCompleted(object sender, ReplicatorChildEventArgs e)
        {

        }

        private void replicatorActivity1_Completed(object sender, EventArgs e)
        {

        }

        private void replicatorActivity1_Initialized(object sender, EventArgs e)
        {

        }
    }
}


we are really only going to focus on two of the functions:

  • replicatorActivity1_Initialized
  • replicatorActivity1_ChildInitialized

so you can delete the other two if you'd like, one other thing that you're going to have to do is create an iList, this what you should end up with
using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Collections;
using System.Drawing;
using System.Linq;
using System.Workflow.ComponentModel.Compiler;
using System.Workflow.ComponentModel.Serialization;
using System.Workflow.ComponentModel;
using System.Workflow.ComponentModel.Design;
using System.Workflow.Runtime;
using System.Workflow.Activities;
using System.Workflow.Activities.Rules;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Workflow;
using Microsoft.SharePoint.WorkflowActions;

namespace SPM_WorkFlow.Workflow
{
    public sealed partial class Workflow : SequentialWorkflowActivity
    {
        public Workflow()
        {
            InitializeComponent();
        }

        public IList TaskList = default(System.Collections.IList);
        public Guid workflowId = default(System.Guid);
        public SPWorkflowActivationProperties workflowProperties = new SPWorkflowActivationProperties();

        private void replicatorActivity1_ChildInitialized(object sender, ReplicatorChildEventArgs e)
        {

        }

        private void replicatorActivity1_Initialized(object sender, EventArgs e)
        {

        }
    }
}


Notice the highlighted IList, lets go back to our replicatorActivity properties

  • bind the InitialChildData to our IList TaskList. 
  • set the execution type to Parallel

also if you removed the two completed fucntions your'e going to have to remove them from the properties to end up with.


Now go back into the code and lets focus on the replicatorActivity1_Initialized function.

private void replicatorActivity1_Initialized(object sender, EventArgs e)
{
    //set up the array list, which is going to be distributed amoungst each instance
    //the task gernator activity
    TaskList = new ArrayList();
    TaskList.Add(@"Domain\User1");
    TaskList.Add(@"Domain\User2");
    TaskList.Add(@"Domain\User3");
    TaskList.Add(@"Domain\User4");

    //initiate the first task
    taskGenerator1.Task_Title = "My Task";
    taskGenerator1.Task_Description = "<ul><li>One</li><li>Two</li></ul>";
    taskGenerator1.Task_AssignedTo = TaskList[0].ToString();

    taskGenerator1.Site_GUID = workflowProperties.SiteId;

}

next lets look at the replicatorActivity1_ChildInitialized function

private void replicatorActivity1_ChildInitialized(object sender, ReplicatorChildEventArgs e)
{
    taskGenerator1.Task_Title = "My Task";
    taskGenerator1.Task_Description = "<ul><li>One</li><li>Two</li></ul>";
    taskGenerator1.Task_AssignedTo = e.InstanceData.ToString();

    taskGenerator1.Site_GUID = workflowProperties.SiteId;

}

now deploy your project and test out your workflow