Question

I am maintaining a service written to synchronize Contacts with EWS (ExchangeWebService). This service is updating contactpictures which stopped working recently. The exception thrown is a DeleteAttachmentException

Initially the code looked like this:

contact.SetContactPicture(filePath);
contact.Update(ConflictResolutionMode.AlwaysOverwrite); // throws the excpetion

Eventhough it only "overwrites" the contactpicture, it seems the old one gets deleted first internally, hence the error message i guess.

So i tried to manually make sure of the deletion first, which is not a soloution either:

if(contact.HasPicture)
{
    contact.RemoveContactPicture();
    contact.Update(ConflictResolutionMode.AlwaysOverwrite); // throws the Exception
}

When creating new contacts, this works fine. It displays the Contactpicture. However the updating of existing ones doesnt work.

Sadly the Exception doesnt provide much information:

Microsoft.Exchange.WebServices.Data.DeleteAttachmentException ist aufgetreten.
  _HResult=-2146233088
  _message=At least one attachment couldn't be deleted.
  HResult=-2146233088
  IsTransient=false
  Message=At least one attachment couldn't be deleted.
  Source=Microsoft.Exchange.WebServices
  StackTrace:
  Microsoft.Exchange.WebServices.Data.AttachmentCollection.InternalDeleteAttachments(IEnumerable`1 attachments)
InnerException: 

The whole thing runs in usercontext (owner of the mailbox).

UPDATE

I made it completely independet whether the Attachment is a ContactPicture or whatever, still same result:

_item is an ExchangeItem (which has been loaded)

public void UpdateAttachment(string attachmentPath, string attachmentName)
{
    Attachment attachment = _item.Attachments.SingleOrDefault(att => String.Equals(att.Name, attachmentName, StringComparison.OrdinalCultureIgnoreCase));

    if (attachment != null)
    {
        _item.Attachments.Remove(attachment);
        _item.Update(ConflictResolutionMode.AlwaysOverwrite);
    }

    attachment = _item.Attachments.AddFileAttachment(attachmentName, attachmentPath);

    _item.Update(ConflictResolutionMode.AlwaysOverwrite);
}
Was it helpful?

Solution

You are correct that the EWS Managed API is internally deleting the old attachment first before saving the new one. The error you are receiving is due to a communication issue with the service. If your code was working before, I'm guessing that something has changed regarding the permissions of the user that is running this code.

So the first thing I would suggest you check is the permissions of the user. Secondly, if that doesn't resolve the problem for you, I would suggest adding a trace listener to your code and post your EWS request and response so we can track this down a bit further. Here is an article that should help you with the trace listener:

How to: Trace requests and responses to troubleshoot EWS Managed API applications

I hope this helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top