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

企业级搜索管理对象模型中的 Schema 对象提供对为共享服务提供程序 (SSP) 的搜索服务配置的托管属性的访问。

下面的过程演示如何从控制台应用程序编写托管属性及其属性标识符 (PID) 的完整列表。

从控制台应用程序显示托管属性名称及其 PID 的列表

  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. 若要循环访问托管属性并显示每个属性的名称和 PID,请添加以下代码。

    foreach (ManagedProperty property in properties)
    {
        Console.WriteLine(property.Name + "  PID: " + property.PID);
    }
    

示例

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

先决条件

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

项目引用

运行此示例之前,将下列项目引用添加到控制台应用程序的代码项目中:

  • 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 properties = sspSchema.AllManagedProperties;
                foreach (ManagedProperty property in properties)
                {
                    Console.WriteLine(property.Name + "  PID: " + property.PID);
                }
            }
            catch(Exception ex)
            {
                 Console.WriteLine(ex.ToString());
            }
        }
    }
}

See Also

任务

如何:返回搜索服务提供程序的搜索上下文

如何:创建托管属性

如何:删除托管属性

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

如何:将已爬网属性映射到托管属性

概念

搜索管理对象模型入门

管理元数据