Share via


Console app to start incremental crawl in MOSS 2007

Wrote a little Console application to kick start incremental crawl in MOSS 2007.

Here's how it can be used.

Usage: StartIncrementalCrawl.exe SiteURL "Content SourceName"
(Example: StartIncrementalCrawl.exe https://MyMOSSSite "Local Office Sharepoint Server Sites" )

As you can see, First I am getting the [SearchContext] Reference and then [ContentSources] collection, then just search for the passed content source name string.  Upon finding the content source, kick the incremental crawl on [ContentSrouce] object with static method StartIncrementalCrawl().
(Note: This utility should be run under the context of Farm Admin only)

Copy paste following code in a console app.

using System;

using System.Collections;

using System.Text;

using Microsoft.Office.Server.Search.Administration;

using Microsoft.SharePoint;

namespace PN.Moss.Samples

{

    /// <summary>

    /// Console app to start Incremental Crawl

    ///

    /// Usage: StartIncrementalCrawl.exe SiteUrl "ContentSourceName"

    /// (Example: StartIncrementalCrawl.exe https://MyMossSite "MyContentSource")

  ///

    /// </summary>

    class Program

    {

        static void Main(string[] args)

        {

            try

            {

                if (args.Length < 2)

                {

                    Usage();

                    return;

                }

                Console.WriteLine("Getting Search context...");

                string strURL = args[0];

                SearchContext context;

                using (SPSite site = new SPSite(strURL))

                {

                  context = SearchContext.GetContext(site);

                }

                Content sspContent = new Content(context);

                string strContentSource = args[1];

                Console.WriteLine(string.Format("Searching content source [{0}] ...", strContentSource));

                ContentSourceCollection sspContentSources = sspContent.ContentSources;

                if (!sspContentSources.Exists(strContentSource))

                {

                    Console.WriteLine("====================================================================");

                    Console.WriteLine(string.Format("ERROR: Unable to find content source [{0}]. \nPlease provide correct content source name.", strContentSource));

                    Console.WriteLine("====================================================================");

                }

                else

                {

                    ContentSource cs = sspContentSources[strContentSource];

                    if (cs.CrawlStatus == CrawlStatus.Idle)

                    {

                        Console.WriteLine("Starting Incremental Crawl...");

                        cs.StartIncrementalCrawl();

                        Console.WriteLine("====================================================================");

                        Console.WriteLine("Incremental Crawl started Successfully.\nPlease check SSP web site for completion.");

                        Console.WriteLine("====================================================================");

                    }

                    else

                    {

                        Console.WriteLine("====================================================================");

                        Console.WriteLine(string.Format("ERROR: Unable to start incremental crawl, as crawl status is not [idle].\n\n[INFO: Current Crawl Status={0}] \n\nPlease verify and try after crawl status becomes idle.", cs.CrawlStatus.ToString()));

                        Console.WriteLine("====================================================================");

                    }

                }

               

            }

            catch (Exception e)

            {

                Console.WriteLine("=========================");

                Console.WriteLine("*** Exception Occured ***");

                Console.WriteLine("=========================");

                Console.WriteLine(e.ToString());

            }

        }

        private static void Usage()

        {

            Console.WriteLine("=========================");

            Console.WriteLine("Start Incremental Crawl");

            Console.WriteLine("=========================");

            Console.WriteLine("Usage: StartIncrementalCrawl.exe SiteUrl \"ContentSourceName\"");

           Console.WriteLine("");

            Console.WriteLine("(Ex: StartIncrementalCrawl.exe https://MyMossSite \"MyContentSource\")");

        }

    }

}