Click here to Skip to main content
Click here to Skip to main content

How to Use Windows Vista Search API from a WPF Application

, 1 Nov 2007 CPOL
Rate this:
Please Sign up or sign in to vote.
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Tamir Khason
Architect Better Place
Israel Israel
Hello! My name is Tamir Khason, and I am software architect, project manager, system analyst and [of course] programmer. In addition to writing big amount of documentation, I also write code, a lot of code. I used to work as a freelance architect, project manager, trainer, and consultant here, in Israel, but recently join the company with extremely persuasive idea - to make a world better place. I have very pretty wife and 3 charming kids, but unfortunately almost no time for them.
 
To be updated within articles, I publishing, visit my blog or subscribe RSS feed. Also you can follow me on Twitter to be up to date about my everyday life.

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web02 | 2.8.141022.1 | Last Updated 1 Nov 2007
Article Copyright 2007 by Tamir Khason
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid