Wildcard search of Users using CSOM in SharePoint

  • Posted by: Softree Technology

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 web but I did not find any proper way to perform wildcard search of users from SharePoint site using CSOM. For e.g lets, I want to find all users present in site collection where the first name starts with “John” or last name starts with “Patt”.

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

The script as follows:-

[code lang=”c”]

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 { }
}
}
}

[/code]

In this example, I am getting the values in JsonString format. Then I am converting it to object and by looping the collection we can get the required info about the users like loginname, displayname, email etc.

In this blog, I have explained have you can get multiple users based on wildcard search based on either start name or last name or email id or with a partial name. Hope this information will help you out in a few scenarios.

This solution is brought to you by our SharePoint professionals.

Softree Consulting employs SharePoint consultants; we are a technology services provider with the aim to help companies achieve exceptional performance through SharePoint. Our dedicated team of SharePoint consultants has the right bent of mind to understand and execute customer requirements.

Be it SPFx or SharePoint add-in developments, SharePoint 2019 developments, web part developments, migrating from SharePoint 2010/2013 to SharePoint 2013/2016/Office 365, Office 365, SharePoint hosted apps development or something else in SharePoint, we strive to deliver the best

Author: Softree Technology

Leave a Reply