How to: Return the Search Context for the Search Service Provider

When you write code to programmatically configure Enterprise Search in Microsoft Office SharePoint Server 2007, you use the SearchContext class as the entry point to the Enterprise Search Administration object model. To configure the Enterprise Search settings at the Shared Services Provider (SSP) administration level, you must retrieve the search context of the SSP for the search service.

To access the search context, you use the GetContext method of the SearchContext class. The GetContext method has three overloads; when you call this method, you must pass one of the following as a parameter:

To access the search context using the ServerContext class

  1. In Visual Studio 2005, on the Project menu, click Add Reference.

  2. On the .NET tab, select each of the following references, and then click OK after each selection

    • Microsoft.Office.Server.dll

    • Microsoft.Office.Server.Search.dll

  3. Add the following namespace directives near the top of your code.

    using Microsoft.Office.Server;
    using Microsoft.Office.Server.Search.Administration;
    
  4. Add the following to the section of your code where you need to access the search context.

    SearchContext srchContext = SearchContext.GetContext(ServerContext.Current);
    

To access the search context using the SPSite class

  1. In Visual Studio 2005, on the Project menu, click Add Reference.

  2. On the .NET tab, select each of the following references, and then click OK after each selection

    • Microsoft.SharePoint.dll

    • Microsoft.Office.Server.Search.dll

  3. Add the following namespace directives near the top of your code:

    using Microsoft.SharePoint;
    using Microsoft.Office.Server.Search.Administration;
    
  4. Add the following to the section of your code where you must access the search context (replace yourSiteName with the name of your site).

    SearchContext srchContext;
    using (SPSite site = new SPSite("http://yourSiteName"))
    {
         srchContext = SearchContext.GetContext(site);
    }
    

    Note

    When you use the SPSite class, we recommend that you use the same approach as the code in the preceding step, to avoid memory usage issues.

    For the third overload of the GetContext method, before you call the method, you must determine the application name for the SSP providing the search service.

To access the search context using the Shared Service Provider application name

  1. In Visual Studio 2005, on the Project menu, click Add Reference.

  2. On the .NET tab, select each of the following references, and then click OK after each selection.

    • Microsoft.Office.Server.dll

    • Microsoft.Office.Server.Search.dll

  3. Add the following namespace directives near the top of your code.

    using Microsoft.Office.Server.Search.Administration;
    
  4. Add the following to the section of your code where you need to access the search context (replace SSP_GUID with a string containing the application GUID for the SSP hosting the search service).

    SearchContext srchContext = SearchContext.GetContext("SSP_GUID");
    

See Also

Concepts

Getting Started with the Enterprise Search Administration Object Model