nmarus / node-ews

A simple JSON wrapper for the Exchange Web Services (EWS) SOAP API.
MIT License
116 stars 52 forks source link

FindItem with Restriction element - multiple elements inside `IsEqualTo` #81

Closed carera closed 4 years ago

carera commented 6 years ago

Hi all, I am trying to fetch all unread emails in mailbox:

const ewsArgs = {
    attributes: {
      Traversal: 'Shallow'
    },
    ItemShape: {
      BaseShape: 'IdOnly',
      AdditionalProperties: {
        FieldURI: [
          {attributes: {FieldURI: 'item:Subject'}},
          {attributes: {FieldURI: 'message:IsRead'}}
        ]
      }
    },
    Restriction: {
      IsEqualTo: [
        {FieldURI: {attributes: {FieldURI: 'message:IsRead'}}},
        {FieldURIOrConstant: {Constant: {attributes: {Value: 'false'}}}}
      ]
    },
    ParentFolderIds: [{
      DistinguishedFolderId: {
        attributes: {
          Id: 'inbox'
        }
      }
    }]
  }

But the restriction element just does not work and the result fetches all emails regardless of their read status. I have also tried the IsEqualTo to be an object:

    Restriction: {
      IsEqualTo: {
        FieldURI: {attributes: {FieldURI: 'message:IsRead'}},
        FieldURIOrConstant: {
          Constant: {attributes: {Value: 'false'}}
        }
      }
    },

I have also tried similar approach as with multiple tags of identical name (https://github.com/CumberlandGroup/node-ews/issues/65):

    Restriction: {
      IsEqualTo: {
        FieldURIOrConstant: [
          {attributes: {FieldURI: 'message:IsRead'}},
          {Constant: {attributes: {Value: 'false'}}}
        ]
      }
    },

But still got the same result. Any ideas?

carera commented 6 years ago

I figured out it can be worked around with manually adding t: prefix to the elements:

    Restriction: {
      't:IsEqualTo': {
        't:FieldURI': {attributes: {FieldURI: 'message:IsRead'}},
        't:FieldURIOrConstant': {
          't:Constant': {attributes: {Value: 'false'}}
        }
      }
    },

I am not sure what are the t: prefixes, but it seems they are required and the processing procedure does not add them

kcastrotech commented 6 years ago

Glad you got it figured out!

t: = Type m: = Message

Those are supposed to be automatically prefixed when forming the XML, but as seen in issue #61 it doesn't always seem to do so. I haven't had time to investigate where the issue is (I suspect it to be something in the node-soap library) so for now manually adding the prefix is the appropriate workaround.

vksinghibm commented 6 years ago

After this I need to call GetItem to get the body of email. Can you provide the pointer how to integrate "GetItem" and "FindItem" integrated together in single program using this node-ews framework. Any pointer will be appreciated...

mhal12 commented 6 years ago

@carera what ews function did you use to retreive emails, im using expandDL but no messages are returned.

CExMigbo commented 5 years ago
ews.run(ewsFunction, ewsArgs).then(result => {
        console.log(JSON.stringify(result.ResponseMessages));
        console.log(JSON.stringify(result.ResponseMessages.FindItemResponseMessage.RootFolder.Items));
        if (!result.ResponseMessages.FindItemResponseMessage.RootFolder.Items.Message.length) {
            result.ResponseMessages.FindItemResponseMessage.RootFolder.Items.Message = [result.ResponseMessages.FindItemResponseMessage.RootFolder.Items.Message]
        }

        async.eachSeries(result.ResponseMessages.FindItemResponseMessage.RootFolder.Items.Message, function(item, eachCallback){
            console.log('item.ItemId ==>', item.ItemId);
            getItem(item.ItemId.attributes, function(){
                eachCallback();
            });
        })

    }).catch(err => {
        console.log(err.stack);
    });
CExMigbo commented 5 years ago
var getItem = function(item, callback){
    const ewsFunction = 'GetItem';

    const ewsArgs = {
      'ItemShape': {
        'BaseShape': 'Default',
        'IncludeMimeContent':'true'
      },
      "ItemIds" : {
        "ItemId" : {
            "attributes": {
              "ChangeKey" : item.ChangeKey,
               "Id" : item.Id
            }
          },
      }
    };
    ews.run(ewsFunction, ewsArgs).then(result => {
        console.log(JSON.stringify(result));
        callback(result)
    }).catch(err => {
        console.log(err.stack);
        callback(err);
    });
}

var readEmail  = function(limit, isRead){
    const ewsFunction = 'FindItem';

    isRead = !!isRead;
    //limit = limit

    const ewsArgs = {
      'attributes': {
        'Traversal': 'Shallow'
      },
      'ItemShape': {
        'BaseShape': 'IdOnly'
      },
      'ParentFolderIds' : {
        'DistinguishedFolderId': {
          'attributes': {
            'Id': 'inbox'
          }
        }
      },
      'AdditionalProperties':[{
        'FieldURI':{
            'attributes': {
                'FieldURI': 'item:Subject'
            }
        },
        'FieldURI':{
                'attributes': {
                    'FieldURI': 'message:IsRead'
                }
        }
      }],
      'Restriction':{
        't:IsEqualTo':{
            't:FieldURI':{
                'attributes': {
                    'FieldURI': 'message:IsRead'
                }
            },
            't:FieldURIOrConstant':{
                't:Constant':{
                    'attributes': {
                        'Value': isRead.toString()
                    },
                }
            }
        }
      },
      'IndexedPageItemView': {
        'attributes': {
            'BasePoint': 'Beginning',
            'Offset': '0',
            'MaxEntriesReturned': limit.toString()
          }
      }
    };