How to: Delete a Content Source
In Enterprise Search, you indicate the content that you want the search service to crawl through the content sources you configure for the search service's Shared Services Provider (SSP).
The following procedure shows how to delete a new content source programmatically by using the Enterprise Search object model.
To delete a content source from the SSP's content source collection programmatically, you use the Delete method of the ContentSource object.
Note
When a content source is deleted, Enterprise Search will initiate a crawl to remove all the items in the content index for the deleted content source.
To delete a content source
Set references in your application to the following DLLs:
Microsoft.SharePoint.dll
Microsoft.Office.Server.dll
Microsoft.Office.Server.Search.dll
In your console application's class file, add the following using statements near the top of the code with the other namespace directives.
using Microsoft.SharePoint; using Microsoft.Office.Server.Search.Administration;
Add the following code to retrieve the Content object for the SSP's search context:
/* Replace <SiteName> with the name of a site using the SSP */ string strURL = "http://<SiteName>"; SearchContext context; using (SPSite site = new SPSite(strURL)) { Context = SearchContext.GetContext(site); } Content sspContent = new Content(context);
For more information about ways to retrieve the search context, see How to: Return the Search Context for the Search Service Provider.
Retrieve the value specified in the args[] parameter, which indicates the name of the content source to delete.
string strContentSource = args[0];
Retrieve the collection of content sources.
ContentSourceCollection sspContentSources = sspContent.ContentSources;
Retrieve the content source with the name matching the value of the strContentSource variable.
ContentSource cs = sspContentSources[strContentSource];
Call the Delete method of the ContentSource object.
cs.Delete();
Example
Following is the complete code for the sample console application described in this topic.
Prerequisites
- Ensure a Shared Services Provider is already created.
Project References
Add the following Project References in your console application code project before running this sample:
Microsoft.SharePoint
Microsoft.Office.Server
Microsoft.Office.Server.Search
using System;
using System.Collections;
using System.Text;
using Microsoft.Office.Server.Search.Administration;
using Microsoft.SharePoint;
namespace DeleteContentSourceSample
{
class Program
{
static void Main(string[] args)
{
try
{
/*
Replace <SiteName> with the name of a site using the SSP
*/
string strURL = "<SiteURL>";
SearchContext context;
using (SPSite site = new SPSite(strURL))
{
Context = SearchContext.GetContext(site);
}
Content sspContent = new Content(context);
string strContentSource = args[0];
ContentSourceCollection sspContentSources = sspContent.ContentSources;
ContentSource cs = sspContentSources[strContentSource];
cs.Delete();
Console.WriteLine(strContentSource + " deleted.");
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
}
}
To test this code sample, do the following:
Compile the project for the console application.
Open a command window, and navigate to the directory containing DeleteContentSourceSample.exe.
Run the following code:
DeleteContentSourceSample.exe <Name>
Note
Replace <Name> with the actual name of the content source you want to delete.
See Also
Tasks
How to: Return the Search Context for the Search Service Provider
How to: Retrieve the Content Sources for a Shared Services Provider
How to: Add a Content Source
How to: Programmatically Manage the Crawl of a Content Source
How to: Programmatically Configure a Crawl Schedule for a Content Source
Concepts
Getting Started with the Enterprise Search Administration Object Model
Content Sources Overview