Using the KeywordQuery Class to Get a List of Department Employees

Along the same lines as this post, once one has located the relevant department that they desire from a collection, how is it possible to use that department string name in order to get a list of the relevant employees? As we used the KeywordQuery class the last time, we are going to take the same approach in order to return a new DataTable with those values.

Here is the code in order to execute that:

[csharp]
public static DataTable BuildDepartmentEmployees(string url, string deptName, int queryLimit)
{
using (var result  = new DataTable())
{
using (var site = new SPSite(url))
{
using (var query = new KeywordQuery(site))
{
query.ResultTypes = ResultType.RelevantResults;
query.EnableStemming = true;
query.TrimDuplicates = true;
query.StartRow = 0;
query.RowLimit = queryLimit;
string str =
query.QueryText = string.Format(“scope:\”{0}\””, “people”) + string.Format(” department:\”{0}\””, deptName);
query.SortList.Add(“Rank”, SortDirection.Ascending);
query.SelectProperties.Add(“AccountName”);
query.SelectProperties.Add(“FirstName”);
query.SelectProperties.Add(“LastName”);
query.SelectProperties.Add(“JobTitle”);
query.SelectProperties.Add(“WorkEmail”);
query.SelectProperties.Add(“Department”);
query.SelectProperties.Add(“WorkPhone”);
query.SelectProperties.Add(“Fax”);
query.SelectProperties.Add(“UserProfile_GUID”);
query.SelectProperties.Add(“UserName”);
query.SelectProperties.Add(“PreferredName”);
query.SelectProperties.Add(“OfficeNumber”);
query.SelectProperties.Add(“MobilePhone”);
query.SelectProperties.Add(“PIctureURL”);
query.SelectProperties.Add(“WebSite”);
query.SelectProperties.Add(“PublicSiteRedirect”);
using (ResultTable reader = query.Execute()[ResultType.RelevantResults])
{
result.Load(reader, LoadOption.OverwriteChanges);
}
}
}
return result;
}
}
[/csharp]

Share

One thought on “Using the KeywordQuery Class to Get a List of Department Employees”

Leave a Reply

Your email address will not be published. Required fields are marked *