2

We are using ColdFusion 2016 and are using the EWS API to connect to exchange. I have managed to get the code sample from the following post working but am not able to figure out how to use the SearchFilter with the findItems() method. Why is the get action of cfexchangemail never returning?

service = createObject("Java", "microsoft.exchange.webservices.data.ExchangeService");
service.init();

version = createObject("Java", "microsoft.exchange.webservices.data.ExchangeVersion");
service.init(version.Exchange2010);

credentials = createObject("Java", "microsoft.exchange.webservices.data.WebCredentials");

credentials.init(yourusername, yourpassword);
service.setCredentials(credentials);

uri = createObject("Java", "java.net.URI");
uri.init("outlook webservices url");
service.setUrl(uri);
WellKnownFolderName=createObject("Java","microsoft.exchange.webservices.data.WellKnownFolderName");
result = service.FindItems(service.WellKnownFolderName.Inbox, CreateObject("java", "microsoft.exchange.webservices.data.ItemView").init(100));

for(item in result.getItems(){
    // ..loop through each field and store their value 
    // in query object or something...
}

So basically is it possible to translate this Java code to Coldfusion?

ItemView view = new ItemView(10);
view.getOrderBy().add(ItemSchema.DateTimeReceived, SortDirection.Ascending);
view.setPropertySet(new PropertySet(BasePropertySet.IdOnly, ItemSchema.Subject, ItemSchema.DateTimeReceived));

FindItemsResults<Item> findResults =
    service.findItems(WellKnownFolderName.Inbox,
        new SearchFilter.SearchFilterCollection(
            LogicalOperator.Or, new SearchFilter.ContainsSubstring(ItemSchema.Subject, "EWS"),
        new SearchFilter.ContainsSubstring(ItemSchema.Subject, "API")), view);

service.loadPropertiesForItems(findResults, PropertySet.FirstClassProperties);
System.out.println("Total number of items found: " + findResults.getTotalCount());

for (Item item : findResults) {
    System.out.println(item.getSubject());
    System.out.println(item.getBody());
    // Do something with the item.
}

Update from comments:

I can't figure out how to instantiate the SearchFilter or SearchFilterCollection. I tried creating a searchFilter object, but it does not have the properties to work with. I must be must be missing something.

CreateObject("java","microsoft.exchange.webservices.data.Sea‌​rchFilter");
Community
  • 1
  • 1
DTECH99
  • 43
  • 3
  • (Edit) What part are you having difficulty with? – Leigh Apr 28 '17 at 21:29
  • I can't figure out how to instantiate the searchFilter or SearchFilterCollection. `code`SearchFilter.new SearchFilter.SearchFilterCollection( LogicalOperator.Or, new SearchFilter.ContainsSubstring(ItemSchema.Subject, "EWS"))`code`. When creating this searchFilter object it does not have the properties to work with. I must be must be missing something. CreateObject("java","microsoft.exchange.webservices.data.SearchFilter"); – DTECH99 Apr 28 '17 at 23:21

2 Answers2

2

The final working result was:

folderName = createObject("java", "microsoft.exchange.webservices.data.WellKnownFolderName");
itemView = CreateObject("java","microsoft.exchange.webservices.data.ItemView").init(10,0);
folderID = CreateObject("java","microsoft.exchange.webservices.data.FolderId").init(
        folderName.Inbox,
        mailBox
        );
sortDirection = CreateObject("java","microsoft.exchange.webservices.data.SortDirection");
basePropertySet = createObject("java", "microsoft.exchange.webservices.data.BasePropertySet");
propertyset = createObject("java", "microsoft.exchange.webservices.data.PropertySet");
itemSchema = createObject("java", "microsoft.exchange.webservices.data.ItemSchema");
searchFilterCollection = CreateObject("java","microsoft.exchange.webservices.data.SearchFilter$SearchFilterCollection");
logicalOperator = createObject("java", "microsoft.exchange.webservices.data.LogicalOperator");
subString1 = CreateObject("java","microsoft.exchange.webservices.data.SearchFilter$ContainsSubstring").init(
        itemSchema.Subject,
        "EWS"
        );
subString2 = CreateObject("java","microsoft.exchange.webservices.data.SearchFilter$ContainsSubstring").init(
        itemSchema.Subject,
        "API"
        );
searchFilterCollection.add(SubString1);
searchFilterCollection.add(SubString2);
searchFilterCollection.setLogicalOperator(LogicalOperator.Or);
bodyType = createObject("java", "microsoft.exchange.webservices.data.BodyType");
propertyset.add(itemSchema.Subject);
propertyset.add(itemSchema.DateTimeReceived);
propertyset.setBasePropertySet(basePropertySet.IdOnly);
propertyset.setRequestedBodyType(bodyType.HTML);
itemView.getOrderBy().add(itemSchema.DateTimeReceived, sortDirection.Ascending);
itemView.setPropertySet(propertyset);
viewResults = service.findItems(folderID, searchFilterCollection, itemView);
Leigh
  • 28,765
  • 10
  • 55
  • 103
DTECH99
  • 43
  • 3
1

(Not an a full answer, but too long for comments)

No wonder... that is a lot of nesting. I do not have that lib installed, but if it were me I would start by breaking it down into separate statements, one new object per line. Start with the innermost constructor:

new SearchFilter.ContainsSubstring(
          ItemSchema.Subject
        , "API"
    )

You will need to create an instance of ItemSchema first, to have access the Subject property. Looking at the C# docs (no javadocs available) the path should be something like this:

ItemSchema = createObject("java", "microsoft.exchange.webservices.data.ItemSchema");

After that move on to creating the SearchFilter. Notice the period . in the class name? The keyword new followed by a class name, containing a period, usually means it is an inner class. Inner classes require a special syntax, ie ParentClass$InnerClass. Something like this should work:

APISubString = CreateObject("java","microsoft.exchange.webservices.data.SearchFilter$ContainsSubstring");
writeDump(APISubString );

If it works, add init() to initialize the object:

  APISubString = CreateObject("java","microsoft.exchange.webservices.data.SearchFilter.ContainsSubstring").init(ItemSchema.Subject, "API");

Then move to the next innermost object:

new SearchFilter.ContainsSubstring(
           ItemSchema.Subject
          , "EWS"
    )

and lather, rinse, repeat for the remaining objects:

// ItemSchema.Subject is a static value and can be reused
EWSSubString = CreateObject("java","microsoft.exchange.webservices.data.SearchFilter.ContainsSubstring").init(ItemSchema.Subject, "EWS");
Leigh
  • 28,765
  • 10
  • 55
  • 103