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.
- 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";
- string webUrl = "<site url>";
- string userName = "<user name>";
- string password = "<password>";
- SecureString passWord = new SecureString();
- try {
-
- using(ClientContext clientCtx = new ClientContext(webUrl)) {
- foreach(char c in password.ToCharArray())
- passWord.AppendChar(c);
- clientCtx.Credentials = new SharePointOnlineCredentials(userName, passWord);
- clientCtx.ExecuteQuery();
-
- 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");
-
- 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 {}
- try {
- string email = obj["Description"].ToString();
- } catch {}
- try {
- string Title = obj["DisplayText"].ToString();
- } catch {}
- try {
- string entityType = obj["EntityType"].ToString();
- } catch {}
- } 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.
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.