in

SharePoint Blogs

The Best Place for SharePoint-related Blogs

This Blog

Syndication

Mirrored Blogs

Create Search Crawl Rules Programmatically

A few days ago, I mentioned that we're trying to automate as much as possible in our new SharePoint based product, and I explained how we are creating custom search scopes in a Feature Receiver.

Another component we're automating is the creation of Crawl Rules for the search engine in the SSP the site collection is associated.

According to MSDN crawl rules are defined as follows:

Crawl rules provide you with the ability to set the behavior of the Enterprise Search index engine when you want to crawl content from a particular path. By using these rules, you can:

  • Prevent content within a particular path from being crawled. 

  • Indicate that a particular path that would otherwise be excluded from the crawl should be crawled.

Here's how we're creating the crawl rules in code using a similar Feature Receiver as before:

using System; using System.Collections.Generic; using System.Text; using Microsoft.SharePoint; using Microsoft.Office.Server.Search.Administration; using System.Diagnostics; public class FeatureReceiver
: SPFeatureReceiver { SPSite currentSite = null; public override void FeatureActivated(SPFeatureReceiverProperties
properties) { //Debugger.Launch(); this.currentSite
= properties.Feature.Parent as SPSite; createCrawlRules();
} private void createCrawlRules()
{ //get the site SSP's search server instance SearchContext
searchContext = SearchContext.GetContext(this.currentSite); //For
this example, I'll just show how to exclude the Explorer view of a list //Notice
how we're using wildcard characters to specify that every /Forms/WebFldr.aspx page //
will be excluded regaurdless of protocol or location in the site collection string crawlRule
= " *://*/Forms/WebFldr.aspx"; //get
the content source for the search server Content sspContent = new Content(searchContext); //check
to see if the crawl rule exists, and create it if it doesn't if (!sspContent.CrawlRules.Exists(rule))
{ 

//create the crawl rule, setting the type of crawl rule and the crawl rule string CrawlRule crawlRule = sspContent.CrawlRules.Create(CrawlRuleType.ExclusionRule, rule); //we want to make sure that the rule still works if there are querystring variables crawlRule.FollowComplexUrls = true; //at this point, you can set other properties such as authentication information to crawl // content using a different identity or authentication method // for more info , check out http://technet.microsoft.com/en-us/library/cc263150(TechNet.10).aspx //commit the rule in the database crawlRule.Update(); } }

public override void FeatureDeactivating(SPFeatureReceiverProperties properties){}

public override void FeatureInstalled(SPFeatureReceiverProperties properties){}

public override void FeatureUninstalling(SPFeatureReceiverProperties properties){}

}

Read the complete post at http://feeds.feedburner.com/~r/RichFinn/~3/315628967/CreateSearchCrawlRulesProgrammatically.aspx


Need SharePoint Training? Attend a SharePoint Bootcamp!

Posts (c) their respective authors. Everything else (c) 2007 SharePoint Experts