In this blog, I will explain how to find users using ClientPeoplePickerSearchUser class. There are many ways in CSOM to get the users from SharePoint site or the web but I could not find any proper way to perform a wildcard search of users from SharePoint site using CSOM. For example, let's say I want to find all the users present in the site collection where the first name starts with “John” or the last name starts with “Patt”.

This below-mentioned code sample will help us to perform both wildcard and explicit search. The output will be matching the result whether it is a collection of users or a single user.

The script for this is given below.
  1. using Microsoft.SharePoint.Client;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Security;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. namespace FindUserDetailsByPeoplepickerQuery {
  9. class Program {
  10. static void Main(string[] args) {
  11. string userToFind = "userToFind"; //it may be full or partial name of the user
  12. string webUrl = "<site url>";
  13. string userName = "<user name>";
  14. string password = "<password>";
  15. SecureString passWord = new SecureString();
  16. try {
  17. //creating the context of the SharePoint site
  18. using(ClientContext clientCtx = new ClientContext(webUrl)) {
  19. foreach(char c in password.ToCharArray())
  20. passWord.AppendChar(c);
  21. clientCtx.Credentials = new SharePointOnlineCredentials(userName, passWord);
  22. clientCtx.ExecuteQuery();
  23. //preparing the search query options
  24. Microsoft.SharePoint.ApplicationPages.ClientPickerQuery.ClientPeoplePickerQueryParameters peoplePickerQuery = new Microsoft.SharePoint.ApplicationPages.ClientPickerQuery.ClientPeoplePickerQueryParameters();
  25. peoplePickerQuery.AllowEmailAddresses = true;
  26. peoplePickerQuery.AllowMultipleEntities = false;
  27. peoplePickerQuery.ForceClaims = false;
  28. peoplePickerQuery.MaximumEntitySuggestions = 60;
  29. peoplePickerQuery.PrincipalType = Microsoft.SharePoint.Client.Utilities.PrincipalType.All;
  30. peoplePickerQuery.PrincipalSource = Microsoft.SharePoint.Client.Utilities.PrincipalSource.All;
  31. peoplePickerQuery.QueryString = userToFind;
  32. peoplePickerQuery.AllUrlZones = true;
  33. peoplePickerQuery.SharePointGroupID = 0;
  34. peoplePickerQuery.WebApplicationID = new Guid("00000000-0000-0000-0000-000000000000");
  35. //Using peoplepicker webservice and searchquery getting the matched userInformations from SharePoint
  36. ClientResult < String > resultData = Microsoft.SharePoint.ApplicationPages.ClientPickerQuery.ClientPeoplePickerWebServiceInterface.ClientPeoplePickerSearchUser(clientCtx, peoplePickerQuery);
  37. clientCtx.ExecuteQuery();
  38. if (resultData != null) {
  39. Object[] queryMatchesFound = (Object[]) clientCtx.ParseObjectFromJsonString(resultData.Value);
  40. for (var i = 0; i < queryMatchesFound.Length; i++) {
  41. try {
  42. Dictionary < string, object > obj = (Dictionary < string, object > ) queryMatchesFound[i];
  43. try {
  44. string loginName = obj["Key"].ToString();
  45. } catch {} //this will give the loginName of the user
  46. try {
  47. string email = obj["Description"].ToString();
  48. } catch {} //this will give the email of the user
  49. try {
  50. string Title = obj["DisplayText"].ToString();
  51. } catch {} //this will give the title/DisplayName of the user
  52. try {
  53. string entityType = obj["EntityType"].ToString();
  54. } catch {} //this will give the type of the object whether it is a group or user
  55. } catch {}
  56. }
  57. }
  58. }
  59. } catch {}
  60. }
  61. }
  62. }

In this example, I am getting the values in a JSON String format. Then, I am converting it into an object and by looping the collection, we can get the required information of the users like loginname, displayname, email etc.

Conclusion

In this blog, I have explained how you can get multiple users based on wildcard search. I hope this information will help you out in a few scenarios.