[方法] 管理プロパティを作成する
エンタープライズ検索 管理オブジェクト モデルの [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("Create Managed Property"); Console.WriteLine("Usage: CreateManagedPropertiesSample.exe PropertyName <DataType>"); Console.WriteLine("<DataType> - The data type for the new property. Must be one of:"); Console.WriteLine("binary"); Console.WriteLine("datetime"); Console.WriteLine("decimal"); Console.WriteLine("integer"); Console.WriteLine("text"); Console.WriteLine("yesno"); }
コンソール アプリケーションの Main() 関数で、args[] パラメータ内のアイテム数をチェックするコードを追加します。この数は 2 である必要があります。2 でない場合は、手順 3. で定義した Usage() 関数を呼び出します。
if (args.Length != 2) { Usage(); return; }
args[] パラメータで指定されている、使用する予定の値を取得して、管理プロパティの名前とデータ型を指定します。
string strName = args[0]; string strDataType = args[1];
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("Managed Property with that name already exists."); return; }
Contains メソッドが false を返す場合は、strDataType 変数の値を使用してどのデータ型を使用するかを特定してから、Create メソッドを呼び出して新しい管理プロパティを作成します。
switch (strDataType) { case ("binary"): properties.Create(strName, ManagedDataType.Binary); Console.WriteLine(strName + " created."); break; case ("datetime"): properties.Create(strName, ManagedDataType.DateTime); Console.WriteLine(strName + " created."); break; case ("decimal"): properties.Create(strName, ManagedDataType.Decimal); Console.WriteLine(strName + " created."); break; case ("integer"): properties.Create(strName, ManagedDataType.Integer); Console.WriteLine(strName + " created."); break; case ("text"): properties.Create(strName, ManagedDataType.Text); Console.WriteLine(strName + " created."); break; case ("yesno"): properties.Create(strName, ManagedDataType.YesNo); Console.WriteLine(strName + " created."); break; default: Console.WriteLine("Datatype not recognized."); Usage(); break; }
例
以下に、このトピックで説明したサンプル コンソール アプリケーションの完全なコードを示します。
前提条件
- 共有サービス プロバイダが既に作成されていることを確認します。
プロジェクト参照
このサンプルを実行する前に、コンソール アプリケーション コード プロジェクトに以下のプロジェクト参照を追加します。
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 CreateManagedPropertiesSample
{
class Program
{
static void Main(string[] args)
{
try
{
if (args.Length != 2)
{
Usage();
return;
}
string strName = args[0];
string strDataType = args[1];
/*
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("Managed Property with that name already exists.");
return;
}
switch (strDataType)
{
case ("binary"):
properties.Create(strName, ManagedDataType.Binary);
Console.WriteLine(strName + " created.");
break;
case ("datetime"):
properties.Create(strName, ManagedDataType.DateTime);
Console.WriteLine(strName + " created.");
break;
case ("decimal"):
properties.Create(strName, ManagedDataType.Decimal);
Console.WriteLine(strName + " created.");
break;
case ("integer"):
properties.Create(strName, ManagedDataType.Integer);
Console.WriteLine(strName + " created.");
break;
case ("text"):
properties.Create(strName, ManagedDataType.Text);
Console.WriteLine(strName + " created.");
break;
case ("yesno"):
properties.Create(strName, ManagedDataType.YesNo);
Console.WriteLine(strName + " created.");
break;
default:
Console.WriteLine("Datatype not recognized.");
Usage();
break;
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
static void Usage()
{
Console.WriteLine("Create Managed Property");
Console.WriteLine("Usage: CreateManagedPropertiesSample.exe PropertyName <DataType>");
Console.WriteLine("<DataType> - The data type for the new property. Must be one of:");
Console.WriteLine("binary");
Console.WriteLine("datetime");
Console.WriteLine("decimal");
Console.WriteLine("integer");
Console.WriteLine("text");
Console.WriteLine("yesno");
}
}
}
See Also
タスク
[方法] 共有サービス プロバイダの管理プロパティを取得する