Ram Prasad Meenavalli

C# .NET Client API code snippet to search/query for people in SharePoint Server 2013/2016

Published on 19-Jan-2017 | Comments

In this post I show how to query/search for people within a .Net application (windows application or a console application) using the .Net Client API for SharePoint Server.

Though we have REST API to search for people and Client People picker control (JS based), it is difficult or impossible to use these within a windows application. Also, there might be scenarios where User Profile Service is not available within your farm, and you have to query for people within your client application. For example, let's say you might want to assign a task to an SP user. In such scenarios, we can use the 'ClientPeoplePickerWebServiceInterface' and 'ClientPeoplePickerQueryParameters' classes from Microsoft.SharePoint.ApplicationPages.ClientPickerQuery namespace. These are the same ones used by the out of the box people picker. So, the results would be identical. These classes are available within the 'Microsoft.SharePoint.Client.dll' assembly.

Below is the code snippet to query for people or groups.

String strSearchPhrase = "Ram Meenavalli" ClientContext spCtx; spCtx = new ClientContext("http://ramspc/sites/teamsite"); ClientPeoplePickerQueryParameters query = new ClientPeoplePickerQueryParameters(); query.AllowEmailAddresses = true; query.MaximumEntitySuggestions = 30; query.PrincipalType = PrincipalType.SecurityGroup | PrincipalType.User; query.PrincipalSource = PrincipalSource.All; query.QueryString = strSearchPhrase; query.WebApplicationID = new Guid("00000000-0000-0000-0000-000000000000"); ClientResult<String> result = ClientPeoplePickerWebServiceInterface.ClientPeoplePickerSearchUser(spCtx, query); spCtx.ExecuteQuery(); Console.Write(result.Value);

The result.Value will give the results in JSON format, which will have all the required properties for each user. Sample format is shown below

[ {"Key" : "XX\\XXXXXXXX", "Description" : "XXXXXXXXX", "DisplayText" : "XXXXXXXXXXX", "EntityType" : "User", "ProviderDisplayName" : "Active Directory", "ProviderName" : "AD", "IsResolved" : true, "EntityData" : {"Title" : "", "MobilePhone" : "", "SIPAddress" : "XXXXXXXXXXXXX", "Department" : "XXXXXXXXXX", "Email" : "XXXXXXXXXXXXX"}, "MultipleMatches" : []}, {"Key" : "XXXXXXXX", "Description" : "XXXXXXXXXX", "DisplayText" : "XXXXXXXXXXXX", "EntityType" : "User", "ProviderDisplayName" : "Active Directory", "ProviderName" : "AD", "IsResolved" : true, "EntityData" : {"Title" : "", "MobilePhone" : "", "SIPAddress" : "", "Department" : "XXXXXXXXXXXX", "Email" : ""}, "MultipleMatches" : []} ]

There are many utility classes available using which the JSON can be de-serialized and access the required properties of a user or security group.


View Ram Prasad Meenavalli's LinkedIn profile

SharePoint Technology Evangelist