kamagra how much to take

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 :)

  1. public static DataTable RetrieveDepartmentNames(string url, int rowLimit, string department)
  2. {
  3. using (var table = new DataTable())
  4. {
  5. using (var site = new SPSite(url))
  6. {
  7. using (var query = new KeywordQuery(site))
  8. {
  9. query.ResultTypes = ResultType.RelevantResults;
  10. query.EnableStemming = true;
  11. query.TrimDuplicates = true;
  12. query.StartRow = 0;
  13. query.RowLimit = rowLimit;
  14. query.QueryText = string.Format("scope:\"{0}\"", "people");
  15. query.SortList.Add("Rank", SortDirection.Ascending);
  16. query.SelectProperties.Add("Department");
  17. using (ResultTable reader = query.Execute()[ResultType.RelevantResults])
  18. {
  19. table.Load(reader, LoadOption.OverwriteChanges);
  20. }
  21. if (((table.Rows != null)) && (table.Rows.Count > 0))
  22. {
  23. using (var view = new DataView(table) {Sort = department, RowFilter = (string.Format("NOT Isnull({0},'') = '' AND NOT {0} = ''", department))})
  24. {
  25. return view.ToTable("Department", true, new[] { department });
  26. }
  27. }
  28. }
  29. }
  30. }
  31. return null;
  32. }

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!

Share

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>