none
Outlook 2007 - Items.Restrict (How to filter contacts based on HasPicture?) RRS feed

  • Question

  • Hi,

    I am currently using a Restrict filter, to limit the items in the ContactsItem Folder. Once I get the filtered items I loop through them and check if they HasPicture. This feels very inefficient.

    Microsoft.Office.Interop.Outlook.Items allItems = Application.GetNamespace("MAPI").GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts).Items;
    var sfilter = "@SQL=" + "\"" + "urn:schemas:contacts:customerid" + "\"" + " ci_startswith 'ADDIN-ID'";
    var contactItems = allItems.Restrict(sfilter);

    Can someone give me a AQS or DASL query example where I can filter and return only contacts where HasPicture = False

    Wednesday, April 29, 2015 9:52 AM

Answers

All replies

  • Hello,

    Try to use the following one:

    sFilter = "[HasPicture] = True" 

    Also I'd suggest breaking the chain of calls in the following line of code:

     Application.GetNamespace("MAPI").GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts).Items

    Just declare a property or method call on a separate line of code. It allows to release all underlying COM objects instantly. Use System.Runtime.InteropServices.Marshal.ReleaseComObject to release an Outlook object when you have finished using it.  Then set a variable to Nothing in Visual Basic (null in C#) to release the reference to the object. Read more about that in the Systematically Releasing Objects article.

    Wednesday, April 29, 2015 12:10 PM
  • Hi Eugene,

    That didn't work, I tried the following filter and got an exception:

    sFilter = "[HasPicture] = False" 

    I get the exception "Condition is not valid."

    Wednesday, April 29, 2015 1:37 PM
  • Do you have only contact items in the folder? Any distribution lists?

    The fact is that only ContactItem class has the HasPicture property. You may try to filter items on the message class first.


    Wednesday, April 29, 2015 2:36 PM
  • I only have ContactItems in the Contact Folder. Using that filter via the Restrict method causes the exception.

    If it can't be done then thats fine, but I will have to loop through each of the ContactItems and check if HasPicture == False, then apply a picture and save the item that way?

    Wednesday, April 29, 2015 3:20 PM
  • It seems you need to iterate over contacts to find ones that have the HasPicture property set to false or true. Unfortunately I couldn't find any explanation why it is not possible to use the HasPicture property in the Restrict method.

    BTW Did you try to use the AdvancedSearch method of the Application class? Does it work in that case?

    Wednesday, April 29, 2015 3:43 PM
  • I not familar with the AdvancedSearch. But I have decided to stick with the loop model and check the HasPicture property for each Contact Item.

    However I am experiencing an Outlook UI freeze when this loop starts and during. I have a Thread.Sleep(500) after each iteration of each Contact Item. Is there anything else I can do to improve performance?

    Approx I have 300 Contact items I have to look through, check HasPicture and optionally AddPicture.


    Wednesday, April 29, 2015 4:21 PM
  • Try to use the GetTable method of the Folder class.
    Wednesday, April 29, 2015 4:53 PM
  • You need to specify the following DALS property name, not the OOM name (HasPicture) in your query:

    sfilter = "@SQL=\"http://schemas.microsoft.com/mapi/id/{00062004-0000-0000-C000-000000000046}/8015000B\" = 'true'  ";


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Wednesday, April 29, 2015 5:11 PM
  • Hi Dmitry 

    That didnt work, I used the filter you suggested as follows, just changing true to false.

    var sfilter = "@SQL=\"http://schemas.microsoft.com/mapi/id/{00062004-0000-0000-C000-000000000046}/8015000B\" = 'false'";
    var contactItems = allItems.Restrict(sfilter);

    No contact items are returned from the restrcit method.

    Thursday, April 30, 2015 1:22 PM
  • In DASL syntax, you must convert True/False to an integer value, where 0 represents False and 1 represents True:

    var sfilter = "@SQL=\"http://schemas.microsoft.com/mapi/id/{00062004-0000-0000-C000-000000000046}/8015000B\" = 0";

    Thursday, April 30, 2015 1:28 PM
  • Sorry the filter I am using is:

    var sfilter = "@SQL=\"http://schemas.microsoft.com/mapi/id/{00062004-0000-0000-C000-000000000046}/8015000B\" = 'false'";

    Please ignore the previous post.

    Thanks

    Thursday, April 30, 2015 1:32 PM
  • Does it work?

    If no, try to replace the true/false values in the filter string with 1 or 0 as listed in my previous post. In DASL syntax, you must convert True/False to an integer value, where 0 represents False and 1 represents True

    Thursday, April 30, 2015 2:04 PM
  • Replacing False with 0 doesn't make any difference.
    Thursday, April 30, 2015 2:35 PM
  • If you want contacts *without* pictures, change the query to

    @SQL=(\"http://schemas.microsoft.com/mapi/id/{00062004-0000-0000-C000-000000000046}/8015000B\" = 'false') OR (\"http://schemas.microsoft.com/mapi/id/{00062004-0000-0000-C000-000000000046}/8015000B\" IS null)


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    • Marked as answer by soledev Thursday, April 30, 2015 3:31 PM
    Thursday, April 30, 2015 2:43 PM