locked
EWS Search by Sender Name (Not email) RRS feed

  • Question

  • Guys,

    I've been trying to do a search in sender name and have not been succesful. In the search condition if I use the following as the FieldURI it searches in email;

    searchField.FieldURI =

    UnindexedFieldURIType.messageSender;

    or

    searchField.FieldURI =

    UnindexedFieldURIType.messageFrom;

    and after going through the UnindexedFieldURIType list for several times, I still can't figure out what is the appropritate field that I should use to search by sender name.

    The same issue is applicable to recipient name search as well. Could you tell me how to do a search by sender or recipient name in EWS?

     

    Thanks

    Duke

    Thursday, April 29, 2010 2:36 PM

Answers

  • I would suggest you use the PR_Sender_Name Extended property http://msdn.microsoft.com/en-us/library/bb446050.aspx (equivilent to the urn:schemas:httpmail:fromname) instead eg

          ItemView iv = new ItemView(2000);
          ExtendedPropertyDefinition PR_SENDER_NAME = new ExtendedPropertyDefinition(3098, MapiPropertyType.String);
          SearchFilter sf = new SearchFilter.ContainsSubstring(PR_SENDER_NAME, "Text to search");
          FindItemsResults<Item> fiItems = service.FindItems(WellKnownFolderName.Inbox, sf, iv);
          foreach (Item itItem in fiItems.Items) {
            Console.WriteLine(itItem.Subject);
          
          }

    Cheers
    Glen

    • Marked as answer by Duke2 Friday, April 30, 2010 8:24 AM
    Friday, April 30, 2010 5:32 AM
  • Try something like

       FindItemType fiFindItemRequest = new FindItemType();
       fiFindItemRequest.Traversal = ItemQueryTraversalType.Shallow;
       ItemResponseShapeType ipItemProperties = new ItemResponseShapeType();
       ipItemProperties.BaseShape = DefaultShapeNamesType.IdOnly;
       fiFindItemRequest.ItemShape = ipItemProperties;
    
    
       DistinguishedFolderIdType[] faFolderIDArray = new DistinguishedFolderIdType[1];
       faFolderIDArray[0] = new DistinguishedFolderIdType();
       faFolderIDArray[0].Id = pfParentFolder.Id;
       fiFindItemRequest.ParentFolderIds = faFolderIDArray;
       RestrictionType ffRestriction = new RestrictionType();
    
       PathToExtendedFieldType PR_SenderName = new PathToExtendedFieldType();
       PR_SenderName.PropertyTag = "0x0C1A";
       PR_SenderName.PropertyType = MapiPropertyTypeType.String;
    
       ConstantValueType cnValue = new ConstantValueType();
       cnValue.Value = "Text to Search";
       ContainsExpressionType ceType = new ContainsExpressionType();
       ceType.Item = PR_SenderName;
       ceType.ContainmentComparison = ContainmentComparisonType.IgnoreCase;
       ceType.Constant = cnValue;
    
       ffRestriction.Item = ceType;
       fiFindItemRequest.Restriction = ffRestriction;

    Cheers

    Glen

    • Marked as answer by Duke2 Monday, May 3, 2010 11:50 PM
    Monday, May 3, 2010 11:28 PM

All replies

  • I would suggest you use the PR_Sender_Name Extended property http://msdn.microsoft.com/en-us/library/bb446050.aspx (equivilent to the urn:schemas:httpmail:fromname) instead eg

          ItemView iv = new ItemView(2000);
          ExtendedPropertyDefinition PR_SENDER_NAME = new ExtendedPropertyDefinition(3098, MapiPropertyType.String);
          SearchFilter sf = new SearchFilter.ContainsSubstring(PR_SENDER_NAME, "Text to search");
          FindItemsResults<Item> fiItems = service.FindItems(WellKnownFolderName.Inbox, sf, iv);
          foreach (Item itItem in fiItems.Items) {
            Console.WriteLine(itItem.Subject);
          
          }

    Cheers
    Glen

    • Marked as answer by Duke2 Friday, April 30, 2010 8:24 AM
    Friday, April 30, 2010 5:32 AM
  • Thanks a lot Glen,

    Could you kindly tell me how to do this in classic EWS if it's not too much trouble for you? I tried to map the values in MAPI property to an ExtendedProeprtyType but always get an error saying

    The request failed schema validation: The 'PropertyTag' attribute is invalid - The value '0x0C1A001F' is invalid according to its datatype 'http://schemas.microsoft.com/exchange/services/2006/types:PropertyTagType' - The value '0x0C1A001F' is not valid according to any of the memberTypes of the union.

    Following is how I created the RestrictionType in my program

     

     

    PathToExtendedFieldType extProp = new PathToExtendedFieldType();

     

    ContainsExpressionType containsExpr = new ContainsExpressionType();

    extProp.PropertyType =

    MapiPropertyTypeType.String;

    extProp.PropertyTag =

    "0x0C1A001F";

    containsExpr.Constant =

    new ConstantValueType();

    containsExpr.Constant.Value = sFieldValue;

    containsExpr.ContainmentComparison =

    ContainmentComparisonType.IgnoreCase;

    containsExpr.ContainmentComparisonSpecified =

    true;

    containsExpr.ContainmentMode =

    ContainmentModeType.Substring;

    containsExpr.ContainmentModeSpecified =

    true;

    containsExpr.Item = extProp;

     

    Thanks

    Duke

    Friday, April 30, 2010 8:22 AM
  •  

    Glen,

     

    I played with the extended property setting a bit more but still couldn't get it to work.

    Your help is much appreciated.

     

    Thanks

    Duke

    Monday, May 3, 2010 2:22 PM
  • Try something like

       FindItemType fiFindItemRequest = new FindItemType();
       fiFindItemRequest.Traversal = ItemQueryTraversalType.Shallow;
       ItemResponseShapeType ipItemProperties = new ItemResponseShapeType();
       ipItemProperties.BaseShape = DefaultShapeNamesType.IdOnly;
       fiFindItemRequest.ItemShape = ipItemProperties;
    
    
       DistinguishedFolderIdType[] faFolderIDArray = new DistinguishedFolderIdType[1];
       faFolderIDArray[0] = new DistinguishedFolderIdType();
       faFolderIDArray[0].Id = pfParentFolder.Id;
       fiFindItemRequest.ParentFolderIds = faFolderIDArray;
       RestrictionType ffRestriction = new RestrictionType();
    
       PathToExtendedFieldType PR_SenderName = new PathToExtendedFieldType();
       PR_SenderName.PropertyTag = "0x0C1A";
       PR_SenderName.PropertyType = MapiPropertyTypeType.String;
    
       ConstantValueType cnValue = new ConstantValueType();
       cnValue.Value = "Text to Search";
       ContainsExpressionType ceType = new ContainsExpressionType();
       ceType.Item = PR_SenderName;
       ceType.ContainmentComparison = ContainmentComparisonType.IgnoreCase;
       ceType.Constant = cnValue;
    
       ffRestriction.Item = ceType;
       fiFindItemRequest.Restriction = ffRestriction;

    Cheers

    Glen

    • Marked as answer by Duke2 Monday, May 3, 2010 11:50 PM
    Monday, May 3, 2010 11:28 PM
  • Thank you so much Glen, your post solved the problem. I now can search by sender name.

     

    I was messing with the wrong tag value all this time and feel really dumb about it.

     

    My next task is to find the right value for recipient name which I believe shouldn't be that hard.

     

    Thanks again for your help.

     

    Duke

    Monday, May 3, 2010 11:54 PM
  • I have a similar problem in Exchange WS 2007

    i was able to create a restriction filter for one simple field (subject,  message conversation index, etc)

    But i was not able to create a filter for message sender.Like is visible in the example, the structure for sender is more complex

    In findItem response (without restriction) i have:

    <t:Message>

    <t:ConversationIndex>AcrmIrjbApWD0QsnRTCVXRPbmHTD7g==</t:ConversationIndex>

     

    <t:From>
                                        <t:Mailbox>
                                            <t:Name>someOne@company.com</t:Name>
                                        </t:Mailbox>
                                    </t:From>

    </t:Message>

     

    Can you help me in this direction?

    Thank you

    Tuesday, May 11, 2010 3:22 PM
  • Sorry i dont understand your question are you trying to use an And Serach filter ? if so try posting the code your trying to use that isn't working (raw xml doesn't really help).

    Cheers
    Glen

     

    Wednesday, May 12, 2010 2:27 AM
  • Sorry for inconvenience. It's about a simple restriction.

    This is the code:

            RestrictionType restriction = new RestrictionType();
            IsEqualToType isEqualToType = new IsEqualToType();

            FieldURIOrConstantType fieldURI = new FieldURIOrConstantType();
            ConstantValueType constant = new ConstantValueType();
            constant.setValue("someOne@company.com");
            fieldURI.setConstant(constant);

            isEqualToType.setFieldURIOrConstant(fieldURI);

            PathToUnindexedFieldType pathToUnindexedField = new PathToUnindexedFieldType();
            pathToUnindexedField.setFieldURI(UnindexedFieldURIType.MESSAGE_SENDER);
            JAXBElement<? extends BasePathToElementType> fieldURIExpression = objectFactory.createFieldURI(pathToUnindexedField);
            isEqualToType.setPath(fieldURIExpression);
            JAXBElement<IsEqualToType> isEqualToTypeExpression = objectFactory.createIsEqualTo(isEqualToType);
            restriction.setSearchExpression(isEqualToTypeExpression);

            request.setRestriction(restriction);

    Wednesday, May 12, 2010 7:04 AM
  • I would say the problem is your trying to do an exact match rather then a substring with that property it will be a combination of both display and email address so i would try

       RestrictionType ffRestriction = new RestrictionType();
    
    
          PathToUnindexedFieldType snSender = new PathToUnindexedFieldType();
          snSender.FieldURI = UnindexedFieldURIType.messageSender;
    
    
    
          ConstantValueType cnValue = new ConstantValueType();
          cnValue.Value = "Email addresss";
          ContainsExpressionType ceType = new ContainsExpressionType();
          ceType.ContainmentMode = ContainmentModeType.Substring;
          ceType.ContainmentModeSpecified = true;
          ceType.Item = snSender;
          ceType.ContainmentComparisonSpecified = true;
          ceType.ContainmentComparison = ContainmentComparisonType.IgnoreCase;
          ceType.Constant = cnValue;
    

    Cheers
    Glen

    Thursday, May 13, 2010 1:02 AM
  • i follow your suggestion, and the code begun:

            RestrictionType restriction = new RestrictionType();
            ContainsExpressionType containsExpression = new ContainsExpressionType();

            FieldURIOrConstantType fieldURI = new FieldURIOrConstantType();
            ConstantValueType constant = new ConstantValueType();
            constant.setValue("someOne@company.com");
            fieldURI.setConstant(constant);

            containsExpression.setConstant(constant);
            containsExpression.setContainmentComparison(ContainmentComparisonType.IGNORE_CASE);
            containsExpression.setContainmentMode(ContainmentModeType.SUBSTRING);

            PathToUnindexedFieldType pathToUnindexedField = new PathToUnindexedFieldType();
            pathToUnindexedField.setFieldURI(UnindexedFieldURIType.MESSAGE_SENDER);
            JAXBElement<? extends BasePathToElementType> fieldURIExpression = objectFactory.createFieldURI(pathToUnindexedField);
            containsExpression.setPath(fieldURIExpression);
            JAXBElement<ContainsExpressionType> containsExpressionType = objectFactory.createContains(containsExpression);
            restriction.setSearchExpression(containsExpressionType);

            fiFindItemRequest.setRestriction(restriction);

     

    And a very weird thing happent:

    if i use as value someOne@company.com , there is no message returned

    If the value is changed to :

    someOne@company.co , omeOne@company.com , someOne , etc,

    the results are acurate.

    So, there is a implementation bug in exchange or is something wrong with my code?

     

    Thanks again for your prompt answeer.

     

    Thursday, May 13, 2010 8:42 AM
  • It looks like your using some sort of Java proxy so its hard to tell whats happening you could try this using a Windows machine in Visual Studio then compare what you get from your Java proxy to what you get on windows. The other thing to try maybe to use UnindexedFieldURIType.messageFrom

    Cheers
    Glen

    Friday, May 14, 2010 6:06 AM
  • Saeme problem with UnindexedFieldURIType.MESSAGE_FROM.

     

    I am constraint to use java, because the application will run in non-windows environment into an application write in java

    I will try to test into a Windows environment using .net technology to see if the bug persist.

     

    Thanks for your support

     

    Tuesday, May 18, 2010 1:24 PM