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.
- using Microsoft.SharePoint.Client;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Security;
- using System.Text;
- using System.Threading.Tasks;
- namespace FindUserDetailsByPeoplepickerQuery {
- class Program {
- static void Main(string[] args) {
- string userToFind = "userToFind"; //it may be full or partial name of the user
- string webUrl = "<site url>";
- string userName = "<user name>";
- string password = "<password>";
- SecureString passWord = new SecureString();
- try {
- //creating the context of the SharePoint site
- using(ClientContext clientCtx = new ClientContext(webUrl)) {
- foreach(char c in password.ToCharArray())
- passWord.AppendChar(c);
- clientCtx.Credentials = new SharePointOnlineCredentials(userName, passWord);
- clientCtx.ExecuteQuery();
- //preparing the search query options
- Microsoft.SharePoint.ApplicationPages.ClientPickerQuery.ClientPeoplePickerQueryParameters peoplePickerQuery = new Microsoft.SharePoint.ApplicationPages.ClientPickerQuery.ClientPeoplePickerQueryParameters();
- peoplePickerQuery.AllowEmailAddresses = true;
- peoplePickerQuery.AllowMultipleEntities = false;
- peoplePickerQuery.ForceClaims = false;
- peoplePickerQuery.MaximumEntitySuggestions = 60;
- peoplePickerQuery.PrincipalType = Microsoft.SharePoint.Client.Utilities.PrincipalType.All;
- peoplePickerQuery.PrincipalSource = Microsoft.SharePoint.Client.Utilities.PrincipalSource.All;
- peoplePickerQuery.QueryString = userToFind;
- peoplePickerQuery.AllUrlZones = true;
- peoplePickerQuery.SharePointGroupID = 0;
- peoplePickerQuery.WebApplicationID = new Guid("00000000-0000-0000-0000-000000000000");
- //Using peoplepicker webservice and searchquery getting the matched userInformations from SharePoint
- ClientResult < String > resultData = Microsoft.SharePoint.ApplicationPages.ClientPickerQuery.ClientPeoplePickerWebServiceInterface.ClientPeoplePickerSearchUser(clientCtx, peoplePickerQuery);
- clientCtx.ExecuteQuery();
- if (resultData != null) {
- Object[] queryMatchesFound = (Object[]) clientCtx.ParseObjectFromJsonString(resultData.Value);
- for (var i = 0; i < queryMatchesFound.Length; i++) {
- try {
- Dictionary < string, object > obj = (Dictionary < string, object > ) queryMatchesFound[i];
- try {
- string loginName = obj["Key"].ToString();
- } catch {} //this will give the loginName of the user
- try {
- string email = obj["Description"].ToString();
- } catch {} //this will give the email of the user
- try {
- string Title = obj["DisplayText"].ToString();
- } catch {} //this will give the title/DisplayName of the user
- try {
- string entityType = obj["EntityType"].ToString();
- } catch {} //this will give the type of the object whether it is a group or user
- } catch {}
- }
- }
- }
- } catch {}
- }
- }
- }
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.
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.

Join the conversation! Your thoughts help the community grow.