I needed to run a few utilities following a site migration from SP2007 to SP2010, so naturally I wrote a script to string together all the operations, beginning with Mount-SPContentDatabase.

One or two of those utilities - console apps I've written - worked as expected when run independently. But when run inside the script, they inconsistently reported little or no work for them to do (i.e. when run independently, one would report 18 pages needing updating, but within the script, it would sometimes report only two pages needing updates, and other times even zero pages).

After a couple of days' head-scratching, I ran a SQL Trace to find out what was happening.

It turns out that some of the database tables - notably dbo.AllWebParts - are NOT populated by Mount-SPContentDatabase: they're actually populated by the first ensuing incremental crawl. Of course, it's harder to detect when that incremental crawl is finished.

Has anyone else here seen this, or even better, found a way to programmatically detect when an incremental crawl finishes?


1 Answer 1


The easiest crawl to track is the one you initiate yourself.

The SP2010-specific code looks like this:

SearchService ss = new SearchService("OSearch14", SPFarm.Local);
SearchServiceApplication ssap = null;
bool bNotFinished = true;

// for me, the first SearchServiceApplication I find will suffice.
// for a more sophisticated environment, you need to choose from several possible 
// providers depending on circumstances, etc.
foreach (SearchServiceApplication ssa in ss.SearchApplications)
    ssap = ssa;
    Helpers.Trace(String.Format("    ... Discovered search service application:'{0}', 
                   Provider:'{1}'", ssap.Name, ssap.DefaultSearchProvider));

Content content = null;
ContentSource cs = null;

    content = new Content(ssap);
    cs = content.ContentSources[ContentSource];
catch (Exception ex) 
    Helpers.Trace(String.Format("\n    Exception encountered while trying to start incremental crawl: {0}", ex.Message));

while (cs.CrawlStatus == CrawlStatus.Idle)

if (cs.CrawlStatus != CrawlStatus.CrawlStarting && 
    cs.CrawlStatus != CrawlStatus.CrawlingIncremental)
    Helpers.Trace(String.Format("    .... for some reason, crawl did not start"));

Helpers.Status(String.Format("    ... Crawl started ... waiting for it to finish "));
while (bNotFinished)
    if (cs.CrawlStatus == CrawlStatus.Idle)
        bNotFinished = false;


For more on accessing search service applications, see the post by Steve Peschka on Getting Search Service Applications and Proxies in SharePoint 2010 Object Model

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.