Using the KeywordQuery Class to Get a List of Departments
On a recent side project of mine I was writing some SharePoint UserProfile heavy code in order to offer different schemes of showing user data. One of the common methods that I was using was collection building of arbitrary pieces of related user and business structure data, such as building lists of relevant departments assimilated by different data display. While there exist several mechanisms in order to support such a generic call, I have found the Microsoft.Office.Server.Search.Query.KeywordQuery class to be most effective and from a programmatic standpoint exceptionally easy to use (and it’s not just because I think the EnableUrlSmashing property has a nifty name). At a very high level, the KeywordQuery class simply provides the mechanisms in order to execute keyword syntax queries in the same manner as MOSS, returning a DataTable object.
The SDK IMHO adequately covers the innards of the particular classes, so let’s just get down to the code :)
-
public static DataTable RetrieveDepartmentNames(string url, int rowLimit, string department)
-
{
-
{
-
{
-
{
-
query.ResultTypes = ResultType.RelevantResults;
-
query.EnableStemming = true;
-
query.TrimDuplicates = true;
-
query.StartRow = 0;
-
query.RowLimit = rowLimit;
-
query.QueryText = string.Format("scope:\"{0}\"", "people");
-
query.SortList.Add("Rank", SortDirection.Ascending);
-
query.SelectProperties.Add("Department");
-
using (ResultTable reader = query.Execute()[ResultType.RelevantResults])
-
{
-
table.Load(reader, LoadOption.OverwriteChanges);
-
}
-
if (((table.Rows != null)) && (table.Rows.Count> 0))
-
{
-
using (var view = new DataView(table) {Sort = department, RowFilter = (string.Format("NOT Isnull({0},'') = '' AND NOT {0} = ''", department))})
-
{
-
}
-
}
-
}
-
}
-
}
-
return null;
-
}
Important to note is that DataTable objects implement IDisposable since it inherits from MarshalByRefComponent, and even while most consider it unnecessary it is proper to place the references in using statements!
Related posts:
- Using the KeywordQuery Class to Get a List of Department Employees
- SharePoint ECMAScript (Client Object Model) Empty List Helper
- System.ServiceModel.FaultException Error When Using QueryService.QueryEx
- Solving SharePoint FullTextSqlQuery Wrong Site Collection Error
- SharePoint Security Helper Class
2 Comments »
RSS feed for comments on this post. TrackBack URL























Articles & Research
SharePoint Architecture
Research Methodology
Personal/Off-Topic
Article Or Research Filed Under 

[...] 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. [...]
Pingback by SharePoint Shelter » Blog Archive » — November 7, 2008 @ 11:20 am
[...] Using the KeywordQuery Class to Get a List of Departments [...]
Pingback by Links (11/9/2008 « Steve Pietrek - Everything SharePoint) — November 9, 2008 @ 4:53 pm