如何:检索映射到托管属性的已爬网属性

可以使用企业级搜索管理对象模型访问映射到托管属性的已爬网属性的列表。

以下过程介绍如何从控制台应用程序为映射到特定托管属性的已爬网属性编写 GUID 和类别。

检索映射到托管属性的已爬网属性

  1. 在应用程序中设置对以下 DLL 的引用:

    • Microsoft.SharePoint.dll

    • Microsoft.Office.Server.dll

    • Microsoft.Office.Server.Search.dll

  2. 在控制台应用程序的类文件中,将以下 using 语句添加到具有其他命名空间指令的代码顶部附近。

    using Microsoft.SharePoint;
    using Microsoft.Office.Server.Search.Administration;
    
  3. 若要为文共享服务提供程序 (SSP) 的搜索上下检索 Schema 对象,请添加以下代码。有关搜索上下文的检索方法的详细信息,请参阅如何:返回搜索服务提供程序的搜索上下文

    /*
    Replace <SiteName> with the name of a site using the SSP
    */
    string strURL = "http://<SiteName>";
    SearchContext context;
    using (SPSite site = new SPSite(strURL))
    {
        Context = SearchContext.GetContext(site);
    }
    Schema sspSchema = new Schema(context);
    
  4. 使用以下代码检索托管属性的集合:

    ManagedPropertyCollection properties = sspSchema.AllManagedProperties;
    
  5. 检索与 args[0] 参数中所指定名称相匹配的托管属性。

    ManagedProperty mProp = properties[args[0]]
    
  6. 为该托管属性检索已爬网属性的列表,并将类别名称和属性 GUID 写入控制台。为此,请将前面步骤中的“< … >”字符替换为以下代码:

    foreach (CrawledProperty cProp in mProp.GetMappedCrawledProperties(mProp.GetMappings().Count))
    {
    Console.WriteLine(cProp.Name);
    Console.WriteLine(cProp.Propset);
    }
    

示例

下面是控制台应用程序类示例的完整代码。

先决条件

  • 确保已创建了共享服务提供程序。

项目引用

运行本示例之前,在控制台应用程序代码项目中添加以下项目引用:

  • Microsoft.SharePoint

  • Microsoft.Office.Server

  • Microsoft.Office.Server.Search

using System;
using System.Collections;
using System.Text;
using Microsoft.Office.Server.Search.Administration;
using Microsoft.SharePoint;

namespace ManagedPropertiesSample
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                /*
                   Replace <SiteName> with the name
                   of a site using the SSP
                */
                string strURL = "http://<SiteName>";
                SearchContext context;
                using (SPSite site = new SPSite(strURL))
                {
                    Context = SearchContext.GetContext(site);
                }
                Schema sspSchema = new Schema(context);
                ManagedPropertyCollection props = sspSchema.AllManagedProperties;
                ManagedProperty mProp = properties[args[0]];
                foreach (CrawledProperty cProp in mProp.GetMappedCrawledProperties(mProp.GetMappings().Count))
                        {
                             Console.WriteLine(cProp.Name);
                             Console.WriteLine(cProp.Propset);
                        }
            }
            catch(Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }
    }
}

See Also

任务

如何:检索共享服务提供程序的托管属性

如何:创建托管属性

如何:删除托管属性

概念

搜索管理对象模型入门

管理元数据