Forum Discussion

fayrehouse's avatar
fayrehouse
Frequent Contributor
13 years ago

TestComplete and EWS (Exchange Web Services)

Hi All,



I have EWS installed on my PC, and have (by way of example) the following PowerShell script to connect to my Exchange account, and retrieve the latest 8 items in my inbox. 




$email    = "my.name@mycompany.co.uk"


$s = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService


$s.UseDefaultCredentials = $true


$s.AutodiscoverUrl($email)


$inbox = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($s,[Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Inbox)



$psPropertySet.RequestedBodyType = [Microsoft.Exchange.WebServices.Data.BodyType]::Text;


$items = $inbox.FindItems(8)


 


What I'm struggling to work out however - is the jscript alternative for use in TC - particularly (I think this is the key) the line in bold above...



Can anyone help? :)





6 Replies


  • Hi Steve,


     


    Sorry for the late reply.


     


    Unfortunately, we've failed to convert the C# code to JScript :( 


    The issue is related to the callback function used when calling the AutodiscoverUrl method. 


     


    I can suggest several approaches to accomplish your task:


     - You can continue using your PowerShell script to get the needed data. This thread contains an example demonstrating how to call a PowerShell script in TestComplete. You will just need to add AddCommand to the script and specify the name of the script file: powerShell.AddScript(". .\MyScript.ps1");


     - Based on the sample I posted, you can create a small .NET app or a .NET assembly that will get the information you need.


     - Work with EWS as with a usual web service. TestComplete provides a built-in feature to work with web services. Refer to the "Testing Web Services - Overview" article to learn more about it. Unfortunately, in this case, you will have to create a script anew.


     


    Let me know which option you choose.

  • fayrehouse's avatar
    fayrehouse
    Frequent Contributor
    Hi All... 



    So I've gotten a lot closer:




      var myExchange = dotNET.Microsoft_Exchange_WebServices_Data.ExchangeService.zctor_3(dotNET.Microsoft_Exchange_WebServices_Data.ExchangeVersion.Exchange2007_SP1);


      var myCredentials = dotNET.Microsoft_Exchange_WebServices_Data.ExchangeCredentials();


      var MyInboxId = dotNET.Microsoft_Exchange_WebServices_Data.FolderId.zctor("Inbox");


      myExchange.UseDefaultCredentials = true;



      myExchange.AutodiscoverUrl("me@mydomain.com");




      var MyPropSet = dotNET.Microsoft_Exchange_WebServices_Data.PropertySet.zctor_4(dotNET.Microsoft_Exchange_WebServices_Data.BasePropertySet.FirstClassProperties);


      MyPropSet.RequestedBodyType = dotNET.Microsoft_Exchange_WebServices_Data.BodyType.Text;






    HOWEVER... Sometimes I have to work remotely, via VPN. As I discovered today, in this case, the AutodiscoverURL above generates an error: "Autodsciver blocked a potentially insecure redirection to <<<autodiscover.xml path>>> . To allow Autodiscover to follow the rediection use the AutodiscoverURL(string, AutodiscoverRedirectionUrlValidationCallback) overload.



    So I did some googling, but cannot work out the 2nd parameter for:

      myExchange.AutodiscoverUrl("me@mydomain.com", ????SOMETHINGHERE????);







    Can anyone advise?






  • Hi Steve,


     


    I used to work with EWS from a .NET app, and the following C# code worked for me:




    public int Main()


    {


    ...


    try


    {


    _exchangeService.AutodiscoverUrl(emailAddress, RedirectionUrlValidationCallback);


    }


    catch (Exception ex)


    {


    _log = "Couldn't autodiscover the URL: " + ex.Message;


    }


    ...


    }


    // Create the callback to validate the redirection URL.


    static bool RedirectionUrlValidationCallback(String redirectionUrl)


    {



    }




    Try converting it to TestComplete's JScript code. Please let me know if you need my assistance.

  • fayrehouse's avatar
    fayrehouse
    Frequent Contributor
    Hi Tanya,



    A bit of help with the conversion to JScript would be hugely appreciated. I'm probably being really dumb here but...



    The 2nd parameter in AutoDiscover is of course a call to ReedirectionUrlValidationCallback. The call doesn't mention a param, but the function defintion DOES include a string param (the URL). Also, the function returns a bool, but the 2nd overload of AutoDiscoverUrl is expecting the 2nd param to be of *type* AutoDiscoverRedirectionUrlValidationCallback?





    I suspect a lot of this is about me learning to convert some of the EWS samples (typically C#) into JScript / using the TC dotNet notation...



    I've no doubt I'm missing something obvious, but... :)



    Thanks



    Steve



  • fayrehouse's avatar
    fayrehouse
    Frequent Contributor
    Hi all,



    Can anyone help translate Tanya's function above? Please bear in mind my previous comments - unless I'm missing something, it's currently returning a bool when it should be returning an object of type AutoDiscoverRedirectionUrlValidationCallback - and the function definition has a parameter, but the call to it doesn't pass one? Where does it pick up the URL from? :)





  • fayrehouse's avatar
    fayrehouse
    Frequent Contributor
    Sorry to bump this folks - but I really do need some help here - Tanya, can you help out here - esp with my post of 25 Sept?



    Thanks!