Tags

, , ,

I was digging into SharePoint 2013 client people picker control and I have decided to write some findings about it. First of all, my approach is more classical. I was trying to do as much as I can declarative, instead of creating complicated JavaScript code for it. Here is how I have done.

First thing I did was to be sure I have all required JavaScript files loaded. So adding these lines to the header should be more than fine:

<SharePoint:ScriptLink name="clienttemplates.js" runat="server" LoadAfterUI="true" Localizable="false" OnDemand="False" />
<SharePoint:ScriptLink name="clientforms.js" runat="server" LoadAfterUI="true" Localizable="false"  OnDemand="False" />
<SharePoint:ScriptLink name="clientpeoplepicker.js" runat="server" LoadAfterUI="true" Localizable="false"  OnDemand="False" />
<SharePoint:ScriptLink name="autofill.js" runat="server" LoadAfterUI="true" Localizable="false"  OnDemand="False" />					

In the content area I have added client picker editor tag, defining also a handler for user resolved event.

<SharePoint:ClientPeoplePicker ErrorMessage="Not a valid user" ValidationEnabled="True" runat="server" AllowMultipleEntities="False" ClientIDMode="Static" AutoFillEnabled="True" Required="True" OnUserResolvedClientScript="userResolved"  ID="AssignedTo" VisibleSuggestions="10"/>


<script type="text/javascript">
var userResolved = function (elementId, users) {


}
 </script>

You can notice I have added the script inside page content. For some reasons, probably loading order, if I move the JavaScript function handler to an external file and load it through ScriptManager (set to load scripts after UI) it won’t work and will find the function as being undefined. For me it looks like a loading order and probably client picker control is processed before my file is loaded by the script manager. Not nice what I have done, I know, but is working. 🙂

Coming back to the handler, it should be a function called with two parameters. First one represents the id of the element. More exactly is a id of the top container which includes multiple DOM elements required by the people picker control. The second parameter is an array of objects. For each user an object is pushed to an array and passed to our function.

var userResolved = function (elementId, users) {
    ///<summary>Handler for user resolved event</summary>
    /// <param Type="String">Element id</param>
    /// <param Type="Array" ElementType="Object">Users</param>

    for (var i = 0; i < users.length; i++) {
        // Object containing properties for the users, like Key, Description, EntityType, IsResolved
        var user = users[i];
        alert(user['Key']);
    }
}

However, beside user resolved event, we can also have a handler for value changed or for picker validate. You can bind the event to the handler in the same way.

<SharePoint:ClientPeoplePicker ErrorMessage="Not a valid user" ValidationEnabled="True" runat="server" AllowMultipleEntities="False" ClientIDMode="Static" AutoFillEnabled="True" Required="True" OnControlValidateClientScript="pickerValidate" OnValueChangedClientScript="valueChanged"  ID="AssignedTo" VisibleSuggestions="10"/>

<script type="text/javascript">
var valueChanged = function (elementId, users) {
    ///<summary>Handler for value changed event</summary>
    /// <param type="String">Element id</param>
    /// <param Type="Array" ElementType="Object">Users</param>
}

var pickerValidate = function (elementId, users) {
    ///<summary>Handler for picker validate event</summary>
    /// <param type="String">Element id</param>
    /// <param Type="Array" ElementType="Object">Users</param>
}
 </script>

So far so good, but I think there is still a question to be answered. How do we get the value of the control?

To get the value of people picker control, you can do the following.

// Get the div
var div = document.querySelector('div[id*="AssignedTo"][id*="TopSpan"]');
// Get instance of the people picker
var ctl = SPClientPeoplePicker.SPClientPeoplePickerDict[div.id];
// Obtaining the users as an array of objects
var users = ctl.GetAllUserInfo();

What I do not like here is querySelector, but you can bypass this if you are setting “ClientIDMode” property to “Static”. This means that you can calculate client id of container div by concatenating ID of the control you have set declarative with “_TopSpan”.

// Get the div
var div = $get("AssignedTo" + "_TopSpan");
// Get instance of the people picker
var ctl = SPClientPeoplePicker.SPClientPeoplePickerDict[div.id];
// Obtaining the users as an array of objects
var users = ctl.GetAllUserInfo();

An interesting fact is that value of client picker control is persistent after complete post back. This means you can use new control, even if is client side one, using the development techniques you used for previous version of SharePoint.

Advertisements