The ContentSource class in the Enterprise Search Administration object model provides several methods you can use to programmatically start, stop, pause, and resume crawls outside of a crawl schedule for a specific content source. You can also use several ContentSource class properties to check the crawl status of a content source.
The steps in the following procedures show how to do the following tasks:
-
Set up a console application to use the Enterprise Search Administration object model.
-
Retrieve a specific content source.
-
Perform various crawl management tasks for that content source.
You can also programmatically configure the crawl schedule for a content source. For more information, see How to: Programmatically Configure a Crawl Schedule for a Content Source.
To set up your application to use the Enterprise Search Administration object model
-
Set references in your application to the following DLLs:
-
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("Manage Content Source Crawl Status");
Console.WriteLine("Usage: ManageCrawlStatus.exe <ContentSource>");
Console.WriteLine("<ContentSourceName> - Specify the content source name.");
}
-
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 1, meaning that no value was specified to identify the content source, then call the Usage() function defined in the previous step.
if (args.Length < 1 )
{
WriteUsage();
return;
}
To retrieve a specific content source
-
Add the following code to retrieve the Content object for the Shared Services Provider's (SSP) search context:
/*
Replace <SiteName> with the name of a site using the SSP
*/
string strURL = "http://<SiteName>";
Content sspContent = new Content(SearchContext.GetContext(new SPSite(strURL)));
For more information about ways to retrieve the search context, see How to: Return the Search Context for the Search Service Provider.
-
Retrieve the collection of content sources.
ContentSourceCollection sspContentSources = sspContent.ContentSources;
-
Retrieve the value specified in first item of the args[] parameter, which indicates the name of the content source to retrieve.
string strContentSourceName = args[0];
-
Retrieve the content source with the specified name from the content source collection.
ContentSource cs = sspContentSources[strContentSourceName];
<…>
To start an incremental crawl of the content source
To start a full crawl of the content source
To pause a crawl in process
To resume a paused crawl
To stop a crawl of the content source
To check the crawl status values for a content source
See Also