function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion

Adding Search to Site

I am using to build a public site that displays content from a custom object. I would like to add a search box to the page that would allow users to perform a text search against the data in the custom object. How would I do this?
Since searching static content is not provided out of the box, one way to achieve this is to create a widget based on using Google site search :
I was just going to check out WidgetBox, but they actually shut down on March 28.
Tyler SchryverTyler Schryver
I've done this with a custom object. You need to include a custom controller for your visualforce page that has the search field in it. Here's the controller.
public with sharing class empsearchcontroller {  public list <employee_directory__c> emp {get;set;}  public string searchstring {get;set;}  public empsearchcontroller(ApexPages.StandardController controller) {   }     public void search(){  string searchquery='select name,id from employee_directory__c where name like \'%'+searchstring+'%\' or department__c like \'%'+searchstring+'%\' Limit 100';  emp = Database.query(searchquery);     }     public void clear(){     emp.clear();    }   }

Here's the visualforce page: 
<apex:page standardController="Employee_Directory__c" extensions="empsearchcontroller">  
  <apex:form >  
 <apex:inputText value="{!searchstring}" label="Name:"/>   
  <apex:commandButton value="Search records" action="{!search}"/>  
  <apex:pageBlock title="Search Result" >  
       <apex:outputText value="Input a first name (or part thereof) or last name (or part thereof) or department in the box above and press 'Search' "/>
       <apex:outputText value=" Search results will appear below. Click on a name to view that record."/>
    <apex:pageblockTable value="{!emp }" var="a">  
     <apex:column >  
      <apex:outputlink value="/{!}"> {!a.Name} </apex:outputlink>  
     <apex:column value="{!}"/>  

Then you'll need a second visualforce page that displays the data that is selected. In order for a public to use it, it must be used in the 'view' button. 

User-added image