How to: Add a Content Source
In Enterprise Search in Microsoft Office SharePoint Server 2007, you indicate what content you want the search indexing service to crawl through the content sources that are configured for the search service's Shared Services Provider (SSP).
You can access the SSP's content sources from the ContentSourceCollection object of the Enterprise Search Administration object model. To create a content source, you use the Create method of the ContentSourceCollection class.
The following procedure shows how to programmatically create a content source by using the Enterprise Search object model.
To add a content source programmatically
In your application, set references to the following DLLs:
Microsoft.SharePoint.dll
Microsoft.Office.Server.dll
Microsoft.Office.Server.Search.dll
In your console application's class file, add the following using statements near the top of the code with the other namespace directives.
using Microsoft.SharePoint; using Microsoft.Office.Server.Search.Administration;
Create a function to write out usage information to the console window.
private static void Usage() { Console.WriteLine("Create Content Source"); Console.WriteLine("Usage: CreateContentSource.exe ContentSourceName ContentSourceType startaddress [startaddress...]"); }
In the Main() function of the console application, add code to check the number of items in the args[] parameter; if it is less than 3, then call the WriteUsage() function defined in Step 3.
if (args.Length < 3) { Usage(); return; }
Add the following code to retrieve the Content object for the SSP's search context. For more information about ways to retrieve the search context, see How to: Return the Search Context for the Search Service Provider.
/* 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);
Retrieve the values specified in the args[] parameter by specifying the name and type for the content source.
string strName = args[0]; string strType = args[1];
Retrieve the collection of content sources.
ContentSourceCollection sspContentSources = sspContent.ContentSources;
Determine if the collection of content sources already includes a content source with the same name as the one specified for the new content source.
if(sspContentSources.Exists(strName)) { Console.WriteLine("A content source with that name already exists"); return; }
If the Exists method returns false, create a list to store the start addresses to configure for the content source.
List<Uri> startAddresses = new List<Uri>(); for (int i = 2; i < args.Length; ++i) { startAddresses.Add(new Uri(args[i])); }
Using the value in the strType variable, determine which content source type to create, call the Create method, and then specify the start addresses for the new content source.
For the CustomContentSource, FileShareContentSource, ExchangePublicFolderContentSource, and LotusNotesContentSource objects, you can specify whether the search index component crawls only the folder of each start address or all the subfolders as well by setting the FollowDirectories property. For this example, this value is set to true for those content sources.
For the SharePointContentSource object, you can specify if the search index component crawls only the SharePoint site specified in the start address, or every site included within that host name by using the SharePointCrawlBehavior property. For this example, the value in this property specifies to crawl only the SharePoint site.
For the WebContentSource object, you can specify how many page hops the search index component crawls by using the MaxPageEnumerationDepth property, as well as how many site hops the search index component crawls by using the MaxSiteEnumerationDepth property. In this example, MaxPageEnumerationDepth is set to 100, and MaxSiteEnumerationDepth is set to 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; }
Finally, you can write out the list of content sources in the SSP's collection of content sources (for more information, see How to: Retrieve the Content Sources for a Shared Services Provider). To do this, add the following code to your application.
foreach (ContentSource cs in sspContentSources) { Console.WriteLine("NAME: " + cs.Name + " ID: " + cs.Id); }
Example
Following is the complete code for the sample console application described in this topic.
Prerequisites
- Ensure a Shared Services Provider is already created.
Project References
Add the following Project References in your console application code project before running this sample:
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...]");
}
}
}
To test this code sample, do the following:
Compile the project for the console application.
Open a command window, and navigate to the directory containing CreateContentSourceSample.exe.
In the command window, run the following:
CreateContentSourceSample.exe <ContentSourceName> <ContentSourceType> <StartAddress1> <StartAddress2> etc…
Note
Replace <ContentSourceName> with the name of the content source you want to create, and <ContentSourceType> with one of the following to indicate the type of content source to create: custom, exchange, file, lotusnotes, sharepoint, web. Replace <StartAddress1> with the first start address for the content source, <StartAddress2> with the second start address, and so on.
See Also
Tasks
How to: Return the Search Context for the Search Service Provider
How to: Retrieve the Content Sources for a Shared Services Provider
How to: Delete a Content Source
How to: Programmatically Manage the Crawl of a Content Source
How to: Programmatically Configure a Crawl Schedule for a Content Source
Concepts
Getting Started with the Enterprise Search Administration Object Model
Content Sources Overview