Search Based on Custom Property

Add-in Express™ Support Service
That's what is more important than anything else

Search Based on Custom Property
 
theangrycoder




Posts: 26
Joined: 2006-09-25
Hi,

I am adding some custom properties in my Outlook mail items. I want to know if there is a way to find mail items having that custom property?

thanks,
Sameers
Posted 14 Mar, 2009 07:54:01 Top
Sergey Grischenko


Add-in Express team


Posts: 7202
Joined: 2004-07-05
Hi Sameers.

Please look at the following reference:
http://msdn.microsoft.com/en-us/library/bb220350.aspx
Posted 16 Mar, 2009 11:38:25 Top
theangrycoder




Posts: 26
Joined: 2006-09-25
Sergey,


I had seen that example before, but the problem with that example is that it is restricted to one folder only. Is there anyway to initiate global search (in all folders including inbox, sent items, any other user defined folders either under inbox or any other)?

thanks,
Sameers
Posted 16 Mar, 2009 15:59:52 Top
Dmitry Kostochko


Add-in Express team


Posts: 2740
Joined: 2004-04-05
Hello Sameers,

You can try to use the AdvancedSearch method of the Application object.


Posted 17 Mar, 2009 11:19:16 Top
Dmitry Kostochko


Add-in Express team


Posts: 2740
Joined: 2004-04-05
Hello Sameers,

Sorry, I forgot to mention that the AdvancedSearch method is introduced in the Outlook 2002 Object Model.

Posted 17 Mar, 2009 11:22:49 Top
theangrycoder




Posts: 26
Joined: 2006-09-25
I already had tried the AdvancedSearch folder, but I was not sure how to specify the CustomProperty field in search criteria. maybe, if I can get a sample, that will help.

there is one weird thing with Items.Find method, it does find the first match, but when I try FindNext, it always returns NULL (Nothing in VB). Of course, I am not changing any criteria, code is simple.

dim item as object = items.Find
If Item is Not Nothing
Item = Items.FindNext()
End If

But FindNext is always NULL (Nothing).

Any idea?
thanks,
Sameers
Posted 18 Mar, 2009 11:30:30 Top
Eugene Astafiev


Add-in Express team


Posts: 8540
Joined: 2007-07-25
Hi Sameers,

Please note that you write your messages to the Delphi forum. I think a better place for your posts is the .Net forum.

there is one weird thing with Items.Find method, it does find the first match, but when I try FindNext, it always returns NULL (Nothing in VB). Of course, I am not changing any criteria, code is simple.


Did you try to use the GetFirst function instead?
.NET, Extended MAPI, Windows Phone & RT
Posted 18 Mar, 2009 12:06:41 Top
theangrycoder




Posts: 26
Joined: 2006-09-25
Hi,

For those, who may need to do the same (search based on custom properties), here is the example using both Items.Find and AdvancedSearch.


 Private Sub SearchItemsIndividualFolder()
        Dim oInBox As MAPIFolder = Mapi.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox)
        Dim query As String = "@SQL=" & Chr(34) & "http://schemas.microsoft.com/mapi/string/{00020329-0000-0000-C000-000000000046}/CustomPropertyName"  & Chr(34) & " = FALSE"
        Dim oItems As Items = oInBox.Items
        Dim item As Object = oItems.Find(query)
        Dim totalFound As Integer = 0
        Dim subjects As String = ""
        While item IsNot Nothing
            totalFound += 1
            subjects +=CType(item, MailItem).Subject & vbCrLf
            item = oItems.FindNext
        End While
        MsgBox("total Found: " & totalFound & vbCrLf & subjects)
    End Sub



And here is the Advanced Search method



Private Sub SearchUsingAdvancedSearch()
        Try
            _m_SearchComplete = False
            Dim scope As String = "'" & OutlookApp.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox).FolderPath & "','" & OutlookApp.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderSentMail).FolderPath & "'"
            Dim query As String = Chr(34) & "http://schemas.microsoft.com/mapi/string/{00020329-0000-0000-C000-000000000046}/CustomPrppertyName" & Chr(34) & " = FALSE"
            Dim app As _Application = OutlookApp

            Dim result As Search = app.AdvancedSearch(scope, query, True)

            While Not _m_SearchComplete
                System.Windows.Forms.Application.DoEvents()
            End While
            'Dim outMsg As String = ""
            If result IsNot Nothing Then
                'MsgBox(result.Results.Count)
                For temp As Integer = 1 To result.Results.Count
                    If TypeOf result.Results.Item(temp) Is MailItem Then
                        outMsg +=CType(result.Results.Item(temp), MailItem).Subject & vbCrLf
                    End If
                Next
                MsgBox(outMsg)
            End If
        Catch ex As System.Exception
            MsgBox("Failed in search: " & ex.Message)
        End Try
    End Sub

Private Sub adxOutlookEvents_AdvancedSearchComplete(ByVal sender As Object, ByVal hostObj As Object) Handles adxOutlookEvents.AdvancedSearchComplete
        _m_SearchComplete = True
    End Sub
Private _m_SearchComplete As Boolean = False


Hope that may help someone else here.

Sameers
Posted 24 Mar, 2009 12:51:47 Top
Dmitry Kostochko


Add-in Express team


Posts: 2740
Joined: 2004-04-05
Hello Sameers,

Thanks a lot for publishing your code.

Posted 24 Mar, 2009 15:24:55 Top