Export (0) Print
Expand All
Expand Minimize

Creating Content Sources to Crawl Business Data in SharePoint Server 2007 Enterprise Search

SharePoint 2007

Summary: Learn how to create content sources to crawl business data in Microsoft Office SharePoint Server 2007 Enterprise Search.

Office Visual How To

Applies to: 2007 Microsoft Office System, Microsoft Office SharePoint Server 2007

Patrick Tisseghem, U2U

April 2007

Overview

Think of a content source as a location containing resources that you want to crawl or index. In Microsoft Office SharePoint Server 2007, many types of locations are accessible by default: SharePoint sites, Web sites, network folders, Microsoft Exchange Server public folders, and data exposed by using the Business Data Catalog. In this how-to we'll focus on data exposed by using the Business Data Catalog, and discuss the steps to take when creating and configuring a content source of type Business Data. We'll also review a sample of how to accomplish the steps programmatically, using some of the classes exposed in the new search administration API.

Code It

IDEnumerator in the Application Definition File

Indexing entity instances requires an additional method at the level of the entity in the application definition file. This method must be of type IDEnumerator and must return primary keys for the instances.

<Method Name="EmployeeIDEnumerator">
   <Properties> 
      <Property Name="RdbCommandType" Type="System.String">
         Text
      </Property>
      <Property Name="RdbCommandText" Type="System.String">
         Select EmployeeID from HumanResources.vEmployee
      </Property>
   </Properties>
   <Parameters>
      <Parameter Direction="Return" Name="EmployeeIDs">
         <TypeDescriptor TypeName="System.Data.IDataReader, System.Data, 
           Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" 
           Name="Employees" IsCollection="true">
            <TypeDescriptors>
               <TypeDescriptor TypeName="System.Data.IDataRecord, 
                 System.Data, Version=2.0.3600.0, Culture=neutral, 
                 PublicKeyToken=b77a5c561934e089" Name="Employee">
                  <TypeDescriptors>
                     <TypeDescriptor TypeName="System.Int32" IdentifierName=
                       "EmployeeID" Name="EmployeeID" />
                  </TypeDescriptors>
               </TypeDescriptor>
            </TypeDescriptors>
         </TypeDescriptor>
      </Parameter>
   </Parameters>
   <MethodInstances>
     <MethodInstance Name="EmployeeIDEnumeratorInstance" Type="IdEnumerator" 
       ReturnParameterName="EmployeeIDs" />
     </MethodInstances>
</Method>

The application definition file is imported in the Business Data Catalog metadata repository using the administration site of the Shared Services Provider. A content source of type Business Data can then be created that points to the available business data application.

Required Search Administration and Business Data Catalog References

Developers can create applications that programmatically perform all of the steps administrators take in the browser. Notice that the sample code must be executed on the computer running Office SharePoint Server, and requires references to Microsoft.SharePoint.dll, Microsoft.Office.Server.dll, Microsoft.Office.Server.Search.dll, Microsoft.SharePoint.Portal.dll, and System.Web.dll. The following code example shows the namespaces that are used.

// -- Namespaces used for the search administration classes
using Microsoft.Office.Server;
using Microsoft.Office.Server.Administration;
using Microsoft.Office.Server.Search;
using Microsoft.Office.Server.Search.Administration;
// -- Namespaces used to access the Business Data Catalog
using Microsoft.Office.Server.ApplicationRegistry.MetadataModel;
using Microsoft.Office.Server.ApplicationRegistry.Infrastructure;
using Microsoft.Office.Server.ApplicationRegistry.SystemSpecific.Db;

Connecting to the Shared Services Provider and the Search Context

Before you can work with the classes to manipulate the content sources, you must obtain the reference to the context of the Shared Services Provider and the Search Service. The following code example shows this, and assumes that the name of the Shared Services Provider is SharedServices1.

private ServerContext serverctx = null;
private SearchContext searchctx = null;
private void Form1_Load(object sender, EventArgs e)
{
  serverctx = ServerContext.GetContext("SharedServices1");
  searchctx = SearchContext.GetContext(serverctx);
}

Listing Existing Content Sources

You can retrieve the list of content sources by creating an instance of the Content class with the reference of the SearchContext as an argument in the constructor. Next, loop over all of the content sources for display. Every content source has one or more start addresses.

Content content = new Content(this.searchctx);
foreach (ContentSource contentsource in content.ContentSources)
{
  TreeNode node = treeViewContentSources.Nodes.Add(contentsource.Name);
  node.Tag = contentsource;
  foreach (object startaddress in contentsource.StartAddresses)
  {
     node.Nodes.Add(startaddress.ToString());
  }
}

Retrieving Crawling Status

Several properties that are exposed at the level of the ContentSource class store information about the crawling status and timing.

ContentSource contentsource = (ContentSource)node.Tag;
labelCrawlStatus.Text = contentsource.CrawlStatus.ToString();
labelCrawlStarted.Text = contentsource.CrawlStarted.ToString();
labelCrawlCompleted.Text = contentsource.CrawlCompleted.ToString();

Starting a Crawl

Two crawl methods are available: full crawl and incremental crawl. Both are exposed as methods of the ContentSource class.

contentsource.StartFullCrawl();
contentsource.StartIncrementalCrawl();

Retrieving the Business Data Applications

All of the functionality of the Business Data Catalog is exposed by a rich object model (the Microsoft.Office.Server.ApplicationRegistry namespace in Microsoft.SharePoint.Portal.dll). If the code is not running in the context of SharePoint Server, an instance of the SqlSessionProvider and a call to the SetSharedResourceProviderToUser method internally hooks up the application with the Shared Services Provider context. The ApplicationRegistry class exposes a GetLobSystemInstances method with all of the business data applications available in the Business Data Catalog.

SqlSessionProvider.Instance().SetSharedResourceProviderToUse
  ("SharedServices1");
NamedLobSystemInstanceDictionary instances = 
  ApplicationRegistry.GetLobSystemInstances();
foreach (LobSystemInstance app in instances.Values)
{
   comboBoxBDCApps.Items.Add(app.Name);
}

Creating a Content Source

To create a content source, you call the Create method at the level of the ContentSourceCollection object. You can create a Business Data Catalog-specific URI to the business data application by calling the static method ConstructStartAddress exposed by the type BusinessDataContentSource. This URI is added to the StartAddresCollection object of the ContentSource instance. A call to the Update method saves everything in the database.

Uri bdcuri = BusinessDataContentSource.ConstructStartAddress
   (this.searchctx.Name, comboBoxBDCApps.SelectedItem.ToString());
Content content = new Content(this.searchctx);
BusinessDataContentSource contentsource = 
  (BusinessDataContentSource)content.ContentSources.Create
  (typeof(BusinessDataContentSource),textBoxContentSourceName.Text);
contentsource.StartAddresses.Add(bdcuri);
contentsource.Update(); 

Read It

The crawler that is provided in SharePoint Server can be directed to a location and ordered to index content available in that location by creating and configuring a content source at the level of the Shared Services Provider. Following are the different types of content sources that can be created:

  • SharePoint sites

  • Web sites

  • Network folders

  • Exchange Server public folders

  • Business data

  • Lotus Notes databases (only after a post-installation step)

Business data stored in a structured way in a relational database such as Microsoft SQL Server, or stored in line-of-business (LOB) systems such as SAP or Microsoft Dynamics CRM can be exposed in a unified and consistent way via the Business Data Catalog middle-layer. Developers model the business data in a declarative way in an XML file: the application definition file. A full discussion of all of the elements in this file is out of scope here, but one method is very important and must be included in the application definition file for the crawler to index the data. This method must be of type IDEnumerator and return all of the primary keys of the records to index. The crawler generates a profile page for each of them and indexes the content on that page.

The search administration object model exposes different classes you can use to programmatically create, configure, and manage content sources. Figure 1 shows all of the classes used within the sample code.

Figure 1. The search-related classes discussed in this how-to

The search-related classes discussed
See It

Video for MOSS 2007 Creating Content Sources

Watch the Video

Length: 14:27 | Size: 14.9 MB | Type: WMV file

Explore It

Community Additions

ADD
Show:
© 2014 Microsoft