Programmatically Disabling or Enabling Site Search Visibility

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(); 



About Geoff Varosky
Geoff Varosky is a Senior Architect for Insight, based out of Watertown, MA. He has been architecting and developing web based applications his entire career, and has been working with SharePoint for the past 15 years. Geoff is an active member of the SharePoint community, Co-Founder and Co-Organizer of the Boston Area SharePoint Users Group, co-founder for the Boston Office 365 Users Group, co-organizer for SharePoint Saturday Boston and speaks regularly at SharePoint events and user groups.

One Response to Programmatically Disabling or Enabling Site Search Visibility

  1. Vikas says:

    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.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: