+ Start a Discussion
cory mcory m 

search box that redirects to CRM Content search page

I'm trying to put a simple text search box on a VF page that, when submitted, will redirect the user to the CRM Content search page along with the search term.  I can "hack the URL" manually to accomplish this.  For example, if I put /sfc/#search?searchTerm=blah in my browser, it behaves correctly.  however, I can't seem to get a visualforce page to redirect similarly.

 

Here's my controller code:

 

public with sharing class SalesPlaybookHomeController {

	public String searchString {get;set;}
	
	private final String path = '/sfc/#search';
	
	public PageReference search() {
        String queryString = '';
       //build the query string
        if (searchString != null && searchString.length() > 0){
            queryString = 'searchTerm=' + EncodingUtil.urlEncode(searchString, 'UTF-8');
        }
        PageReference p = new PageReference(path + '?' + queryString);
        p.setRedirect(true);
        return p;
    }
}

 

 

When the code executes, it redirects to the content page, but the query string is not there, and so the searchTerm is not passed correctly.

 

Is there a different way to formulate the URL in order to effect a CRM Content search?  Other ideas?

 

Thanks in advance,
Cory

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

Interesting one this.  It appears to be the anchor tag (the #search) in the URL that is giving the PageReference a problem.  I've tried adding the search term in as a parameter to the page reference, but then the #search gets moved to the end of the query string.

 

The only way I've been able to get this to work is to carry out the redirection from the page itself using javascript as shown below:

 

Controller

 

 

public with sharing class SalesPlaybookHomeController {

	public String searchString {get;set;}
	public String executeSearchString {get; set;}
	
	private final String path = '/sfc/#search';
	
	public PageReference search() {
        String queryString = '';
       //build the query string
        if (searchString != null && searchString.length() > 0){
            queryString = 'searchTerm=' + EncodingUtil.urlEncode(searchString, 'UTF-8');
        }
        executeSearchString = path + '?' + queryString;
        
        return null;
   }
}

 

So rather than redirecting, the executeSearchString property is set to the full URL.

 

 

Page

 

 

<apex:page controller="SalesPlaybookHomeController">
   <apex:outputPanel >
      <apex:outputPanel rendered="{!NOT(ISBLANK(executeSearchString))}">
      	 <script>
      	   window.location.href="{!executeSearchString}";
      	 </script>
      </apex:outputPanel>
   </apex:outputPanel>
   <apex:form >
     Enter search term <apex:inputText value="{!searchString}"/><br/>
     <apex:commandButton value="Go" action="{!search}"/>
   </apex:form>
</apex:page>

 

 

Then in the page, if the executeSearchString is defined, the window location href is changed to the search string, which preserves the anchor tag in its correct place.

 

Hope this helps.

 

All Answers

bob_buzzardbob_buzzard

Interesting one this.  It appears to be the anchor tag (the #search) in the URL that is giving the PageReference a problem.  I've tried adding the search term in as a parameter to the page reference, but then the #search gets moved to the end of the query string.

 

The only way I've been able to get this to work is to carry out the redirection from the page itself using javascript as shown below:

 

Controller

 

 

public with sharing class SalesPlaybookHomeController {

	public String searchString {get;set;}
	public String executeSearchString {get; set;}
	
	private final String path = '/sfc/#search';
	
	public PageReference search() {
        String queryString = '';
       //build the query string
        if (searchString != null && searchString.length() > 0){
            queryString = 'searchTerm=' + EncodingUtil.urlEncode(searchString, 'UTF-8');
        }
        executeSearchString = path + '?' + queryString;
        
        return null;
   }
}

 

So rather than redirecting, the executeSearchString property is set to the full URL.

 

 

Page

 

 

<apex:page controller="SalesPlaybookHomeController">
   <apex:outputPanel >
      <apex:outputPanel rendered="{!NOT(ISBLANK(executeSearchString))}">
      	 <script>
      	   window.location.href="{!executeSearchString}";
      	 </script>
      </apex:outputPanel>
   </apex:outputPanel>
   <apex:form >
     Enter search term <apex:inputText value="{!searchString}"/><br/>
     <apex:commandButton value="Go" action="{!search}"/>
   </apex:form>
</apex:page>

 

 

Then in the page, if the executeSearchString is defined, the window location href is changed to the search string, which preserves the anchor tag in its correct place.

 

Hope this helps.

 

This was selected as the best answer
cory mcory m

Thanks , great suggestion.  Ideally I think salesforce should "fix" this issue, but doing the redirect in javascript is acceptable.  I may try moving all the logic in to javascript to avoid the extra trip to the server....

MANATTWebMANATTWeb

Man - this really helped. Thanks for posting!

 

EDIT: In my particular implementation, this seems to fail in IE on the first search. If you submit it a second time, then it just sends the user to the Content search page with no parameters.

 

Thoughts???