PeoplePicker in Provider Hosted App

Adding a Peoplepicker control to a SharePoint app is a very common requirement in any SharePoint App project. Until SharePoint 2010 this was a very easy task as Microsoft provided a people picker control which can be used in service side code.

SharePoint from 2013 onward shifted the focus shifted client side coding and hence building this control was not easily available. There are some resources available on MSDN which helps to build this control

For SharePoint hosted app:

For provide hosted app:

In this article I want to showcase one more option for building this control using Kendo control which can be used easily for people picker action. HTML dropdown control can also be used for this and can be tried.

We will be using SP.Utilities.Utility.SearchPrincipals for searching user and returning a list of users which can be bound to a dropdown control to simulate a people picker control.

The detailed steps are as below,

  1. Create a MVC based provider hosted app for SharePoint 2013.

  2. Add kendo.all.min.js in the scripts folder for reference,

  3. Add a input control to your view as below,
    1. <input name="Users" id="users" /> 
  4. Add a js file named peoplepicker.js to your solution in scripts folder. Here we will write all code for binding the control. This can be any existing file as per project structure.

  5. Add reference to below assemblies in HomeController,
    1. using System.Runtime.Serialization;  
    2. using Microsoft.SharePoint.Client.Utilities;  
  6. Add below method in Homecontroller
    1. public JsonResult searchPerson(string Id)  
    2. {  
    3.     List < SPUser > userlist = new List < SPUser > ();  
    4.     var searchUsers = new PeoplePicker();  
    5.     userlist = searchUsers.SearchPeople(Id, System.Web.HttpContext.Current.Session["SPHostUrl"].ToString(), System.Web.HttpContext.Current.Session["AccessToken"].ToString());  
    6.     return Json(userlist, JsonRequestBehavior.AllowGet);  
    7. }  
    This method assumes that SPHostURL and Access token are stored in session. The searchpeople method can return a List of SPUser which can have predefined attributes which we will be binding to dropdown control.

  7. The SearchPeople method can be build using Searchprinciples as below,
    1. public List < SPUser > SearchPeople(string searchText, string hostWeb, string contextToken)  
    2.   
    3. {  
    4.   
    5.     try {  
    6.   
    7.         List < SPUser > people = new List < SPUser > ();  
    8.   
    9.         using(SP.ClientContext ctx = TokenHelper.GetClientContextWithAccessToken(hostWeb, contextToken))  
    10.   
    11.         {  
    12.   
    13.             SP.Web web = ctx.Web;  
    14.   
    15.             ctx.Load(web);  
    16.   
    17.             var results = SP.Utilities.Utility.SearchPrincipals(ctx, web, searchText,  
    18.   
    19.                 SP.Utilities.PrincipalType.User, SP.Utilities.PrincipalSource.All, null, 10);  
    20.   
    21.             ctx.ExecuteQuery();  
    22.   
    23.             foreach(var user in results)  
    24.   
    25.             {  
    26.   
    27.                 SPUser uUser = new SPUser(user);  
    28.   
    29.                 people.Add(uUser);  
    30.   
    31.             }  
    32.   
    33.         }  
    34.   
    35.         return people;  
    36.   
    37.     } catch (Exception ex)  
    38.   
    39.     {  
    40.   
    41.         throw ex;  
    42.   
    43.     }  
    44.   
  8. Once we get the result from the above method we can bind the list of users to Kendo dropdown by below syntax,

  1. <script>  
  2.     $(document).ready(function() {  
  3.   
  4.         $("#users").kendoDropDownList({  
  5.   
  6.             dataTextField: "ContactName",  
  7.   
  8.             dataValueField: "CustomerID",  
  9.   
  10.             headerTemplate: '<div class="dropdown-header k-widget k-header">' +  
  11.   
  12.                 '<span>Photo</span>' +  
  13.   
  14.                 '<span>Contact info</span>' +  
  15.   
  16.                 '</div>',  
  17.   
  18.             valueTemplate: '<span class="selected-value" style="background-image: url(\'../content/web/Customers/#:data.CustomerID#.jpg\')"></span><span>#:data.ContactName#</span>',  
  19.   
  20.             template: '<span class="k-state-default" style="background-image: url(\'../content/web/Customers/#:data.CustomerID#.jpg\')"></span>' +  
  21.   
  22.                 '<span class="k-state-default"><h3>#: data.ContactName #</h3></span>',  
  23.   
  24.             dataSource: {  
  25.   
  26.                 transport: {  
  27.   
  28.                     read: function(e) {  
  29.   
  30.                         $http({  
  31.   
  32.                             url: '/PHA/Home/searchPerson/' + e.data.filter.filters[0].value, // pass search text value here  
  33.   
  34.                             method: "GET"  
  35.   
  36.                         }).then(function(data) {  
  37.   
  38.                             var users = [];  
  39.   
  40.                             angular.forEach(data.data, function(item) {  
  41.   
  42.                                 users.push({  
  43.   
  44.                                     Login: item.CustomerID,  
  45.   
  46.                                     Name: item.ContactName,  
  47.   
  48.                                     Email: item.CustomerEmail  
  49.   
  50.                                 });  
  51.   
  52.                             });  
  53.   
  54.                             e.success(users);  
  55.   
  56.                         }, function(error) {  
  57.   
  58.                             console.log(error);  
  59.   
  60.                         });  
  61.                     }  
  62.                 }  
  63.             }  
  64.         });  
  65.   
  66.         var dropdownlist = $("#users").data("kendoDropDownList");  
  67.   
  68.     });  
  69. </script>