Outlook获取网页的联系人 - 0x800a139e - JavaScript运行时错误

时间:2016-08-14 16:08:04

标签: c# asp.net web outlook exchange-server

我有一个Outlook联系人查找,我想通过点击按钮从我的网络应用程序运行。以下代码是我的dll类和方法:

 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("", "", "", "");

        }            
    }

以下代码是在网页应用上点击按钮时运行的时间:

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

    var contact = al.getContact();
}

首次打开VS时,整个过程按预期运行,联系变量返回正确的数据。此问题是当我尝试再次单击该按钮或再次运行整个Web应用程序时,该过程超时。

  

{8}中第8栏第8行的未处理异常......   0x800a139e - JavaScript运行时错误:Sys.WebForms.PageRequestManagerTimeoutException:服务器请求超时。

我觉得我错过了一些基本的东西,因为我之前还没有这样做过。非常感谢你的帮助。

当我运行它时,Windows应用程序按预期加载(如果有帮助)

1 个答案:

答案 0 :(得分:0)

我知道你是在本地主机上运行它但是看起来你正在服务器端上下文中进行Office的自动化,并且根据知识库文章Considerations for server-side Automation of Office不支持。这可以解释为什么使用winform应用程序可以正常工作。

您不打算在服务器上安装Office?相反,您应该使用Exchange Servers WebService进行服务器端自动化实现,例如获取联系人。

修改

  

“这个事实刚刚出现在我身上。这个网络应用程序用于内部网系统,dll将在所有用户PC上注册。为了能够使用这个dll的用户办公室安装,我需要运行它通过javascript以某种方式?谢谢“

Javascript方法:

我个人认为the orthodox method and use the web service允许用户使用任何网页浏览器, IE是唯一支持ActiveX 的网页浏览器。除了让它在IE中工作之外,您可能还必须使用ActiveX权限(SysAdmin可以覆盖或锁定)。如果你想通过Javascript这样做,这应该有所帮助:

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”]+”)”);
}

“如果此代码不起作用,请执行以下操作: 在Internet Explorer中,转到“工具”|互联网选项|安全 - &gt;从“自定义级别”转到“初始化并编写未标记为可安全执行脚本的ActiveX控件”并选择“提示” - 这应该可以立即使用“

Wordpress blog ref

Stackoverflow ref

Exchange Web服务方法:

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);
            }
        }
    }
}

参考我的代码和MSDN ref