Share via


How to: Programmatically Manage the Crawl of a Content Source

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

  1. Set references in your application to the following DLLs:

    • Microsoft.SharePoint.dll

    • Microsoft.Office.Server.dll

    • Microsoft.Office.Server.Search.dll

  2. 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;
    
  3. 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.");
    }
    
  4. 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

  1. 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.

  2. Retrieve the collection of content sources.

    ContentSourceCollection sspContentSources = sspContent.ContentSources;
    
  3. 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];
    
  4. 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

  • Replace the "<…>" placeholder with the following code.

    cs.StartIncrementalCrawl();
    break;
    

To start a full crawl of the content source

  • Replace the "<…>" placeholder with the following code.

    cs.StartFullCrawl();
    break;
    

To pause a crawl in process

  • Replace the "<…>" placeholder with the following code.

    cs.PauseCrawl();
    break;
    

To resume a paused crawl

  • Replace the "<…>" placeholder with the following code.

    cs.ResumeCrawl();
    break;
    

To stop a crawl of the content source

  • Replace the "<…>" placeholder with the following code.

    cs.StopCrawl();
    break;
    

To check the crawl status values for a content source

  • There are several crawl status values you can check for a content source, using the properties of the ContentSource object. The following sample shows how to check these properties.

    Console.WriteLine("Crawl Status = " + cs.CrawlStatus);
    Console.WriteLine("Crawl started at: " + cs.CrawlStarted.ToString());
    Console.WriteLine("Crawl completed at: " + cs.CrawlCompleted.ToString());
    

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: Add a Content Source
How to: Delete a Content Source
How to: Programmatically Configure a Crawl Schedule for a Content Source

Concepts

Content Sources Overview