Today I have my first sessions here in Phoenix, one on Web Parts and one on InfoPath. While reviewing yesterday my demos, I came upon a piece of code I was planning to update already for a long time. It involves a cross-site CAML query using the new SPSiteDataQuery class and displaying the the outcome in a dropdown. The dropdown displays the different types of beers I have in my many beer lists sitting in sites in my site collection.

My challenge was to come up with a better solution to make sure we have distinct values in the dropdown. I recognize that some of you might know this already but to be honest, I did not know about this super-easy way that is now available at the level of the ADO.NET DataView class. There is this ToTable() method you can call, letting your DataView create a new ADO.NET DataTable and you can enforce distinct values. I love the .NET 2.0 Framework! Here is the snippet:

  public static DataTable GetBeerTypes()
  {
            // -- retrieve the reference to the site where the Web part is on
            SPWeb web = SPContext.Current.Web;

            // -- retrieve all the different types of beers via a CAML query
            SPSiteDataQuery qry = new SPSiteDataQuery();
            qry.Query = "<OrderBy><FieldRef Name='Type' /></OrderBy>";
            qry.ViewFields = "<FieldRef Name='Type' />";
            qry.Lists = "<Lists ServerTemplate='100' />";
            qry.Webs = "<Web Scope='Recursive' />";

            // -- execute the query
            DataTable tbl = web.GetSiteData(qry);

            // -- return the unique types
            DataView v = new DataView(tbl);
            return v.ToTable(true, "Type");
  }