如何:删除内容源

在 企业级搜索中,通过为搜索服务的共享服务提供程序 (SSP) 配置的内容源来指示希望搜索服务爬网的内容。

以下步骤演示如何以编程方式通过使用企业级搜索对象模型来删除新的内容源。

若要以编程方式从 SSP 的内容源集合删除内容源,请使用 [ContentSource] 对象的 Delete 方法。

备注

删除内容源时,企业级搜索将启动爬网,以便从被删除的内容源的内容索引中移除所有项。

删除内容源

  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. 检索在 args[] 参数中指定的值,它指示要删除的内容源的名称。

    string strContentSource = args[0];
    
  5. 检索内容源的集合。

    ContentSourceCollection sspContentSources = sspContent.ContentSources;
    
  6. 检索其名称与 strContentSource 变量的值相匹配的内容源。

    ContentSource cs = sspContentSources[strContentSource];
    
  7. 调用 ContentSource 对象的 Delete 方法。

    cs.Delete();
    

示例

以下是本主题中描述的示例控制台应用程序的完整代码。

先决条件

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

项目引用

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

  • 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 DeleteContentSourceSample
{
    class Program
    {
        static void Main(string[] args)
        {
                try
                {
                        /*
                        Replace <SiteName> with the name of a site using the SSP
                        */
                        string strURL = "<SiteURL>";
                        SearchContext context;
                        using (SPSite site = new SPSite(strURL))
                        {
                             Context = SearchContext.GetContext(site);
                        }
                        Content sspContent = new Content(context);
                        string strContentSource = args[0];
                        ContentSourceCollection sspContentSources = sspContent.ContentSources;
                        ContentSource cs = sspContentSources[strContentSource];
                        cs.Delete();
                        Console.WriteLine(strContentSource + " deleted.");
                }
                catch (Exception e)
                {
                        Console.WriteLine(e.ToString());
                }
        }
    }
}

若要测试此代码示例,请执行下列操作:

  1. 编译控制台应用程序的项目。

  2. 打开一个命令窗口,导航到包含 DeleteContentSourceSample.exe 的目录。

  3. 运行以下代码:

    DeleteContentSourceSample.exe <Name>
    

    备注

    用要删除的内容源的实际名称替换 <Name>。

See Also

任务

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

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

如何:添加内容源

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

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

概念

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

内容源概述