gautamsi / ews-javascript-api

EWS API for TypeScript/JavaScript - ported from OfficeDev/ews-managed-api - node, cordova, meteor, Ionic, Electron, Outlook Add-Ins
MIT License
281 stars 72 forks source link

ItemAttachment.Load() fails with "The element at position 0 is invalid" #389

Closed cm253 closed 7 months ago

cm253 commented 3 years ago

Used library version: 0.10.3 Nodejs: 14 API Exchange Version: Exchange2013_SP1

How to reproduce: Create an ItemAttachment for an appointment, for example drag & drop an Outlook contact, mail or appointment to an appointment. Try call the ItemAttachment's Load() function. The following error is thrown:

Stack trace:

Exception: The element at position 0 is invalid
at Function.EwsUtilities.ValidateParamCollection (node_modules/ews-javascript-api/js/Core/EwsUtilities.js:611:23)
at GetAttachmentRequest.Validate (node_modules/ews-javascript-api/js/Core/Requests/GetAttachmentRequest.js:157:41)
at GetAttachmentRequest.ServiceRequestBase.ValidateAndEmitRequest (node_modules/ews-javascript-api/js/Core/Requests/ServiceRequestBase.js:394:14)
at node_modules/ews-javascript-api/js/Core/Requests/SimpleServiceRequestBase.js:55:19
at Promise._execute (node_modules/bluebird/js/release/debuggability.js:384:9)
at Promise._resolveFromExecutor (node_modules/bluebird/js/release/promise.js:518:18)
at new Promise (node_modules/bluebird/js/release/promise.js:103:10)
at GetAttachmentRequest.SimpleServiceRequestBase.InternalExecute (node_modules/ews-javascript-api/js/Core/Requests/SimpleServiceRequestBase.js:52:16)
at node_modules/ews-javascript-api/js/Core/Requests/MultiResponseServiceRequest.js:48:19
    at Promise._execute (node_modules/bluebird/js/release/debuggability.js:384:9)
at Promise._resolveFromExecutor (node_modules/bluebird/js/release/promise.js:518:18)
at new Promise (node_modules/bluebird/js/release/promise.js:103:10)
at GetAttachmentRequest.MultiResponseServiceRequest.Execute (node_modules/ews-javascript-api/js/Core/Requests/MultiResponseServiceRequest.js:47:16)
at ExchangeService.InternalGetAttachments (node_modules/ews-javascript-api/js/Core/ExchangeService.js:1044:24)
at ExchangeService.GetAttachment (node_modules/ews-javascript-api/js/Core/ExchangeService.js:1010:21)
at ItemAttachment.Attachment.InternalLoad (node_modules/ews-javascript-api/js/ComplexProperties/Attachment.js:212:29)

Code:

const exchangeAttachments: Attachment[] = item.Attachments?.GetEnumerator() ?? [];
for await (const attachment of exchangeAttachments) {
    await attachment.Load();
}

Similar issue: https://github.com/gautamsi/ews-javascript-api/issues/201

1056109 commented 2 years ago

A useful workaround may be to fetch the attachment again with ExchangeService.GetAttachments After this re-fetch ItemAttachment.Load() is possible (at least in my case)

if (exchangeItem.HasAttachment) {
  const exchangeAttachments: Attachment[] = exchangeItem.Attachments?.GetEnumerator() ?? [];

  for await (const attachment of exchangeAttachments) {
    const attachments = await ExchangeService.GetAttachments([attachment.Id], BodyType.HTML, []);
    const itemAttachment: ItemAttachment = attachments.Responses[0].Attachment as ItemAttachment;
    await itemAttachment.Load([ItemSchema.MimeContent]);
  }
}
cclauss commented 1 year ago

Is this still valid?

parlato-vooma commented 10 months ago

ExchangeService.GetAttachments([attachment.Id], ...) results in a Typescript error for me. It appears GetAttachments expects a full Attachment?

(method) ExchangeService.GetAttachments(attachments: Attachment[], bodyType: BodyType, additionalProperties: PropertyDefinitionBase[]): Promise<ServiceResponseCollection<GetAttachmentResponse>> (+1 overload)

Is this a bug?

gautamsi commented 7 months ago

I have to check but use the workaround used by @1056109 for now.