Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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 )
    {
        //BP Deviation documented
        CLRexception = CLRInterop::getLastException();
        while( CLRexception )
        {
            //BP Deviation documented
            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
Posted
Updated 14-Sep-21 22:43pm
Comments
Member 11673967 14-Sep-21 9:18am    
Hi Jan,

Did you manage to solve this? I have the same issue.

Thank you.
Kind regards
Philippe

1 solution

Hi Philippe

No, unfortunately not. We ended up creating a ridiculous amount of code that searches every folder using the query created by this method:
public str createSearchString(str _messageId)
{
    str                                                 quoted;
    str                                                 searchStr;

    quoted      = "'" + _messageId +  "'";
    searchStr   = '@SQL="http://schemas.microsoft.com/mapi/proptag/0x1035001F"=' + quoted;

    return searchStr;
}

And because the search is so slow, we have implemented the code to search most likely folders first, and when found, we save the path as the most like place to find the mail next time.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900