如何:检索共享服务提供程序的内容源

企业级搜索管理对象模型中的 Content 对象可提供对内容源的访问,这些内容源是为共享服务提供程序 (SSP) 的搜索服务配置的。

下面的过程演示如何从控制台应用程序中写出内容源的名称和 ID 的完整列表。

从控制台应用程序显示内容源的名称和 ID 的列表

  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 的搜索上下文的 Content 对象,请添加以下代码。有关用于检索搜索上下文的方法的详细信息,请参阅如何:返回搜索服务提供程序的搜索上下文

    /*
    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);
    }
    Content sspContent = new Content(context);
    
  4. 使用以下代码检索内容源的集合。

    ContentSourceCollection sspContentSources = sspContent.ContentSources;
    
  5. 若要循环访问内容源并显示每个内容源的名称和 ID,请添加以下代码。

    foreach (ContentSource cs in sspContentSources)
    {
       Console.WriteLine("NAME: " + cs.Name + "  ID: " + cs.Id);
    }  
    

示例

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

先决条件

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

项目引用

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

  • 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 ContentSourcesSample
{
    class Program
    {
        static void Main(string[] args)
        {
          /*
           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);
            }
            Content sspContent = new Content(context);
            ContentSourceCollection sspContentSources = sspContent.ContentSources;

            foreach (ContentSource cs in sspContentSources)
            {
                Console.WriteLine("NAME: " + cs.Name + "  ID: " + cs.Id);
            }
        }
    }
}

See Also

任务

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

如何:添加内容源

如何:删除内容源

如何:以编程方式管理对内容源的爬网

如何:以编程方式配置内容源的爬网计划

概念

企业级搜索管理对象模型入门

内容源概述