[方法] 管理プロパティを削除する
エンタープライズ検索 管理オブジェクト モデルの [Schema] オブジェクトは、共有サービス プロバイダ (SSP) の検索サービス用に構成された管理プロパティへのアクセスを提供します。Schema オブジェクトの詳細については、「メタデータを管理する」を参照してください。
以下の手順では、コンソール アプリケーションの エンタープライズ検索 管理オブジェクト モデルを使用して、プログラムによって管理プロパティを削除する方法を示します。
管理プロパティをコンソール アプリケーションから削除するには
アプリケーションで、以下の DLL への参照を設定します。
Microsoft.SharePoint.dll
Microsoft.Office.Server.dll
Microsoft.Office.Server.Search.dll
コンソール アプリケーションのクラス ファイルで、他の名前空間ディレクティブを含むコードの上部付近に次の using ステートメントを追加します。
using Microsoft.SharePoint; using Microsoft.Office.Server.Search.Administration;
利用状況情報をコンソール ウィンドウに書き出す関数を作成します。
static void Usage() { Console.WriteLine("Delete Managed Properties Sample"); Console.WriteLine("Usage: DeleteManagedPropertiesSample.exe PropertyName"); }
コンソール アプリケーションの Main() 関数で、コードを追加して args[] パラメータ内の項目数を確認します。これは 1 でなければなりません。これ以外の場合は、手順 3. で定義した Usage() 関数を呼び出します。
if (args.Length != 1) { Usage(); return; }
args[] パラメータで指定した値を取得します。これを使用して、削除する管理プロパティの名前を指定します。
string strName = args[0];
SSP の検索コンテキストの Schema オブジェクトを取得するには、以下のコードを追加します。検索コンテキストの取得方法の詳細については、「[方法] 検索サービス プロバイダに検索コンテキストを返す」を参照してください。
/* Replace <SiteName> with the name of a site using the SSP */ string strURL = "http://<SiteName>"; Schema sspSchema = new Schema(SearchContext.GetContext(new SPSite(strURL)));
以下のコードを使用して、管理プロパティのコレクションを取得します。
ManagedPropertyCollection properties = sspSchema.AllManagedProperties;
管理プロパティのコレクションに指定したプロパティが含まれているかどうかを確認します。
if (properties.Contains(strName)) { Console.WriteLine(strName + " property does not exist. Delete failed."); return; }
Contains メソッドが true を返す場合は、管理プロパティをループして、それぞれについて Name プロパティが strName の値と一致するかどうかを確認します。
foreach (ManagedProperty property in properties) { if (property.Name == strName) {
次に、DeleteDisallowed プロパティの値をチェックして、プロパティが削除可能なことを確認します。
if (property.DeleteDisallowed) { Console.WriteLine("DeleteDisallowed enabled for " + strName + ". Delete failed."); return; }
DeleteDisallowed プロパティが false を返す場合は、DeleteAllMappings メソッドを呼び出した後、プロパティの Delete メソッドを呼び出します。
property.DeleteAllMappings(); property.Delete(); Console.WriteLine(strName + " deleted."); return; } }
例
以下に、コンソール アプリケーション クラスのサンプルの完全なコードを示します。
前提条件
- 共有サービス プロバイダが既に作成されていることを確認します。
プロジェクト参照
このサンプルを実行する前に、コンソール アプリケーション コード プロジェクトに以下のプロジェクト参照を追加します。
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 DeleteManagedPropertiesSample
{
class Program
{
static void Main(string[] args)
{
try
{
if (args.Length != 1)
{
Usage();
return;
}
string strName = args[0];
/*
Replace <SiteName> with the name of a site using the SSP
*/
string strURL = "http://<SiteName>";
Schema sspSchema = new Schema(SearchContext.GetContext(new SPSite(strURL)));
ManagedPropertyCollection properties = sspSchema.AllManagedProperties;
if (!properties.Contains(strName))
{
Console.WriteLine(strName + " property does not exist. Delete failed.");
return;
}
foreach (ManagedProperty property in properties)
{
if (property.Name == strName)
{
if (property.DeleteDisallowed)
{
Console.WriteLine("DeleteDisallowed enabled for " + strName + ". Delete failed.");
return;
}
property.DeleteAllMappings();
property.Delete();
Console.WriteLine(strName + " deleted.");
return;
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
private static void Usage()
{
Console.WriteLine("Delete Managed Properties Sample");
Console.WriteLine("Usage: DeleteManagedPropertiesSample.exe PropertyName");
}
}
}
See Also
タスク
[方法] 共有サービス プロバイダの管理プロパティを取得する