Leon Havin
Leon Havin

Reputation: 187

Programmatically filtering contacts in Outlook 2010 Add-In

I am trying to programmatically filter Outlook contacts in the Contacts folder in Outlook 2010. I followed DASL filter rules, but it seems working for Find function and throws exception when I assign this filter to view.Filter = FilterString. Any ideas what I am doing wrong? The correct result would display filtered contacts in the existing contacts view.

Outlook.Application myApp = ThisAddIn.myApp; 
Outlook.NameSpace myNamespace = ThisAddIn.nSpace; 
Outlook.MAPIFolder myContactsFolder = ThisAddIn.contactsFolder;
if (myContactsFolder == null)
{
    Log.Verbose("Contacts folder not found");
    return null;
}

Outlook.Items contactItems = ThisAddIn.contactItems; 

//use filter to take only contact and not DistListItem  
Outlook.Items outlookContacts = contactItems.Restrict("[MessageClass] = 'IPM.Contact'");
Outlook.ContactItem contact = null;

int iOutlookContacts = contactItems.Count;
if (iOutlookContacts > 0)
{
    string FilterString = "[FullName]='" + param + "'"; 

    // Find works with this filter
    contact = (Outlook.ContactItem)outlookContacts.Find(FilterString); 
    if (contact != null)
    {
        // need to display in contacts search window
        Outlook.View currentView = myApp.ActiveExplorer().CurrentView;
        currentView.Filter = FilterString; // cannot parse exception occurs here

        currentView.Save();
        currentView.Apply();
    }
}

Upvotes: 2

Views: 1952

Answers (1)

SliverNinja - MSFT
SliverNinja - MSFT

Reputation: 31641

You need to change your DASL search filter to this:

string FilterString = "\"urn:schemas:contacts:cn\"='" + param + "'"; 

[FullName] is not a valid search filter column. The Full Name field maps to the DASL search column urn:schemas:contacts:cn

Upvotes: 3

Related Questions