Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

How to Use Windows Vista Search API from a WPF Application

0.00/5 (No votes)
1 Nov 2007 1  
This article explains the proper way of using Windows Vista Search provider inside a WPF application. It incorporates DataBinding, Query provides and other useful technologies, introduces within Windows Vista and Windows Presentation Foundation

Introduction

Another great service provided by Windows Vista is its integrated search. Can we use it from our WPF application? Sure we can. This is how you'll do it.

Background

First of all, you'll need to find Windows Search API library inside Windows SDK. Locate SearchAPI.tlb and process it with tlbimp tool to create managed assembly to reference to. You'll get a file, named SeachAPILib.dll. This one, you will need. In order to use it, refer to MSDN documentation of Windows Search API 3.0. Actually, using the Search API is not much different than using database queries. However, this is not so trivial in core. Actually, the syntax of Windows Search (AQS - Advanced Query Syntax) is very different. But the search team created brilliant work to make our life easier. CSearchManager and ISeachQueryHelper - those translate AQS to SQL and, even provide us with a proper connection string.

Using the Code

First of all, we'll have to create CSeachManager:

CSearchManager cManager = new CSearchManagerClass(); 

Then, while you are in query, you'll create new ISearchQueryHelper to help you with translations and query strings:

ISearchQueryHelper cHelper = cManager.GetCatalog("SYSTEMINDEX").GetQueryHelper();
cHelper.QuerySelectColumns = "\"System.ItemNameDisplay\""; 

Well done, now. Ask Vista with old good OleDB provider:

using (cConnection = new OleDbConnection(cHelper.ConnectionString)) 
{ 
cConnection.Open(); 
using (OleDbCommand cmd = new OleDbCommand( 
cHelper.GenerateSQLFromUserQuery(SearchString),cConnection)) 
 { 
  if (cConnection.State == ConnectionState.Open) 
  { 
   using (OleDbDataReader reader = cmd.ExecuteReader()) 
    { 
     m_results.Clear(); 
     while (!reader.IsClosed && reader.Read()) 
     { 
      m_results.Add(reader[0].ToString()); 
     } 
    reader.Close();
   } 
 }
} 
cConnection.Close(); 
}

Brilliant. Let's move it into WPF by creating DependencyObject, that provides us with all we need for search - search string and array of results:

public static readonly DependencyProperty SearchTextProperty = 
DependencyProperty.Register
    ("SearchText", typeof(string), typeof(VistaSearchProviderHelper), 
new UIPropertyMetadata(default(string),
new PropertyChangedCallback(OnSearchTextChanged)));

static void OnSearchTextChanged
    (DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
//change
 ToAbortFlag = IsWorkingFlag;
 if (workerCallback == null) workerCallback = new WaitCallback(doSearch);
 searchObjState.SearchString = e.NewValue.ToString();
//if (searchObjState.SearchString.Length > 2)
 {
  workerCallback.BeginInvoke(searchObjState, null, null);
 }
}
public string SearchText
 {
  get { return GetValue(SearchTextProperty).ToString(); }
  set { SetValue(SearchTextProperty, value); }
 }
static ThreadSafeObservableCollection<string> m_results;
public ReadOnlyObservableCollection<string> Results
{
 get { return new ReadOnlyObservableCollection<string>(m_results); }
}

This code uses a custom class named ThreadSafeObservableCollection, introduced earlier in my blog.

The only thing we should do now is to bind input and output to the XAML presentation:

<StackPanel FocusManager.FocusedElement="{Binding ElementName=searchStr}">
 <StackPanel.DataContext>
  <ObjectDataProvider ObjectType="l:VistaSearchProviderHelper"/>
 </StackPanel.DataContext>
 <TextBox Name="searchStr" Text="{Binding Path=SearchText, 
    UpdateSourceTrigger=PropertyChanged }" FontSize="30"/>
 <ListBox ItemsSource="{Binding Path=Results}" />
</StackPanel> 

Conclusion

Now we can search our Windows Vista from a custom WPF application. This is not the final application for sure. There is a long way to go to make it work perfectly, but this is the beginning of how to do it.

History

  • 1st November, 2007: Initial post

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here