About      |       Articles      |      Services      |      Software      |      Contact

Latest Free SharePoint Software

ARB Security Solutions regularly releases free SharePoint software, including WebParts, Client Applications, Framework Extensions, and other Miscellaneous Components.
The most recent freeware is:

Title: Simple SharePoint Rollup WebPart
Date Published: 10/22/2009

Previous Two Free WebPart Releases:

SecureCenter For SharePoint

By SharePoint security integrators, for SharePoint security integrators.

SharePoint Security Assurance Program™

For externally facing SharePoint deployments, security is an acutely important deployment concern. Learn how through daily security scanning, you can ensure external business users and partners that they can collaborate in confidence!

Security Assurance WebPart:



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

C#:
  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/Bookmark

2 Comments »

  1. [...] 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

  2. [...] 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

RSS feed for comments on this post. TrackBack URL

Leave a comment