Share via


[方法] コンテンツ ソースを追加する

Microsoft Office SharePoint Server 2007 でのエンタープライズ検索 では、検索サービスの共有サービス プロバイダ (SSP) に対して構成されているコンテンツ ソースで検索インデックス サービスがクロールするコンテンツを指定します。

SSP のコンテンツ ソースには、エンタープライズ検索 管理オブジェクト モデルの ContentSourceCollection オブジェクトからアクセスできます。コンテンツ ソースを作成するには、ContentSourceCollection クラスの Create メソッドを使用します。

次の手順では、エンタープライズ検索 オブジェクト モデルを使用してコンテンツ ソースをプログラムで作成する方法を示します。

プログラムでコンテンツ ソースを追加するには

  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. 利用状況情報をコンソール ウィンドウに書き出す関数を作成します。

    private static void Usage()
    {
       Console.WriteLine("Create Content Source");
       Console.WriteLine("Usage: CreateContentSource.exe ContentSourceName ContentSourceType startaddress [startaddress...]");
    }
    
  4. コンソール アプリケーションの Main() 関数で、args[] パラメータ内のアイテム数をチェックするコードを追加します。この数が 3 未満である場合は、手順 3. で定義した WriteUsage() 関数を呼び出します。

    if (args.Length < 3)
    {
    Usage();
    return;
    }
    
  5. 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);
    
  6. コンテンツ ソースの名前と種類を指定して、args[] パラメータで指定されている値を取得します。

    string strName = args[0];
    string strType = args[1];
    
  7. コンテンツ ソースのコレクションを取得します。

    ContentSourceCollection sspContentSources = sspContent.ContentSources;
    
  8. コンテンツ ソースのコレクションに、新しいコンテンツ ソースのために指定した名前と同じ名前を持つコンテンツ ソースが既に含まれているかどうかを特定します。

    if(sspContentSources.Exists(strName))
    {
        Console.WriteLine("A content source with that name already exists");
        return;
    }
    
  9. Exists メソッドが false を返す場合は、コンテンツ ソースに対して構成する開始アドレスを格納するためのリストを作成します。

    List<Uri> startAddresses = new List<Uri>();
    for (int i = 2; i < args.Length; ++i)
    {
       startAddresses.Add(new Uri(args[i]));
    }
    
  10. strType 変数の値を使用して作成するコンテンツ ソースの種類を判別し、Create メソッドを呼び出して、新しいコンテンツ ソースの開始アドレスを指定します。

    CustomContentSourceFileShareContentSourceExchangePublicFolderContentSourceLotusNotesContentSource の各オブジェクトでは、FollowDirectories プロパティを設定することで、検索インデックス コンポーネントが各開始アドレスのフォルダのみをクロールするのか、それともすべてのサブフォルダもクロールするのかを指定できます。この例では、これらのコンテンツ ソースに対しては、この値を true に設定します。

    SharePointContentSource オブジェクトでは、SharePointCrawlBehavior プロパティを使用することで、検索インデックス コンポーネントが、開始アドレスで指定されている SharePoint サイトのみをクロールするのか、それともそのホスト名に含まれるすべてのサイトをクロールするのかを指定できます。この例では、SharePoint サイトのみをクロールするようにこのプロパティの値を指定します。

    WebContentSource オブジェクトの場合は、MaxPageEnumerationDepth プロパティを使用することで検索インデックス コンポーネントがクロールするページ ホップの数を指定し、MaxSiteEnumerationDepth プロパティを使用することで検索インデックス コンポーネントがクロールするサイト ホップの数を指定できます。この例では、MaxPageEnumerationDepth を 100 に、MaxSiteEnumerationDepth を 10 に設定します。

    switch (strType)
    {
    case ("custom"):
       CustomContentSource customCS = (CustomContentSource)sspContentSources.Create(typeof(CustomContentSource), strName);
       foreach (Uri startAddress in startAddresses)
       {
          customCS.StartAddresses.Add(startAddress);
       }
       customCS.FollowDirectories = true;
       customCS.Update();
       Console.WriteLine(strName + " created.");
       break;
    
    case ("exchange"):
       ExchangePublicFolderContentSource exchangeCS = (ExchangePublicFolderContentSource)sspContentSources.Create(typeof(ExchangePublicFolderContentSource), strName);
       foreach (Uri startAddress in startAddresses)
       {
          exchangeCS.StartAddresses.Add(startAddress);
       }
       exchangeCS.FollowDirectories = true;
       exchangeCS.Update();
       Console.WriteLine(strName + " created.");
       break;
    
    case ("file"):
       FileShareContentSource fileCS = (FileShareContentSource)sspContentSources.Create(typeof(FileShareContentSource), strName);
       foreach (Uri startAddress in startAddresses)
       {
          fileCS.StartAddresses.Add(startAddress);
       }
       fileCS.FollowDirectories = true;
       fileCS.Update();
       Console.WriteLine(strName + " created.");
       break;
    
    case ("lotusnotes"):
       LotusNotesContentSource lotusnotesCS = (LotusNotesContentSource)sspContentSources.Create(typeof(LotusNotesContentSource), strName);
       foreach (Uri startAddress in startAddresses)
       {
          lotusnotesCS.StartAddresses.Add(startAddress);
       }
       lotusnotesCS.FollowDirectories = true;
       lotusnotesCS.Update();
       Console.WriteLine(strName + " created.");
       break;
    
    case ("sharepoint"):
       SharePointContentSource sharepointCS = (SharePointContentSource)sspContentSources.Create(typeof(SharePointContentSource), strName);
       foreach (Uri startAddress in startAddresses)
       {
          sharepointCS.StartAddresses.Add(startAddress);
       }
       sharepointCS.SharePointCrawlBehavior = SharePointCrawlBehavior.CrawlSites;
       sharepointCS.Update();
       Console.WriteLine(strName + " created.");
       break;
    
    case ("web"):
       WebContentSource webCS = (WebContentSource)sspContentSources.Create(typeof(WebContentSource), strName);
       foreach (Uri startAddress in startAddresses)
       {
          webCS.StartAddresses.Add(startAddress);
       }
       webCS.MaxPageEnumerationDepth = 100;
       webCS.MaxSiteEnumerationDepth = 10;
       webCS.Update();
       Console.WriteLine(strName + " created.");
       break;
    
    default:
       Console.WriteLine("Content source type not recognized.");
       break;
    }
    
  11. 最後に、SSP のコンテンツ ソース コレクションに含まれるコンテンツ ソースのリストを書き出すことができます (詳細は「[方法] 共有サービス プロバイダのコンテンツ ソースを取得する」を参照)。そのためには、次のコードをアプリケーションに追加します。

    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.Generic;
using System.Text;
using Microsoft.Office.Server.Search.Administration;
using Microsoft.SharePoint;

namespace CreateContentSourceSample
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                if (args.Length < 3)
                {
                    Usage();
                    return;
                }
/*
Replace <SiteName> with the name of a site using the SSP
*/
                string strURL = "<SiteName>";
                SearchContext context;
                using (SPSite site = new SPSite(strURL))
                {
                    Context = SearchContext.GetContext(site);
                }
                Content sspContent = new Content(context);
                string strName = args[0];
                string strType = args[1];
                ContentSourceCollection sspContentSources = sspContent.ContentSources;
                if (sspContentSources.Exists(strName))
                {
                    Console.WriteLine("A content source with that name already exists");
                    return;
                }
                List<Uri> startAddresses = new List<Uri>();
                for (int i = 2; i < args.Length; ++i)
                {
                    startAddresses.Add(new Uri(args[i]));
                }

                switch (strType)
                {
                    case ("custom"):
                        CustomContentSource customCS = (CustomContentSource)sspContentSources.Create(typeof(CustomContentSource), strName);
                        foreach (Uri startAddress in startAddresses)
                        {
                            customCS.StartAddresses.Add(startAddress);
                        }
                        customCS.FollowDirectories = true;
                        customCS.Update();
                        Console.WriteLine(strName + " created.");
                        break;
                    case ("exchange"):
                        ExchangePublicFolderContentSource exchangeCS = (ExchangePublicFolderContentSource)sspContentSources.Create(typeof(ExchangePublicFolderContentSource), strName);
                        foreach (Uri startAddress in startAddresses)
                        {
                           exchangeCS.StartAddresses.Add(startAddress);
                        }
                        exchangeCS.FollowDirectories = true;
                        exchangeCS.Update();
                        Console.WriteLine(strName + " created.");
                        break;
                    case ("file"):
                        FileShareContentSource fileCS = (FileShareContentSource)sspContentSources.Create(typeof(FileShareContentSource), strName);
                        foreach (Uri startAddress in startAddresses)
                        {
                            fileCS.StartAddresses.Add(startAddress);
                        }
                        fileCS.FollowDirectories = true;
                        fileCS.Update();
                        Console.WriteLine(strName + " created.");
                        break;
                    case ("lotusnotes"):
                        LotusNotesContentSource lotusnotesCS = (LotusNotesContentSource)sspContentSources.Create(typeof(LotusNotesContentSource), strName);
                        foreach (Uri startAddress in startAddresses)
                        {
                         lotusnotesCS.StartAddresses.Add(startAddress);
                        }
                        lotusnotesCS.FollowDirectories = true;
                        lotusnotesCS.Update();
                        Console.WriteLine(strName + " created.");
                        break;
                    case ("sharepoint"):
                        SharePointContentSource sharepointCS = (SharePointContentSource)sspContentSources.Create(typeof(SharePointContentSource), strName);
                        foreach (Uri startAddress in startAddresses)
                        {
                         sharepointCS.StartAddresses.Add(startAddress);
                        }
                        sharepointCS.SharePointCrawlBehavior = SharePointCrawlBehavior.CrawlSites;
                        sharepointCS.Update();
                        Console.WriteLine(strName + " created.");
                        break;
                    case ("web"):
                        WebContentSource webCS = (WebContentSource)sspContentSources.Create(typeof(WebContentSource), strName);
                        foreach (Uri startAddress in startAddresses)
                        {
                            webCS.StartAddresses.Add(startAddress);
                        }
                        webCS.MaxPageEnumerationDepth = 100;
                        webCS.MaxSiteEnumerationDepth = 10;
                        webCS.Update();
                        Console.WriteLine(strName + " created.");
                        break;

                    default:
                        Console.WriteLine("Invalid content source type.");
                        break;
                }
                foreach (ContentSource cs in sspContentSources)
                {
                    Console.WriteLine("NAME: " + cs.Name + "  ID: " + cs.Id);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
        }
        private static void Usage()
        {
            Console.WriteLine("Create Content Source");
            Console.WriteLine("Usage: CreateContentSource.exe ContentSourceName ContentSourceType startaddress [startaddress...]");
        }
  }
}

このコード サンプルをテストするには、以下を行います。

  1. コンソール アプリケーションのプロジェクトをコンパイルします。

  2. コマンド ウィンドウを開き、CreateContentSourceSample.exe が格納されているディレクトリに移動します。

  3. コマンド ウィンドウで次を実行します。

    CreateContentSourceSample.exe <ContentSourceName> <ContentSourceType> <StartAddress1> <StartAddress2> etc…
    

    注意

    <ContentSourceName> は作成するコンテンツ ソースの名前に置き換え、<ContentSourceType> は作成するコンテンツ ソースの種類を示す custom、exchange、file、lotusnotes、sharepoint、web のいずれかに置き換えます。

    <StartAddress1> はコンテンツ ソースの 1 番目の開始アドレスに置き換え、<StartAddress2> は 2 番目の開始アドレスに置き換えます (以下同様)。

See Also

タスク

[方法] 検索サービス プロバイダに検索コンテキストを返す

[方法] 共有サービス プロバイダのコンテンツ ソースを取得する

[方法] コンテンツ ソースを削除する

[方法] コンテンツ ソースのクロールをプログラム的に管理する

[方法] コンテンツ ソースのクロール スケジュールをプログラムで構成する

概念

エンタープライズ検索管理オブジェクト モデルを使うにあたって

コンテンツ ソースの概要