none
[E2010] [EWS Java API 1.1][JAVA] - Could not update the phone numbers of a contact. RRS feed

  • Question

  • Hi,

    I am trying to modify a contact by updating its phone numbers. Please check the below code for the steps.

        ExchangeService service = TestUtils.getExchangeService();
    
        // Deleting all the contacts
        Folder contactsFolder = Folder.bind( service, WellKnownFolderName.Contacts );
        contactsFolder.empty( DeleteMode.HardDelete, false );
        System.out.println( "Deleted all Contacts" );
    
        // creating a new contact.
        Collection < Item > items = new ArrayList < Item >();
        Contact ct = new Contact( service );
        ct.setGivenName( "Paul" );
        ct.setSurname( "Nibin" );
        ct.getPhoneNumbers().setPhoneNumber( PhoneNumberKey.BusinessPhone, "04865231009" );
        items.add( ct );
        ServiceResponseCollection < ServiceResponse > results = service.createItems( items, new FolderId(
            WellKnownFolderName.Contacts ), MessageDisposition.SaveOnly, SendInvitationsMode.SendToNone );
        System.out.println( "Created the contact." );
    
        // Getting the itemId of the created contact, so it could be used for
        // modifying the contact.
        Field item = results.getResponseAtIndex( 0 ).getClass().getDeclaredField( "item" );
        item.setAccessible( true );
        Contact createdContactObject = ( Contact ) item.get( results.getResponseAtIndex( 0 ) );
        System.out.println( "Id of the cread contact : " + createdContactObject.getId().getUniqueId() );
    
        System.out.println( "Modifying the contact." );
        Contact contact = Contact.bind( service, new ItemId( createdContactObject.getId().getUniqueId() ) );
        contact.getPhoneNumbers().setPhoneNumber( PhoneNumberKey.BusinessPhone, "04841131009" );
        contact.update( ConflictResolutionMode.AutoResolve );
        System.out.println( "Contact modified successfully." );
    
        contact = Contact.bind( service, new ItemId( createdContactObject.getId().getUniqueId() ) );
        System.out.println( "Changed new Home Phone : "
            + contact.getPhoneNumbers().getPhoneNumber( PhoneNumberKey.BusinessPhone ) );

    Here the contact.update( ConflictResolutionMode.AutoResolve) throws ServiceRequestException.

    Exception in thread "main" microsoft.exchange.webservices.data.ServiceRequestException: The request failed. Connection Error java.io.IOException: Server returned HTTP response code: 500 for URL: http://10.192.37.30/ews/Exchange.asmx
    	at microsoft.exchange.webservices.data.SimpleServiceRequestBase.internalExecute(SimpleServiceRequestBase.java:52)
    	at microsoft.exchange.webservices.data.MultiResponseServiceRequest.execute(MultiResponseServiceRequest.java:140)
    	at microsoft.exchange.webservices.data.ExchangeService.internalUpdateItems(ExchangeService.java:576)
    	at microsoft.exchange.webservices.data.ExchangeService.updateItem(ExchangeService.java:660)
    	at microsoft.exchange.webservices.data.Item.internalUpdate(Item.java:254)
    	at microsoft.exchange.webservices.data.Item.update(Item.java:387)
    	at test.ContactFilterTest.main(ContactFilterTest.java:54)
    Caused by: microsoft.exchange.webservices.data.EWSHttpException: Connection Error java.io.IOException: Server returned HTTP response code: 500 for URL: http://10.192.37.30/ews/Exchange.asmx
    	at microsoft.exchange.webservices.data.HttpNativeWebRequest.getInputStream(HttpNativeWebRequest.java:145)
    	at microsoft.exchange.webservices.data.ServiceRequestBase.getResponseStream(ServiceRequestBase.java:351)
    	at microsoft.exchange.webservices.data.SimpleServiceRequestBase.readResponse(SimpleServiceRequestBase.java:98)
    	at microsoft.exchange.webservices.data.SimpleServiceRequestBase.internalExecute(SimpleServiceRequestBase.java:39)
    	... 6 more
    

    I tried with HomePhone, BusinessPhone, AssistantPhone etc. In all these cases I was getting the exception only.

    But if I am updating 2 properties GivenName and BusinessPhone as shown below, it does not throw any error. But the BusinessPhone is not modified. Only Given name is modified.

    I think the problem is even if I modify the BusinessPhone in the contact, the update call message sent to the server is not including this update. The soap message sent are:

    Only BusinessPhone updated.
    
    <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:m="http://schemas.microsoft.com/exchange/services/2006/messages" xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types">
    	<soap:Header>
    		<t:RequestServerVersion Version="Exchange2010_SP1"></t:RequestServerVersion>
    	</soap:Header>
    	<soap:Body>
    		<m:UpdateItem MessageDisposition="SaveOnly" ConflictResolution="AutoResolve">
    			<m:ItemChanges>
    				<t:ItemChange>
    					<t:ItemId Id="AQMkADRjYTg2ZDFhLWFiNmItNDZjNC1iOGI1LTE2ZDdiY2I3YjVlNABGAAADYMPsItRsWUSfH+02NDGqDgcA1DZqiDxpaEmrqwAwbjVaMxoAAAGNFQAAAY6b1DJIS9lPvMAR8jGczG4AAAJAOQAAAA==" ChangeKey="EQAAABYAAACOm9QySEvZT7zAEfIxnMxuAAAAAH8U"></t:ItemId>
    					<t:Updates></t:Updates>
    				</t:ItemChange>
    			</m:ItemChanges>
    		</m:UpdateItem>
    	</soap:Body>
    </soap:Envelope>

    Both BusinessPhone and Given Name updated.
    
    <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:m="http://schemas.microsoft.com/exchange/services/2006/messages" xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types">
    	<soap:Header>
    		<t:RequestServerVersion Version="Exchange2010_SP1"></t:RequestServerVersion>
    	</soap:Header>
    	<soap:Body>
    		<m:UpdateItem MessageDisposition="SaveOnly" ConflictResolution="AutoResolve">
    			<m:ItemChanges>
    				<t:ItemChange>
    					<t:ItemId Id="AQMkADRjYTg2ZDFhLWFiNmItNDZjNC1iOGI1LTE2ZDdiY2I3YjVlNABGAAADYMPsItRsWUSfH+02NDGqDgcA1DZqiDxpaEmrqwAwbjVaMxoAAAGNFQAAAY6b1DJIS9lPvMAR8jGczG4AAAJAOAAAAA==" ChangeKey="EQAAABYAAACOm9QySEvZT7zAEfIxnMxuAAAAAH7n"></t:ItemId>
    					<t:Updates>
    						<t:SetItemField>
    							<t:FieldURI FieldURI="contacts:GivenName"></t:FieldURI>
    							<t:Contact>
    								<t:GivenName>PaulN</t:GivenName>
    							</t:Contact>
    						</t:SetItemField>
    					</t:Updates>
    				</t:ItemChange>
    			</m:ItemChanges>
    		</m:UpdateItem>
    	</soap:Body>
    </soap:Envelope>
    In both the cases, the BusinessPhone is not going in the SOAP message, I think the error is coming when there are no SetItemFields in the Updates.

    How can I solve this issue? Please help.

    Thanks,

    Paul

     

     

    Monday, February 28, 2011 1:58 PM

All replies

  • I think the problem is more serious than incorrect XML.  The server is returning 500, which is 'Internal Server Error'.  If it was just that the XML was wrong, you'd still get a 200 response containing a more helpful EWS error message.  Are you able to do anything at all with EWS that doesn't return a 500?  See if you can spot something in the server's event logs.
    Outlook Web Access For PDA , OWA For WAP
    www.owa-pda.com
    email a@t leederbyshire d.0.t c.0.m
    Monday, February 28, 2011 3:32 PM
  • I think the EWS Java API, does not send the phone numbers and email addresses to the server even if the phone numbers and email are updated in the contact for modifying.

     

    So if I am updating only home phone number, the message sent to the server is:

     

    <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:m="http://schemas.microsoft.com/exchange/services/2006/messages" xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types">
    	<soap:Header>
    		<t:RequestServerVersion Version="Exchange2010_SP1"></t:RequestServerVersion>
    	</soap:Header>
    	<soap:Body>
    		<m:UpdateItem MessageDisposition="SaveOnly" ConflictResolution="AutoResolve">
    			<m:ItemChanges>
    				<t:ItemChange>
    					<t:ItemId Id="AQMkADRjYTg2ZDFhLWFiNmItNDZjNC1iOGI1LTE2ZDdiY2I3YjVlNABGAAADYMPsItRsWUSfH+02NDGqDgcA1DZqiDxpaEmrqwAwbjVaMxoAAAGNFQAAAY6b1DJIS9lPvMAR8jGczG4AAAJAOQAAAA==" ChangeKey="EQAAABYAAACOm9QySEvZT7zAEfIxnMxuAAAAAH8U"></t:ItemId>
    					<t:Updates></t:Updates>
    				</t:ItemChange>
    			</m:ItemChanges>
    		</m:UpdateItem>
    	</soap:Body>
    </soap:Envelope>

    The "<t:Updates></t:Updates>" is going empty to the server. So exchange server might return "Internal Server Error" as the request to update the contacts does not have any update at all.

    If I am updating GivenName and a phone number, then also, the request doesnt contain phone number. But it will contain the GivenName to be updated. The message sent is:

     

    <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:m="http://schemas.microsoft.com/exchange/services/2006/messages" xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types">
    	<soap:Header>
    		<t:RequestServerVersion Version="Exchange2010_SP1"></t:RequestServerVersion>
    	</soap:Header>
    	<soap:Body>
    		<m:UpdateItem MessageDisposition="SaveOnly" ConflictResolution="AutoResolve">
    			<m:ItemChanges>
    				<t:ItemChange>
    					<t:ItemId Id="AQMkADRjYTg2ZDFhLWFiNmItNDZjNC1iOGI1LTE2ZDdiY2I3YjVlNABGAAADYMPsItRsWUSfH+02NDGqDgcA1DZqiDxpaEmrqwAwbjVaMxoAAAGNFQAAAY6b1DJIS9lPvMAR8jGczG4AAAJAOAAAAA==" ChangeKey="EQAAABYAAACOm9QySEvZT7zAEfIxnMxuAAAAAH7n"></t:ItemId>
    					<t:Updates>
    						<t:SetItemField>
    							<t:FieldURI FieldURI="contacts:GivenName"></t:FieldURI>
    							<t:Contact>
    								<t:GivenName>PaulN</t:GivenName>
    							</t:Contact>
    						</t:SetItemField>
    					</t:Updates>
    				</t:ItemChange>
    			</m:ItemChanges>
    		</m:UpdateItem>
    	</soap:Body>
    </soap:Envelope>

     

    <t:Updates>
    <t:SetItemField>
    <t:FieldURI FieldURI="contacts:GivenName"></t:FieldURI>
    <t:Contact>
    <t:GivenName>PaulN</t:GivenName>
    </t:Contact>
    </t:SetItemField>
    </t:Updates>

    In this case, the server does not return 500 as there is GivenName value to be updated.

     

    So I think the problem is in the EWS java API. It is not sending the phone number details and Email addresses as part of the SOAP messages even if they are set in the contact.

    Wednesday, March 2, 2011 8:45 AM
  • Hmm.  There aren't many EWS Java examples out there, are there?  But

    contact.getPhoneNumbers().setPhoneNumber( PhoneNumberKey.BusinessPhone, "04841131009" );

    looks odd.  Have you tried just

    contact.setPhoneNumber( PhoneNumberKey.BusinessPhone, "04841131009" );

    ?


    Outlook Web Access For PDA , OWA For WAP
    www.owa-pda.com
    email a@t leederbyshire d.0.t c.0.m
    Wednesday, March 2, 2011 3:08 PM
  • There are no APIs to set phone numbers like that.
    Friday, March 4, 2011 9:16 AM
  • Are you following a code sample that was provided with the SDK, or from somehere else?


    Outlook Web Access For PDA , OWA For WAP
    www.owa-pda.com
    email a@t leederbyshire d.0.t c.0.m
    Friday, March 4, 2011 2:14 PM
  • The only code sample that I am having is the "Getting started with EWS Java API.RTF" that comes the EWS JAVA API release. In that guide very few samples are there. There are no sample showing setting of the phone number in a contact.

     

    I just checked the APIs in JavaDoc and found setting of phone number could be done this way.

     

    While creating a contact, if I set the phone number like

    contact.getPhoneNumbers().setPhoneNumber( PhoneNumberKey.BusinessPhone, "04841131009" );

    It is working fine. There are no issues. The issue is coming when I update the contact with phone number.

    And by server's event logs, you mean the logs that are shown in the event viewer\Application?

    Monday, March 7, 2011 5:29 AM
  • If it works okay with a new contact, but fails when updating an existing one, then there may be a problem with the API.  I'd expect it to work the same for both operations.

    Yes, I'm thinking of the Event Viewer\Application logs.


    Outlook Web Access For PDA , OWA For WAP
    www.owa-pda.com
    email a@t leederbyshire d.0.t c.0.m
    Monday, March 7, 2011 1:54 PM
  • Hi,

    I was trying again to find the issue, why the phone number is not going in the request even if it is updated. I found the issue and by changing some code, this could be fixed.

    In microsoft.exchange.webservices.data.PhoneNumberDictionary,

    Current code:
    
    	public void setPhoneNumber(PhoneNumberKey key, String value) {
    		if (value == null) {
    			this.internalRemove(key);
    		} else {
    			PhoneNumberEntry entry;
    
    			if (this.getEntries().containsKey(key)) {
    				entry = this.getEntries().get(key);
    				entry.setPhoneNumber(value);
    				this.changed();
    			} else {
    				entry = new PhoneNumberEntry(key, value);
    				this.internalAdd(entry);
    			}
    		}
    	}
    New code:
    
    	public void setPhoneNumber(PhoneNumberKey key, String value) {
    		if (value == null) {
    			this.internalRemove(key);
    		} else {
    			PhoneNumberEntry entry;
    
    			if (this.getEntries().containsKey(key)) {
    				entry = this.getEntries().get(key);
    				entry.setPhoneNumber(value);
    				/* Add the following line. */
    				complexPropertyChanged( entry );
    				this.changed();
    			} else {
    				entry = new PhoneNumberEntry(key, value);
    				this.internalAdd(entry);
    			}
    		}
    	}

    In microsoft.exchange.webservices.data.PhoneNumberEntry

    Current Code:
    
    	public void setPhoneNumber(Object value) {
    		this.canSetFieldValue((String) this.phoneNumber, value);
    	}
    New Code:
    
    	public void setPhoneNumber(Object value) {
    		if( this.canSetFieldValue((String) this.phoneNumber, value) ) {
    			this.phoneNumber = (String)value;
    		}
    	}

    In microsoft.exchange.webservices.data.DictionaryProperty.writeSetUpdateToXml(EwsServiceXmlWriter writer,
                ServiceObject ewsObject, PropertyDefinition propertyDefinition)

    			if (!entry.writeSetUpdateToXml(writer, ewsObject,
    					propertyDefinition.getXmlElement())) {
    				writer.writeStartElement(XmlNamespace.Types, ewsObject
    						.getSetFieldXmlElementName());
    				this.writeUriToXml(writer, entry.getKey());
    
    				writer.writeStartElement(XmlNamespace.Types, ewsObject
    						.getXmlElementName());
    				writer.writeStartElement(XmlNamespace.Types, propertyDefinition
    						.getXmlElementName());
    				entry.writeToXml(writer, this.getEntryXmlElementName(entry));
    				writer.writeEndElement();
    				writer.writeEndElement();
    
    				writer.writeEndElement();
    			}

    Here, I am not sure, how to change the code. We should anyway change this for updating EmailAddress also (See http://social.technet.microsoft.com/Forums/en-US/exchangesvrdevelopment/thread/fa8cb203-e06e-4dc5-9d5b-f069ef9d6555).

    The statement

    				writer.writeStartElement(XmlNamespace.Types, propertyDefinition
    						.getXmlElementName());
    is wrong. This statement writes a start element "FieldURI". But here, it must actually write "PhoneNumbers". I think the solution might be to add one method in DictionaryProperty and it should be overridden in the PhoneNumberDictionary to return "PhoneNumbers" and in EmailAddressDictionary to return "EmailAddresses" and so on. And in the writeSetUpdateToXml() use this method to get the start element. But I am not sure about this.

    If we do these changes then the updating phone number is working.

    Is there anyway I can raise a bug or provide a patch? Could we get this issue fixed in the next release?

    Regards,

    Paul

     

    Wednesday, April 13, 2011 7:50 AM
  • I would suggest starting a new thread asking 'How can I raise a bug alert' (or somethin glike that), explaining what you found.  There are a few people from MS here, but they don't look at every thread, and this one is probably too long for them to look at, now.  I think a new thread would be the best way to attract attention.
    Outlook Web Access For PDA , OWA For WAP
    www.owa-pda.com
    email a@t leederbyshire d.0.t c.0.m
    Wednesday, April 13, 2011 1:04 PM
  • I stumbled on the same bug and the solution is what you said:

    1.) Add

    complexPropertyChanged(entry);

     

    2.) Change

    if (this.canSetFieldValue((String) this.phoneNumber, value)) {
      this.phoneNumber = (String)value;
    }

     

    3.

    In microsoft.exchange.webservices.data.DictionaryProperty change the line in writeSetUpdateToXml() function

     

    writer.writeStartElement(XmlNamespace.Types, propertyDefinition.getXmlElementName());

     

    to

     

    writer.writeStartElement(XmlNamespace.Types, propertyDefinition.getXmlElement());

     

    because API uses the same property when they create new in function (microsoft.exchange.webservices.data.ComplexPropertyDefinitionBase)

     

    @Override
    	protected void writePropertyValueToXml(EwsServiceXmlWriter writer,
    			PropertyBag propertyBag, boolean isUpdateOperation)
    			throws Exception {
    		ComplexProperty complexProperty = (ComplexProperty) propertyBag.getObjectFromPropertyDefinition(this);
    		if (complexProperty != null) {
    			complexProperty.writeToXml(writer, this.getXmlElement());
    		}
    	}
    The same should be done for EmailAddressDictionary and EmailAddressEntry.

     


    Regards, Jan Čustović
    Monday, April 18, 2011 1:47 PM
  • Hi All ,I wanted to let you know that we have an updated version (1.1.2) available with a change to address this issue "Could not update the phone numbers of a contact"  Please let us know if you have any questions or problems using the updated version.  Thank you for using EWS Java API and for your feedback!
    Monday, May 23, 2011 3:28 PM