C# VSTO Outlook, trying to get a query to search by conversation ID but whatever I put it either errors or gives no results

24 Views Asked by At

I want to be able to search from the ribbon in my Add in, but when I construct a query based on the conversationID and attempt to run an advanced search I get the same exception every time:

System.Runtime.InteropServices.COMException: 'The operation failed.'

The code looks something like the following:

private async void inSearchId_TextChanged(object sender, RibbonControlEventArgs e)
{
var searchText = (sender as RibbonEditBox).Text;
if (searchText.Length < 5) return; // No point searching until theres a decent query to search against

var conversationLinks = await GetConversationsBySearch(searchText);

var filter = BuildFilterFromConversationIds(conversationLinks);

Search search = _app.AdvancedSearch("inbox", filter, true, "CustomQuery");
search.Results.Sort("[ReceivedTime]", OlSortOrder.olDescending);
}

The return value of BuildFilterFromConversationIds is

@SQL="http://schemas.microsoft.com/mapi/proptag/0x3013001E" = '22E6722D04D84704B28B060C1CA5AB92'

Or in another iteration:

@SQL="urn:schemas:httpmail:conversationid" = '22E6722D04D84704B28B060C1CA5AB92'

Both of which throw the above error.

I am able to get it to work (as a test) with the following query:

urn:schemas:httpmail:subject LIKE '%Test%'

I've looked in a lot of places, but I can't see what it is that I'm missing. What should the query look like to search by conversation ID? And why is it throwing the exception?

1

There are 1 best solutions below

0
Dmitry Streblechenko On

PR_CONVERSATION_ID property is 0x30130102 (note that the type is PT_BINARY = 0x0102), not 0x3013001E (type is PT_STRING8 = 0x001E).

But OOM would not let you search on binary (PT_BINARY) properties. If using Redemption is an option (I am its author), it does allow binary searches. Off the top of my head:

Redemption.RDOSession session = new Redemption.RDOSession();
session.MAPIOBJECT = _app.Session.MAPIOBJECT;
var inbox = session.GetDefaultFolder(OlDefaultFolders.olFolderInbox);
var items = inbox.Items.Restrict(" ""http://schemas.microsoft.com/mapi/proptag/0x30130102"" = '22E6722D04D84704B28B060C1CA5AB92'");