Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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?

Josh

share|improve this question

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));
    break;
}

Content content = null;
ContentSource cs = null;

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

while (cs.CrawlStatus == CrawlStatus.Idle)
{
    Thread.Sleep(100);
}

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

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

    Helpers.Spin();
    Thread.Sleep(200);
}

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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