Skip to Content

Custom KM Search with KM APIs

Applies to:

SAP Enterprise Portal and Knowledge Management (7.01, all SP levels) to which this article or code sample applies.


To develop KM Search application based on custom/company specific look and feel using CSS without impacting standard portal look and feel.

Table of Contents

  1. Introduction.

  2. Core Search Functionality.

  3. Using JSP Dynpage Component



  4. UI Representation: Search Result.

  5. Importing CSS files in JSP code.

  6. Utilizing the CSS classes while displaying the content

  7. Related Content

  8. Copyright


Sometimes, the SAP’s standard search results will not suite the requirements especially when it comes to the UI part of it. This document explains the way how to utilize the SAP provided KM APIs that helps in achieving the custom search result and displaying the result it based on the custom defined CSS to achieve the good look and feel.

Let’s consider the whole process in the below steps.

  1. Core Search Functionality.
  2. Using JSP Dynpage component.
  3. UI representation of the search result.

Core Search Functionality

The KM Indexmanagement API is the central search service in the Knowledge Management application of SAP NetWeaver Enterprise Portal. It can manage several search engine’ integrations through its index service concept. The standard index service provided by SAP is the TREX index service, which enables the SAP NetWeaver Enterprise Portal to search in KM repositories.

For using the search functionality from a client point of view the KM Indexmanagement API can be used in UI components like PortalComponents, WebDynpro UIs or Servlets. A typical flow of a single request will look like the following diagram:

The required APIs for executing a search with the Indexmanagement Service is the IFederatedSearch interface which combines a search with all activated index services implementations. The IFederatedSearch interface offers the possibility to execute the search either via search() or searchWithSession() method (recommended by SAP). The searchWithSession() method offers a paging mechanism which allows you to retrieve search results from the search engine in a limited request list if there are too many hits in the search engine, e.g. results from start count to end count.

Using JSP Dynpage Component

Considering JSP Dynpage component, as we need to build a CSS based search application.


As this is the first method to be executed, consider writing the code to set the initial setup code of the search.

The basic entity to point the KM repository is Index. So get the index of the repository service which you would like your code to search.

Setup the valid KM Resource Context, which is required to execute the search.

IResourceContext ctxt = ResourceFactory.getInstance().getServiceContext(“cmadmin_service”);

The search functionality in KM Indexmanagement is accessible through the IFederatedSearch interface, which can be retrieved out of the Indexmanagement service instance.

IIndexService indexService = (IIndexService) ResourceFactory.getInstance().getServiceFactory().getService(IServiceTypesConst.INDEX_SERVICE);

The IFederatedSearch interface allows you to search over all active index services of the KM Indexmanagement

IFederatedSearch federatedSearch = (IFederatedSearch) indexService.getObjectInstance(IWcmIndexConst.FEDERATED_SEARCH_INSTANCE);

In the next step the query which is passed through the Indexmanagement must be built up. Therefore you can use the simple factory class SearchQueryListBuilder of standard KM.

SearchQueryListBuilder sqb = new SearchQueryListBuilder();

  1. sqb.setSearchTerm(searchString);

This query list builder class takes care of the syntax validity of the created query entry objects. The queries constructed by the SearchQueryListBuilder are KM Indexmanagement compliant queries which are later passed to every index service implementation.

List indexList = new ArrayList();
if (index != null && index.length() > 0) {
   // index variable contains the id of the index.
} else {
   indexList = indexService.getActiveIndexes();

The indexList contains the exact index of the repository service to be searched. If the search is to be run on multiple indexes, then get all active indexes of a index service you can simply call.

  1. indexService.getActiveIndexes();

After the successful construction of the query entry list, the execution of the search can be done by simply calling the method.

ISearchSession session = federatedSearch.searchWithSession(qel, indexList, ctxt);

The searchWithSession() method is recommended to execute a search on the KM indexmanagement. This method ensures that only the requested results instead of all results are retrieved by the index service implementation later on.

You can get a list of the search result items out of the search session object. The requested results can be limited by a count between 1 and session.getTotalNumberResultKeys() which is the maximum number of results retrieved for the specified query entry list.

sessionCnt = session.getTotalNumberResultKeys();
ISearchResultList searchList = session.getSearchResults(1, sessionCnt);

For performance reasons, it is recommended to retrieve only a subset of the search result items instead of all.

To get each result out of the search results list, you can simply iterate through the number of search results and gather the resource object.

ISearchResultListIterator iter = results.listIterator();
while (iter.hasNext()) {
    ISearchResult result =;
    IResource resource = result.getResource();       

Besides the resource object, additional search-relevant information can be retrieved out of a single result item like rank value, a content snippet or search-specific properties.


Once the resultant search list is looped through and the exact resource object is obtained, the values of the metadata properties can be obtained, as below.

IPropertyName iRating = new PropertyName("", "rating");
if (iRating != null) {
   IProperty iPropertyRating = iProp.get(iRating);
   if (iPropertyRating != null) {
          ratingValue = iPropertyRating.getIntValue();

Where the is the standard namespace defined by the SAP and all the metadata property elements are referred through this namespace.

The obtained values can be put into the bean object created, through getter and setter methods.


As this is the final method which runs in the life cycle of the JSP Dynpage application, we can set the bean object to request object. Thus the same bean can be utilized in JSP page to get the resource metadata property values.

((IPortalComponentRequest) getRequest()).getServletRequest().setAttribute("myBean", myBean);

The next step is to render the obtained search result to the UI through JSP.

UI Representation: Search Result.

The standard UI representation will not be sufficient many of the times. So to accomplish custom UI representation which has rich look and feel, we can utilize the CSS files.

Importing CSS files in JSP code

First the CSS files have to be added to the application, and this can be done in two ways,

  1. Add the CSS files under the CSS folder of the portal application.
  2. Add the CSS files under KM folder.

If there is any change in CSS file, the portal application has to be deployed every time, KM approach is better, as it points to the updated KM file.

The CSS files can be imported, as below.

<link rel=”stylesheet” type=”text/css” href=”/irj/go/km/docs/documents/CSS-Folder/css/cssfilename.css”/>

Utilizing the CSS classes while displaying the content

The centrally defined CSS classes can be used to display the UI elements.

For instance, let consider the utilization of a button,


    <ul class=”hButtons fLeft”>

        <li class=”normalBtn”><a href=”<%=link1%>”>Button 1</a></li>

        <li class=”normalBtn”><a href=”<%=link2%>” target=”_self”>Button 2</a></li>

        <li class=”normalBtn”><a href=”<%=link3%>” target=”_blank”>Button 3</a></li>



Here the buttons are arranged in a sequence using the <ul> and <li> tags of the html elements.

And through classes normalBtn, the graphical part(colour, size) is handled.


Related Content

To report this post you need to login first.

Be the first to leave a comment

You must be Logged on to comment or reply to a post.

Leave a Reply