搜索项并通过聚合视图获取项

本示例演示如何搜索多个文件夹和存储内的项目以及如何在聚合视图中返回这些项目。

示例

TableView 对象的 GetTable() 方法可在聚合视图中返回包含的项目来自同一存储或多个存储中的一个或多个文件夹的 Table 对象。 This is particularly useful when you want to access items returned from a search (for example, a search on all mail items in a store). 本主题提供了一个示例,用来展示如何使用即时搜索来搜索当前用户的经理发送来的所有标记为重要的项目并在之后显示每个搜索结果的主题。

在以下代码示例中,GetItemsInView 方法会检查当前用户的主要送达存储帐户是否是 Exchange 帐户、当前用户是否有经理以及是否可在相应会话的默认存储中执行即时搜索。 由于搜索依赖于 Explorer 对象的 Search 方法,且搜索结果需使用 GetTable 方法获得,因此 GetItemsInView 会创建 Explorer 对象、在该对象中显示 Inbox 并用其设置搜索。

GetItemsInView 会在所有 MailItem 类型的文件夹中搜索来自当前用户的经理且标记为重要的项目。 在 GetItemsInView 调用 Search 方法之后,所有搜索结果都会显示在资源管理器中,包括来自其他文件夹和存储的符合搜索条件的项目。 GetItemsInView 会获取包含搜索结果的此资源管理器视图的 TableView 对象。 然后,通过使用此 TableView 对象的 GetTable 方法,GetItemsInView 会得到包含搜索返回的聚合项目的 Table 对象。 最后,GetItemsInView 会显示 Table 对象的每一行(一行代表搜索结果中的一项)的主题栏。

如果使用 Visual Studio 测试此代码示例,必须先添加对 Microsoft Outlook 15.0 对象库组件的引用,并在导入 Microsoft.Office.Interop.Outlook 命名空间时指定 Outlook 变量。 不得将 using 语句直接添加到此代码示例中的函数前面,这个语句必须后跟公共类声明。 下面的代码行演示了如何在 C# 中执行导入和分配。

using Outlook = Microsoft.Office.Interop.Outlook;
private void GetItemsInView()
{
    Outlook.AddressEntry currentUser =
        Application.Session.CurrentUser.AddressEntry;

    // Check whether the current user uses the Exchange Server.
    if (currentUser.Type == "EX")
    {
        Outlook.ExchangeUser manager =
            currentUser.GetExchangeUser().GetExchangeUserManager();

        // Check whether the current user has a manager.
        if (manager != null)
        {
            string managerName = manager.Name;

            // Check whether Instant Search is enabled and 
            // operational in the default store.
            if (Application.Session.DefaultStore.IsInstantSearchEnabled)
            {
                Outlook.Folder inbox =
                    Application.Session.GetDefaultFolder(
                    Outlook.OlDefaultFolders.olFolderInbox);

                // Create a new explorer to display the Inbox as
                // the current folder.
                Outlook.Explorer explorer =
                    Application.Explorers.Add(inbox,
                    Outlook.OlFolderDisplayMode.olFolderDisplayNormal);

                // Make the new explorer visible.
                explorer.Display;

                // Search for items from the manager marked important, 
                // from all folders of the same item type as the current folder, 
                // which is the MailItem item type.
                string searchFor =
                    "from:" + "\"" + managerName 
                    + "\"" + " importance:high";
                explorer.Search(searchFor,
                    Outlook.OlSearchScope.olSearchScopeAllFolders);

                // Any search results are displayed in that new explorer
                // in an aggregated table view.
                Outlook.TableView tableView = 
                    explorer.CurrentView as Outlook.TableView;

                // Use GetTable of that table view to obtain items in that
                // aggregated view in a Table object.
                Outlook.Table table = tableView.GetTable();
                while (!table.EndOfTable)
                {
                    // Then display each row in the Table object
                    // that represents an item in the search results.
                    Outlook.Row nextRow = table.GetNextRow();
                    Debug.WriteLine(nextRow["Subject"]);
                }
            }
        }
    }
}

另请参阅