0

I have a outlook contact lookup that I want to run from my web app on a button click. The following code is my dll class and method:

 public class AddressLookup
{
    public Contact getContact()
    {
        RDOSession session = new RDOSession();
        session.Logon(Type.Missing, Type.Missing, Type.Missing, true, Type.Missing, Type.Missing);
        bool loggedOn = session.LoggedOn;

        try
        {
            RDOAddressBook rAddressBook = session.AddressBook;
            RDORecipients rContacts = rAddressBook.ShowAddressBook(Title: "Outlook Lookup", OneAddress: true);

            RDORecipient rContact = rContacts.GetFirst();
            RDOAddressEntry aeContact = rContact.AddressEntry;

            return new Contact(aeContact.Name, aeContact.JobTitle, aeContact.CompanyName, aeContact.StreetAddress);
        }
        catch (Exception)
        {
            return new Contact("", "", "", "");

        }            
    }

The following code is when i ran when the button is clicked on the web app:

protected void btnBillHeaderDetailsOutlook_Click(object sender, EventArgs e)
{
    AddressLookup al = new AddressLookup();      

    var contact = al.getContact();
}

When open VS for the first time, the whole process runs as expected and the contact variable returns the right data. This issue is when I try to click the button again or run the whole web app again the process times out.

Unhandled exception at line 885, column 13 in http://localhost:27855/ScriptResource.axd?d=... 0x800a139e - JavaScript runtime error: Sys.WebForms.PageRequestManagerTimeoutException: The server request timed out.

I feel like I am missing something basic as I have yet to do this before. Many thanks for the help.

When I run it as a windows application is loads as expected (if that helps)

Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321
William James
  • 67
  • 1
  • 16
  • Outlook is written in C or C++, (afaik the only office app that's completely .Net/C#/Managed code is InfoPath), therefore you are working with an unManaged library with resources you need to explicitly Dispose of or even easier wrap in a Using statement, so try... using(RDOSession session.... using(RDOAddressBook rAddressBook... ) { – Jeremy Thompson Aug 14 '16 at 16:20
  • @JeremyThompson thanks, it seems these object are not implementing IDisposable as there is no dispose() method available to the RDOSession object. – William James Aug 14 '16 at 16:54

1 Answers1

0

I know you're running this off localhost however appears you are doing automation of Office in a Server-Side context and that is unsupported as per the KB article: Considerations for server-side Automation of Office. This would explain why it works fine using a winform app.

You weren't intending on installing Office on the server? Instead you should use the Exchange Servers WebService for server-side automation implementations like getting contacts.

Edit:

"That fact has just dawned on me. This web app is for a intranet system and the dll will be registered on all the users PC's. To be able to use the users office installation with this dll would I need to run it through javascript somehow? Thanks"

Javascript Method:

Personally I'd go the orthodox method and use the web service as that allows users to use any web browser, IE is the only one that supports ActiveX. In addition to get it working in IE you may have to play with ActiveX permissions (which the SysAdmin may override or lock down). If you want to do it via Javascript this should help:

var Const_olFolderContacts = 10;
var objApp = new ActiveXObject(“Outlook.Application”);
var objNS = objApp.GetNamespace(“MAPI”);
var colContacts = objNS.GetDefaultFolder(Const_olFolderContacts).Items
for( var i=1; i<=colContacts.count;i++)
{
 var v = colContacts.item(i);
 alert(v[“FullName”]+” (“+v[“Email1Address”]+”)”);
}

"If this code does not work, do the following: In Internet Explorer , go to Tools | Internet Options | Security -> from ‘Custom Level’ go to ‘Initialize and script ActiveX controls not marked as safe for scripting’ and select ‘Prompt” — this should work now"

Wordpress blog ref.

Stackoverflow ref.

Exchange Web Service Method:

public class MicrosoftOutlook
{
    private ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010_SP2);
    public MicrosoftOutlook()
    {
        try 
        {
            service.Url = new Uri("https://webmail.YourCompanyName.com.au/EWS/Exchange.asmx");

            service.UseDefaultCredentials = true;

        } catch (System.Runtime.InteropServices.COMException ex) 
        {
        }
    }

    public void ListContacts() {
    // Get the number of items in the contacts folder. To limit the size of the response, request only the TotalCount property.
    ContactsFolder contactsfolder = ContactsFolder.Bind(service, WellKnownFolderName.Contacts,  new PropertySet(BasePropertySet.IdOnly, FolderSchema.TotalCount));

    // Set the number of items to the number of items in the Contacts folder or 50, whichever is smaller.
    int numItems = contactsfolder.TotalCount < 50 ? contactsfolder.TotalCount : 50;

    // Instantiate the item view with the number of items to retrieve from the Contacts folder.
    ItemView view = new ItemView(numItems);

    // To keep the response smaller, request only the display name.
    view.PropertySet = new PropertySet(BasePropertySet.IdOnly, ContactSchema.DisplayName);

    // Request the items in the Contacts folder that have the properties that you selected.
    FindItemsResults<Item>  contactItems = service.FindItems(WellKnownFolderName.Contacts, view);

    // Display the list of contacts. (Note that there can be a large number of contacts in the Contacts folder.)
        foreach (Item item in contactItems)
        {
            if (item is Contact)
            {
                Contact contact = item as Contact;
                Console.WriteLine(contact.DisplayName);
            }
        }
    }
}

Ref my code and this MSDN ref.

Community
  • 1
  • 1
Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321
  • thanks for that. That fact has just dawned on me. This web app is for a intranet system and the dll will be registered on all the users PC's. To be able to use the users office installation with this dll would I need to run it through javascript somehow? Thanks – William James Aug 14 '16 at 16:48