Programmatically Disabling or Enabling Site Search Visibility
March 21, 2009 1 Comment
The following code snippets are from a web part I built for a client, to assist them with generating new sites. There were new sites being created a few times a day, that all needed to follow similar configurations, but, were all based on the blank site template. Security and other things needed to be modified easily from a single, central interface, which only Site Collection Administrators could access. Each site had to follow several different business rules, and almost every one needed to apply a different set of these, so thus, the web part was created to handle this, as creating site definitions for every possible outcome of the business rules, would have required a new site definition for almost every one of the hundreds of sites that were being created.
One of the thing they needed to be able to manage from their initial configuration, is to be able to allow or not allow search visibility into that site.
The following code snippet will allow you to do just that. (web below is a reference to the specific SPWeb object). Some definition, as well as links to the WSS 3.0 SDK are below the code snippet.
1: web.AllowAutomaticASPXPageIndexing = true;
2: web.ASPXPageIndexMode =WebASPXPageIndexMode.Always;
3: web.NoCrawl = false;
4: web.Update();
By setting AllowAutomaticASPXPageIndexing to true, this will allow indexing of the ASPX pages within the site by the search crawler.
ASPXPageIndexMode specifies the type of page indexing that will occur. Check this reference in the WSS 3.0 SDK which specifies all values possible for WebASPXPageIndexMode. By setting this to Always, ASPX page indexing is always enabled.
Setting SPWeb.NoCrawl, if set to false, will allow crawling of the site.
Now, using the combination of the three above, this will allow indexing of everything, including the ASPX pages within the site to be crawled. If you wanted to crawl everything else within the site, however, did not want to crawl the ASPX pages themselves, you would use the following:
1: web.AllowAutomaticASPXPageIndexing = false;
2: web.ASPXPageIndexMode =WebASPXPageIndexMode.Never;
3: web.NoCrawl = false;
4: web.Update();
Hi Geoff,
Thanks for the wonderful blog. I am supporting MOSS 2007 right now and i need to exclude a whole site collection from search, would this code will exclude everything under the site collection from search.
Regards,
Vikas