locked
[E2010] [EWS Java API 1.1][JAVA] - Getting contacts using filter. RRS feed

  • Question

  • Hi,

    I am trying to get the contacts using the filter using EWS Java API. I am using the following code to get the contacts.

    		ExchangeService service = new ExchangeService();
    		URI url = new URI("https://10.192.17.124/ews/Exchange.asmx");
    		service.setUrl(url);
    		ExchangeCredentials credentials = new WebCredentials("administrator",
    				"P@$$w0rd");
    		service.setCredentials(credentials);
    		Collection<Item> items = new ArrayList<Item>();
    		Contact contact = new Contact(service);
    		contact.setGivenName("GivenName");
    		contact.setSurname("Nibin");
    		items.add(contact);
    		FolderId folderId = new FolderId(WellKnownFolderName.Contacts);
    		ServiceResponseCollection<ServiceResponse> responses = service
    				.createItems(items, folderId, MessageDisposition.SaveOnly,
    						SendInvitationsMode.SendToNone);
    		ServiceResponse response = responses.getResponseAtIndex(0);
    		System.out.println(response.getClass());
    		Field field = response.getClass().getDeclaredField("item");
    		field.setAccessible(true);
    		Object object = field.get(response);
    		String uniqueId = ((Item) object).getId().getUniqueId();
    		System.out.println(uniqueId);
    		SearchFilter.IsEqualTo filter = new SearchFilter.IsEqualTo();
    		filter.setPropertyDefinition(ContactSchema.Id);
    		filter.setValue(uniqueId);
    		FindItemsResults<Item> findItems2 = service.findItems(
    				WellKnownFolderName.Contacts, filter, new ItemView(5));
    		for (Item item : findItems2) {
    			Contact contact1 = (Contact) item;
    			System.out.println(contact1);
    		}

    But it is throwing exception from the server.

    The exception I am getting is:

    Exception in thread "main" microsoft.exchange.webservices.data.ServiceResponseException: The specified value is invalid for property.
        at microsoft.exchange.webservices.data.ServiceResponse.internalThrowIfNecessary(ServiceResponse.java:264)
        at microsoft.exchange.webservices.data.ServiceResponse.throwIfNecessary(ServiceResponse.java:252)
        at microsoft.exchange.webservices.data.MultiResponseServiceRequest.execute(MultiResponseServiceRequest.java:150)
        at microsoft.exchange.webservices.data.ExchangeService.findItems(ExchangeService.java:949)
        at microsoft.exchange.webservices.data.ExchangeService.findItems(ExchangeService.java:1002)
        at microsoft.exchange.webservices.data.ExchangeService.findItems(ExchangeService.java:1074)
        at ContactTest.main(ContactTest.java:52)

    The value passed in the filter for item id was a string value. For item id filtering what value should I pass?

    Instead of directly, passing the string I tried passing the Item id object itself.

    SearchFilter.IsEqualTo filter = new SearchFilter.IsEqualTo();
    filter.setPropertyDefinition(ContactSchema.Id);
    filter.setValue(new ItemId(uniqueId));

    Then I am getting the following exception.

    microsoft.exchange.webservices.data.ServiceXmlSerializationException: Values of type 'microsoft.exchange.webservices.data.ItemId' can't be used for the 'Value' attribute.
        at microsoft.exchange.webservices.data.EwsServiceXmlWriter.writeAttributeValue(EwsServiceXmlWriter.java:196)
        at microsoft.exchange.webservices.data.SearchFilter$RelationalFilter.writeElementsToXml(SearchFilter.java:1202)
        at microsoft.exchange.webservices.data.ComplexProperty.writeToXml(ComplexProperty.java:230)
        at microsoft.exchange.webservices.data.ComplexProperty.writeToXml(ComplexProperty.java:246)
        at microsoft.exchange.webservices.data.SearchFilter.writeToXml(SearchFilter.java:113)
        at microsoft.exchange.webservices.data.FindRequest.writeElementsToXml(FindRequest.java:128)
        at microsoft.exchange.webservices.data.ServiceRequestBase.writeBodyToXml(ServiceRequestBase.java:136)
        at microsoft.exchange.webservices.data.ServiceRequestBase.writeToXml(ServiceRequestBase.java:257)
        at microsoft.exchange.webservices.data.ServiceRequestBase.emit(ServiceRequestBase.java:313)
        at microsoft.exchange.webservices.data.ServiceRequestBase.validateAndEmitRequest(ServiceRequestBase.java:705)
        at microsoft.exchange.webservices.data.SimpleServiceRequestBase.internalExecute(SimpleServiceRequestBase.java:36)
        at microsoft.exchange.webservices.data.MultiResponseServiceRequest.execute(MultiResponseServiceRequest.java:140)
        at microsoft.exchange.webservices.data.ExchangeService.findItems(ExchangeService.java:949)
        at microsoft.exchange.webservices.data.ExchangeService.findItems(ExchangeService.java:1002)
        at microsoft.exchange.webservices.data.ExchangeService.findItems(ExchangeService.java:1074)
        at ContactTest.main(ContactTest.java:51)

    i.e., the item id cannot be serialized to an XML.

    What value should I pass for itemId for the filtering?

    When I debugged, the only valid types that can be set are:

    • Enum
    • Boolean
    • Date
    • Primitive types
    • String
    • Integer
    • A class implementing ISearchStringProvider

    Thanks,

    Paul

     

     

    Monday, January 10, 2011 1:02 PM

Answers

  • The ItemID is not a search-able property if you have the UniqueID of the contact its not necessary to search for it anyway you can just bind directly to the object eg just use Contact.Bind(Service,new ItemId(uniqueId))

    The thing to keep in mind is the ItemId is not a static property and there are number of things that will cause the value of this property to change for an item so it not a good choice for example to store in a external database as a means of referencing an Item but for short term use within the same session its fine.

    Cheers
    Glen

    • Marked as answer by PaulNibin Monday, March 7, 2011 12:14 PM
    Tuesday, January 11, 2011 7:02 AM