[方法] コンテンツ ソースを削除する
エンタープライズ検索 では、検索サービスの共有サービス プロバイダ (SSP) に対して構成するコンテンツ ソースにより、検索サービスがクロールするコンテンツを指定します。
次の手順では、エンタープライズ検索 オブジェクト モデルを使用してコンテンツ ソースをプログラムで削除する方法を示します。
プログラムで SSP のコンテンツ ソース コレクションからコンテンツ ソースを削除するには、[ContentSource] オブジェクトの Delete メソッドを使用します。
注意
コンテンツ ソースが削除されると、エンタープライズ検索 はクロールを実行して、削除されたコンテンツ リソースのコンテンツ インデックスに含まれるすべてのアイテムを削除します。
コンテンツ リソースを削除するには
次の DLL に対する参照をアプリケーションに設定します。
Microsoft.SharePoint.dll
Microsoft.Office.Server.dll
Microsoft.Office.Server.Search.dll
コンソール アプリケーションのクラス ファイルで、他の名前空間ディレクティブを含むコードの先頭近くに次の using ステートメントを追加します。
using Microsoft.SharePoint; using Microsoft.Office.Server.Search.Administration;
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);
検索コンテキストの取得方法の詳細については、「[方法] 検索サービス プロバイダに検索コンテキストを返す」を参照してください。
args[] パラメータで指定されている値を取得します。これは、削除するコンテンツ ソースの名前を示します。
string strContentSource = args[0];
コンテンツ ソースのコレクションを取得します。
ContentSourceCollection sspContentSources = sspContent.ContentSources;
strContentSource 変数の値に名前が一致するコンテンツ ソースを取得します。
ContentSource cs = sspContentSources[strContentSource];
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());
}
}
}
}
このコード サンプルをテストするには、以下を行います。
コンソール アプリケーションのプロジェクトをコンパイルします。
コマンド ウィンドウを開き、DeleteContentSourceSample.exe が格納されているディレクトリに移動します。
次のコードを実行します。
DeleteContentSourceSample.exe <Name>
注意
<Name> を、削除するコンテンツ ソースの実際の名前に置き換えます。
See Also
タスク
[方法] 共有サービス プロバイダのコンテンツ ソースを取得する
[方法] コンテンツ ソースのクロールをプログラム的に管理する
[方法] コンテンツ ソースのクロール スケジュールをプログラムで構成する