For those of you using Backbone.js and Backbone.Stickit for client-side SharePoint development, this is a quick article describing how to add a global Stickit handler for SharePoint client People Picker controls.
In this example, we have a SharePoint Person or Group field with an internal name of WorkFor (and a corresponding attribute in our Backbone model) that we want to render using the client People Picker control through our Backbone view.
The following screenshot shows the end result:
Though the client People Picker control is often helpful and has become a de-facto standard for SharePoint users, it does present some challenges when used with backbone and the SharePoint REST API:
- A SharePoint Person or Group field stores the numeric SharePoint ID of the selected user (not the user's account name).
- The client People Picker API doesn't return the SharePoint ID of the selected user.
- In order to set the appropriate SharePoint list item property or backbone model attribute, the selected user's ID must be fetched from SharePoint whenever the selected user is changed.
- The SharePoint REST API returns the Person or Group field with 'Id' appended to the end of the field's actual internal name (see User, Person or Group Fields with REST in SharePoint 2013 for more details). So for this example, we must use 'WorkForId' whenever we need to reference the WorkFor field.
In Stickit, we can create a global handler for specific elements (identified using standard selectors) that eliminates the need to manually create and attach 3rd-party controls outside of the normal Backbone and Backbone.Stickit flow.
- Define the HTML in your view template.
- Add the global Stickit handler in your view.
- Define your Stickit bindings as you normally would. When stickit(this.model, this.bindings) is called in your view's render() function, the control is automatically created and attached to the appropriate element.
The same technique can be used for a variety of client-side controls and plugins to simplify and add consistency to your SharePoint CSOM/JSOM code.