Show TOC

Procedure documentationImplementing a Custom Suggestion Provider Locate this document in the navigation structure

 

The Suggestion Provider API enables you to add and control suggestions displayed when the user enters a search term in the Search field of the Ajax Framework Page (AFP).

The API is contained in the com.sap.portal.suggestion.providers package.

Prerequisites

  • SAP NetWeaver Developer Studio is installed and started

  • In the portal's Framework Page Configuration for Ajax Framework Page, the Enable Quick Launch option is selected.

    For more information about enabling this option, see Creating and Editing a Framework Page.

Procedure

The following procedure describes how to implement and integrate a basic suggestion provider that returns a list of suggestions for a search term.

  1. In SAP NetWeaver Developer Studio, open the Enterprise Portal perspective and create a portal application project.

    For more information, see Creating a Portal Application Project.

  2. In portalapp.xml, set a sharing reference to com.sap.portal.suggestion.providers.

  3. In the portal application project, create a portal service, and add the startup property, setting it to true.

    For more information, see Creating a Portal Service.

  4. In the src.core folder, create a new class named SampleCategory that implements the ICategoryDescriptor interface.

    Example Example

    1. public class SampleCategory implements ICategoryDescriptor 
      {
      	public String getID() 
      	{
      		return "Travel";
      	}
      
      	public String getTitle(Locale locale) 
      	{	
      		return "Travel";
      	}
      }
    End of the code.
  5. In the src.core folder, create a new class named SampleProvider that implements the ISuggestionProvider interface.

    Example Example

    1. public class SampleProvider implements ISuggestionProvider 
      {
      	public ICategoryDescriptor getCategory() 
      	{	
      		return new SampleCategory();
      	}
      
      	public List<ISuggestion> search(String searchTerm, IUser user, Locale locale, ISuggestionFilter filter) 
      	{
      		String term = searchTerm.toLowerCase(locale);
      		String[] words = {"photograph", "seaside", "rock", "sandstone", "sunset"};
      		List<ISuggestion> suggestions = new ArrayList<ISuggestion>();
      
      		for (int i = 0; i < words.length; i++)
      		{
      			SearchOption searchOption = filter.getSearchOption();
      			String word = words[i].toLowerCase(locale);
      			if ( ( (searchOption == null) || (searchOption == SearchOption.contains) && word.contains(term) ) || word.startsWith(term) ) 
      			{
      				Suggestion suggestion = new Suggestion();
      				suggestion.setTitle(word);
      				suggestion.setTooltip(word);
      				suggestion.setUrl("http://www.google.com");
      				suggestion.setSuggestionProvider("ProviderId");
      				suggestions.add(suggestion);
      			}
      		}
      		return suggestions;
      	}
      }
      
    End of the code.
  6. To register the service, copy the following code to the init method of the service implementation class:

    Example Example

    1. ISuggestionProviderRegistration providerService = (ISuggestionProviderRegistration) PortalRuntime.getRuntimeResources().getService(ISuggestionProviderRegistration.KEY);
      
      ISuggestionProvider sampleProvider = new SampleProvider();
      providerService.register(sampleProvider);
      
    End of the code.

    To unregister the service, copy the following code to the destroy method of the service implementation class:

    Example Example

    1. ISuggestionProviderRegistration providerService = (ISuggestionProviderRegistration) PortalRuntime.getRuntimeResources().getService(ISuggestionProviderRegistration.KEY);
      
      providerService.unregister(sampleProvider);
      
    End of the code.
  7. Build and deploy your application. For more information, see Deploying a Portal Application.

  8. Enter a term in the Search field in the portal, and check that the Travel category with suggestions appears below.

More Information

Suggestion API

A client-side API that enables you to retrieve search suggestions and control their appearance