Search code examples
node.jsexchange-serverexchangewebservicesnode-ews

node-ews Update email to mark as read


I'm using "node-ews" library version 3.5.0, but when I try to update any property I get the following error:

{
   "ResponseMessages":{
      "UpdateItemResponseMessage":{
         "attributes":{
            "ResponseClass":"Error"
         },
         "MessageText":"An internal server error occurred. The operation failed., Object reference not set to an instance of an object.",
         "ResponseCode":"ErrorInternalServerError",
         "DescriptiveLinkKey":0,
         "Items":null
      }
   }
}

I'm trying to mark email as read using the following code:

const markFolderAsRead = async (ews, id, changeKey) => {
  const args = {
    attributes: {
      MessageDisposition: "SaveOnly",
    },
    ItemChanges: {
      ItemChange: {
        ItemId: {
          attributes: {
            Id: id,
            ChangeKey: changeKey,
          },
        },
        Updates: {
          SetItemField: {
            FieldURI: {
              attributes: {
                FieldURI: "message:IsRead",
              },
              Message: {
                IsRead: true,
              },
            },
          },
        },
      },
    },
  };
  await ews.run("UpdateItem", args).then((result) => {
    console.log("email read:", JSON.stringify(result));
  });
};

I tried several modifications, including trying to update another fields, but none of it worked. I followed this documentation: https://learn.microsoft.com/pt-br/exchange/client-developer/web-service-reference/updateitem-operation And the lib doesn't show any example of it, but when I change the json to a wrong "soap" construction the error show different messages, or even if I do not pass any of the parameters required as "ChangeKey". So, maybe this error is something relate to microsoft ews soap construction that I'm missing parameters, or so.


Solution

  • Got it working!

    My JSON was wrong. The FieldURI was finishing after the message attribute, it should be before.

    Correct JSON:

    const args = {
        attributes: {
          MessageDisposition: "SaveOnly",
          ConflictResolution: "AlwaysOverwrite",
          SendMeetingInvitationsOrCancellations: "SendToNone",
        },
        ItemChanges: {
          ItemChange: {
            ItemId: {
              attributes: {
                Id: id,
                ChangeKey: changeKey,
              },
            },
            Updates: {
              SetItemField: {
                FieldURI: {
                  attributes: {
                    FieldURI: "message:IsRead",
                  },
                },
                Message: {
                  IsRead: "true",
                },
              },
            },
          },
        },
      };