How to Assign Required Policies with Microsoft Entra ID Dynamic Groups Not every employee ...
How to use Search (KeywordQuery) to query user profiles in SharePoint
Have you ever need to write C# code that returns a list of people (user profiles) and for it also to be lightning quick? Obviously, you could use the UserProfileManager to do this, but be warned it’s NOT quick as you more often that not have to bring back all of the User Profiles and then iterate through them to find the ones matching your criteria. It’s way too slow!
So, how do you do it and also get the performance you need ? Answer : use Search (KeywordQuery).
The following code is an example of how you can get the results you need into a DataTable.
private static DataTable GetPeople(SPSite spSite, string queryText) { var keywordQuery = new KeywordQuery(spSite) { QueryText = queryText, KeywordInclusion = KeywordInclusion.AllKeywords, HiddenConstraints = "scope:" + ""People"", ResultTypes = ResultType.RelevantResults, RankingModelId = "D9BFB1A1-9036-4627-83B2-BBD9983AC8A1" }; keywordQuery.RowLimit = 2; keywordQuery.SelectProperties.Add("AccountName"); keywordQuery.SelectProperties.Add("UserProfile_GUID"); keywordQuery.SelectProperties.Add("PreferredName"); keywordQuery.SelectProperties.Add("WorkEmail"); keywordQuery.SelectProperties.Add("OfficeNumber"); keywordQuery.SelectProperties.Add("PictureURL"); keywordQuery.SelectProperties.Add("MobilePhone"); ResultTableCollection resultTableCol = keywordQuery.Execute(); ResultTable resultTable = resultTableCol[ResultType.RelevantResults]; var resultsDataTable = new DataTable { TableName = "Results" }; resultsDataTable.Load(resultTable, LoadOption.OverwriteChanges); return resultsDataTable; }
Once you have a DataTable returned then it’s just a simple job of using the rows and columns to render out to the screen, or to pump into another process.
You may also like:
November 13, 2026
November 6, 2026
How Do You Migrate Legacy Policies into a Policy Management System? For a lot ...
October 30, 2026
Which Departments Benefit from Policy Management Software in Microsoft 365? Many organizations view policy ...
October 23, 2026
Which Departments Benefit from Policy Management Software in Microsoft 365? Many organizations view policy ...
October 16, 2026
How to Assign Required Policies with Microsoft Entra ID Dynamic GroupsNot every employee needs ...
October 9, 2026
Microsoft Purview vs Policy Acknowledgment Software: What Each Tool Does (and Doesn’t) DoMicrosoft 365 ...