[EWS] Update contact

Résolu
blackhox Messages postés 34 Date d'inscription jeudi 31 octobre 2013 Statut Membre Dernière intervention 20 août 2016 - 11 déc. 2013 à 15:12
blackhox Messages postés 34 Date d'inscription jeudi 31 octobre 2013 Statut Membre Dernière intervention 20 août 2016 - 18 déc. 2013 à 12:13
Bonjour à tous,

J'essaye de modifier un contact se trouvant dans exchange via une application C# en passant par exchange web services. A l'exécution une exception est levée et son contenu est le suivant :

L'identificateur EWS est au format EwsLegacyId, lequel n'est pas pris en charge par la version Exchange indiquée par votre demande. Veuillez utiliser la méthode ConvertId pour convertir le format EwsLegacyId en EwsId.

J'ai essayé différentes méthodes pour convertir EwsLegacyId en EwsId sans succès.

Merci d'avance pour votre réponse et voici mon code :

ExchangeService _service = new ExchangeService(ExchangeVersion.Exchange2010);
_service.Credentials = new WebCredentials("user", "password");
_service.Url = new Uri("https://mail.domain.com/ews/exchange.asmx");

Contact contact = Contact.Bind(_service, labelId.Content.ToString());


// Update the contact's surname and company name.
contact.Surname = textBoxNom.Text;
contact.GivenName = textBoxPrenom.Text;

// Update the contact's phone number.
contact.PhoneNumbers[PhoneNumberKey.BusinessPhone] = textBoxNumTelEntreprise.Text;
contact.PhoneNumbers[PhoneNumberKey.HomePhone] = textBoxNumTelPrive.Text;
contact.PhoneNumbers[PhoneNumberKey.MobilePhone] = textBoxNumTelMobile.Text;
contact.PhoneNumbers[PhoneNumberKey.BusinessPhone2] = textBoxNumTelEntrepriseMobile.Text;

// Update the contact'semail address.
contact.EmailAddresses[EmailAddressKey.EmailAddress2] = new Microsoft.Exchange.WebServices.Data.EmailAddress(textBoxMailFirst.Text);
contact.EmailAddresses[EmailAddressKey.EmailAddress2] = new Microsoft.Exchange.WebServices.Data.EmailAddress(textBoxMailSec.Text);

// Update the contact's business address.
// Specify the home address.
PhysicalAddressEntry paEntry1 = new PhysicalAddressEntry();
paEntry1.Street = textBoxRuePerso.Text;
paEntry1.City = textBoxVillePerso.Text;
paEntry1.State = "";
paEntry1.PostalCode = textBoxCpPerso.Text;
paEntry1.CountryOrRegion = textBoxPaysPerso.Text;
contact.PhysicalAddresses[PhysicalAddressKey.Home] = paEntry1;

// Specify the business address.
PhysicalAddressEntry paEntry2 = new PhysicalAddressEntry();
paEntry2.Street = textBoxRuePro.Text;
paEntry2.City = textBoxVillePro.Text;
paEntry2.State = "";
paEntry2.PostalCode = textBoxCpPro.Text;
paEntry2.CountryOrRegion = textBoxPaysPro.Text;
contact.PhysicalAddresses[PhysicalAddressKey.Business] = paEntry2;

// Save the contact.
contact.Update(ConflictResolutionMode.AlwaysOverwrite);

1 réponse

blackhox Messages postés 34 Date d'inscription jeudi 31 octobre 2013 Statut Membre Dernière intervention 20 août 2016
Modifié par blackhox le 18/12/2013 à 12:13
Voici la solution :

ExchangeServiceBinding esb = new ExchangeServiceBinding();
esb.Credentials = new NetworkCredential("user", "password", "DOMAIN");
esb.RequestServerVersionValue = new RequestServerVersion();
esb.RequestServerVersionValue.Version = ExchangeVersionType.Exchange2010_SP2;
esb.Url = @"https://mail.domain.com/EWS/Exchange.asmx";

//Je récupère l'id qui est au format Legacy
ItemId itemId = textBoxId.Text;

// Create a request to convert identifiers.
ConvertIdType request = new ConvertIdType();
request.SourceIds = new AlternateIdType[1];
request.SourceIds[0] = new AlternateIdType();

// Convert EwsLegacyId to EwsId.
request.SourceIds[0].Format = IdFormatType.EwsLegacyId;
(request.SourceIds[0] as AlternateIdType).Id = itemId.ToString();
(request.SourceIds[0] as AlternateIdType).Mailbox = "mail@domain.com";
request.DestinationFormat = IdFormatType.EwsId;


// Send the request and get the response.
ConvertIdResponseType response = esb.ConvertId(request);

ResponseMessageType[] rmta = response.ResponseMessages.Items;

foreach (ResponseMessageType rmt in rmta)
{
ConvertIdResponseMessageType cirmt = (rmt as ConvertIdResponseMessageType);
AlternateIdType myId = (cirmt.AlternateId as AlternateIdType);
if (myId != null)
{
string format = myId.Format.ToString();
string identifier = myId.Id;
textBoxId.Text = identifier;
string mailbox = myId.Mailbox;
}
}
string id = textBoxId.Text;
Contact contact = Contact.Bind(_service, id);




// Update the contact's surname and company name.
contact.Surname = textBoxNom.Text;
contact.GivenName = textBoxPrenom.Text;

// Update the contact's business phone number.
contact.PhoneNumbers[PhoneNumberKey.BusinessPhone] = textBoxNumTelEntreprise.Text;
contact.PhoneNumbers[PhoneNumberKey.HomePhone] = textBoxNumTelPrive.Text;
contact.PhoneNumbers[PhoneNumberKey.MobilePhone] = textBoxNumTelMobile.Text;
contact.PhoneNumbers[PhoneNumberKey.BusinessPhone2] = textBoxNumTelEntrepriseMobile.Text;

// Update the contact's second email address.
contact.EmailAddresses[EmailAddressKey.EmailAddress1] = new Microsoft.Exchange.WebServices.Data.EmailAddress(textBoxMailFirst.Text);
contact.EmailAddresses[EmailAddressKey.EmailAddress2] = new Microsoft.Exchange.WebServices.Data.EmailAddress(textBoxMailSec.Text);

PhysicalAddressEntry paEntryHome = null;
PhysicalAddressEntry paEntryBusiness = null;

paEntryHome = contact.PhysicalAddresses[PhysicalAddressKey.Home];

paEntryBusiness = contact.PhysicalAddresses[PhysicalAddressKey.Business];

//Specifi the home address
paEntryHome.Street = textBoxRuePerso.Text;
paEntryHome.City = textBoxVillePerso.Text;
paEntryHome.State = "";
paEntryHome.PostalCode = textBoxCpPerso.Text;
paEntryHome.CountryOrRegion = textBoxPaysPerso.Text;

// Specify the business address.
paEntryBusiness.Street = textBoxRuePro.Text;
paEntryBusiness.City = textBoxVillePro.Text;
paEntryBusiness.State = "";
paEntryBusiness.PostalCode = textBoxCpPro.Text;
paEntryBusiness.CountryOrRegion = textBoxPaysPro.Text;

// Save the contact.
try
{
contact.Update(ConflictResolutionMode.AlwaysOverwrite);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
0
Rejoignez-nous