Using a People Picker in SharePoint Workflow Initiation

Creating a workflow from SharePoint Designer is limiting in many ways, but it’s also very quick and easy, and it covers a large majority of every day scenarios. One of the major flaws for me has always been the inability to select ‘Person’ as a data type for an initiation variable (assuming that this would render a nice people picker control in the initiation form and maybe pass back the User ID to the workflow).

I have been determined to resolve this and have come up with a quick and easy fix using some bespoke client side code within the initiation form.

NOTE: In my workflow I actually want to send an email so I am doing a little more in the JavaScript. This only works because on this project UserID@domain.com is a valid email alias. However the techniques outlined here should assist anyone who needs people data into a workflow.

First of all create a new SPD workflow. Give it a name and select your list. Ensure that ‘Allow this workflow to be started manually’ is checked (so we get an initiation form generated for us).

  1. Select Initiation. and Add.
  2. In the Add Field dialog enter a Field name, in my case ’email’ and select ‘Single line of text’.
  3. Leave Default value blank and click on Finish. Then OK.
  4. Back in your Workflow Designer click Next.
  5. In this simple example I am just going to send an email, so click Actions and select Send an email. Click on ‘this message’ to define your email.
  6. Click the address book on the To: line and in the Select Users dialog select Workflow Lookup. and click Add >>.
  7. Define your workflow lookup as Source: Workflow Data and Field: Initiation: email (or whatever you called your initiation variable)
  8. Click OK. Then OK again.
  9. Enter a value in Subject: And also for the body text. Click OK.
  10. Now click Finish as we are done creating this simple workflow.
  11. If you open up the folder structure which SPD has created under Workflows you will see that there should be three files created:
    1. the xoml file
    2. an xml config file
    3. an aspx page
  12. The final one is the one we are interested in as this contains the initiation form for our workflow.
  13. Double click on this so we can take a look.
  14. You will note that essentially this is a standard SharePoint page with a DataFormWebPart which is set up to pass the initiation data to our workflow. In this case we should have a single text box for our initiation variable.
  15. If you look at the code view you will see that various assemblies and namespaces have been included within this page already. Including the Microsoft.SharePoint.WebControls namespace which contains the PeopleEditor control. This is the key to getting people data from the users.
  16. In code view if you put some spaces above the DataFormWebPart definition then we can include this control on the page. Just copy the code below:

    <sharepoint:peopleeditor
        id=”pplPicker””
        runat=”server”
        allowempty=”False”
        validatorenabled=”True”
        onvaluechangedclientscript=”SetPeopleValues()”
        allowtypein=”False”>
    </sharepoint:peopleeditor>

  17. Essentially this is adding the control to the page and setting some properties. The really important properties here are:

    OnValueChangedClientScript
    This property allows us to specify some client side JavaScript which we wish to run every time the value in the people picker is changed.

    AllowTypeIn
    This property restricts the user from manually typing into the people picker and forces them to use the Browse dialog to select users. See final note.

  18. Next we need to add the JavaScript to our page.
  19. At the very top of your page after all the Page and Register tags you will notice the opening <asp:content> tag for PlaceHolderMain. We need to place the following code above this control on the page:

    <asp:Content
        id=”Content1″”
        runat=”server”
        contentplaceholderid=”PlaceHolderAdditionalPageHead”> 
        <script type=”text/javascript”>

            function SetPeopleValues()   
            {         
                //Retrieve the semi-colon separated
                //list of user id’s     
                var people = document.getElementById(‘ctl00_PlaceHolderMain_pplPicker_downlevelTextBox’).value;     
                document.getElementById(‘ctl00_PlaceHolderMain_InitiationForm_ff1_1’).value = people;     
                if (people==”)     
                {       
                    alert(‘No people selected’);
                } else {       
                    //Split the list by ; to get an
                    //array of domain user names       
                    var arrPeople = new Array();       
                    arrPeople = people.split(‘;’);

                    //Create an empty string to hold the
                    //email addresses       
                    var strEmails = ”;

                    //Loop through the array of usernames       
                    var arrEndIndex = arrPeople.length – 1;       
                    var i=0;       
                    while (i <= arrEndIndex)       
                    {         
                        //Strip the domain name off and append
                        //the email address elements         
                        var person = String(arrPeople[i]);         
                        person = person.toLowerCase().replace(‘DOMAIN\’,”);         
                        var email = person + ‘@domain.com’;
                        //Add this email address to our string         
                        strEmails = strEmails + email;

                        //If this is the final email then add this
                        //value to the text box in our initiation
                        //form control, otherwise add the
                        //semi-colon         
                        if (i == arrEndIndex)         
                        {           
                            document.getElementById(‘ctl00_PlaceHolderMain_InitiationForm_ff1_1’).value = strEmails;         
                        } else {           
                            strEmails = strEmails + ‘; ‘;        
                        }
                        i=i+1;
                    }     
                }   
            }

      </script>
    </asp:Content>

  20. This uses the PlaceHolderAdditionalPageHead place holder to ‘inject’ some JavaScript into the head of the html page.

    Essentially this code is retrieving the value from our people picker and inserting it into the text box held on the DataFormWebPart. In my case I am doing some additional processing (to generate a semi-colon separated list of email addresses).

    NOTE: By default the PeoplePicker control (that is the TextArea html control that is rendered by the PeopleEditor SharePoint control) contains a semi-colon separated list of user names, of the form DOMAINUserName, so if you wanted to ignore the string parsing and processing elements of the javascript you could just set this value to the textbox straight away and then do any text processing on these values within your workflow (see below).

    function SetPeopleValues()
    {             
        //Retrieve the semi-colon separated list of user id’s   
        var people = document.getElementById(‘ctl00_PlaceHolderMain_pplPicker_downlevelTextBox’).value;   
        document.getElementById(‘ctl00_PlaceHolderMain_InitiationForm_ff1_1’).value = people;
    }

  21. To tidy up the look and feel of your form select the <tr>
    tag that contains the textbox control from within your DataFormWebPart and set the style tag to:

    style=”visibility: hidden”

    This will mean that the end user does not see the data being passed to this textbox they just select their users and click Start.

    If you save all the changes you have made to this form then try and initiate your workflow you will see that you should be able to select your users using the Browse button and then click start, an email containing the details set out in your workflow should be emailed to the users selected in your People Picker.

    And that’s it!

NOTE: If you don’t declare the PeopleEditor control with AllowTypeIn set to false then your end users could manually type in any name regardless of whether or not it exists or can be resolved. In this scenario clicking start will not cause the people picker to resolve the names first and the value of your textbox will be empty as our JavaScript will not run. If anyone has any ideas on this could they please post comments as I would like to get this working.