I need a solution for, quickly, finding an e-mail item in outlook.
Yes, I could iterate through all items, but there will potentially be thousands of mails in shared folders, and the Find and Restrict functions at least gives the illusion of maybe using an indexed approach.
I have decided to use the property PR_SEARCH_KEY, because the definition suits the purpose, and also because the field is available on Drop.
But somehow the Find and Restrict functions on Items fails to find my mail. Which is weird, because when iterating items in the collection, I can get the value from each item, and of course find the matching item.
What I have tried:
I have constructed a query like this:
@SQL= "http://schemas.microsoft.com/mapi/proptag/0x300B0102" = 'C2C5634D92A67A4980B9D8D138DD8593'
Find() on the Items in the inbox folder returns null, and Restrict() returns a list of items with a length of 0.
So, obviously I am doing something wrong with my query.
This is the complete code to reproduce (in X++, sorry).
Microsoft.Office.Interop.Outlook.ApplicationClass app;
Microsoft.Office.Interop.Outlook.MailItem mailItem;
Microsoft.Office.Interop.Outlook.NameSpace nameSpace;
Microsoft.Office.Interop.Outlook.MAPIFolder mapiFolder;
Microsoft.Office.Interop.Outlook.Items items;
int itemCount;
int itemIdx;
Microsoft.Office.Interop.Outlook.MailItem item;
Microsoft.Office.Interop.Outlook.PropertyAccessor accessor;
str searchKey;
str subject;
CLRObject property;
CLRObject CLRexception;
str errorString;
str PR_SEARCH_KEY = "http://schemas.microsoft.com/mapi/proptag/0x300B0102";
str skey = "C2C5634D92A67A4980B9D8D138DD8593";
str filter = strFmt('@SQL= "%1" = \'%2\'', PR_SEARCH_KEY, skey);
info(filter);
try
{
app = new Microsoft.Office.Interop.Outlook.ApplicationClass();
nameSpace = app.GetNamespace("MAPI");
mapiFolder = nameSpace.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders::olFolderInbox);
items = mapiFolder.get_Items();
item = items.Find(filter);
if (CLRInterop::isNull(item) == false)
{
subject = item.get_Subject();
info("Found " + subject);
}
else
{
Error("Not found");
}
itemCount = items.get_Count();
for (itemIdx = 1; itemIdx <= itemCount; itemIdx++)
{
item = items.get_Item(itemIdx);
subject = item.get_Subject();
info(subject);
accessor = item.get_PropertyAccessor();
property = accessor.GetProperty(PR_SEARCH_KEY);
if (CLRInterop::isNull(property) == false)
{
searchKey = accessor.BinaryToString(property);
info(searchKey);
}
}
}
catch( Exception::CLRError )
{
CLRexception = CLRInterop::getLastException();
while( CLRexception )
{
errorString += CLRInterop::getAnyTypeForObject( CLRexception.get_Message() );
CLRexception = CLRexception.get_InnerException();
}
throw error(errorString);
}
Which will produce this output:
@SQL= "http://schemas.microsoft.com/mapi/proptag/0x300B0102" =
'C2C5634D92A67A4980B9D8D138DD8593'
Not found
test email 05
9E92D4BCB66C7449A3958F8D2C96626A
Infomail: GOM meeting minutes incl. Monitor
C2C5634D92A67A4980B9D8D138DD8593