如何:添加内容源

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

可以通过企业级搜索管理对象模型的 ContentSourceCollection 对象来访问 SSP 的内容源。要创建内容源,请使用 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>,用第二个起始地址替换 <StartAddress2>,依次类推。

See Also

任务

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

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

如何:删除内容源

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

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

概念

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

内容源概述