How to reset SharePoint search filters for a query group

by zebsadiq 17. March 2015 15:27

There doesn’t seem to be any out of the box way of resetting SharePoint search filters by JavaScript. This is why I created the following method in order to achieve this.

SharePoint allows you to have two search listings working side by side on the same page. Obviously these would typically be used to surface different content. If you have this scenario, you probably already have set a unique value for the ‘QueryGroupName’ attribute on your search controls. In my scenario, I was trying to reset the filters for just one of these query groups from an event.  Here is the code for it:

function ClearFilterForQueryGroup(queryGroupName){
	// Store the original hash into a local variable that we will manipulate later
	var hash = window.location.hash;
	// Sets the default search parameter where there is only one
	// queryGroupName being used on the page
	var defaultSearchParameter = '#k=';
	
	// Formulates the query group's parameter in query string
	var targetParameter = '#' + queryGroupName + '=';
	// Checks to see whether the query group parameter exists in the url
	var queryGroupNameExists = (hash.indexOf(targetParameter) > -1);
	// If the group name parameter is not in the query string, it uses the default parameter
	if(!queryGroupNameExists) targetParameter = defaultSearchParameter;
	
	
	
	// if either the group name parameter or the default search parameter exist
	if(queryGroupNameExists || hash.indexOf(defaultSearchParameter) > -1 ) {		
		
		
		// Make a list of all hash paramters
		var paramArray = window.location.hash.split('#');
			
		// Loop through each parameter
		for(var i=0;i < paramArray.length;i++)
		{
			// Prepend # to the parameter as it would be removed by the split function above
			var currentParam = '#' + paramArray[i];
			// Check to see if the current parameter begins with query group name
			if(currentParam.indexOf(targetParameter) == 0)
			{
				// Simply empty the parameter value
				hash = hash.replace(currentParam, targetParameter);
				// Break out of the loop because we're only removing one paramter
				break;
			}				
		}
		
		// Change the href to new changed hash			
		window.location.href = window.location.pathname + window.location.search + hash;	
	}
}

SharePoint generates a different Url depending on whether you have one or more than one search query groups defined on the same page. Therefore my code caters for both scenarios. It assumes that the search will either be using multi query group scenario, in which case it will target a specified group, or have only one query group defined on the page which must be the correct target query group. You could alter the code to cater for different scenarios if needed.

Tags: , , ,

SharePoint 2013 | JavaScript | Cloud

Comments

Add comment




  Country flag

biuquote
  • Comment
  • Preview
Loading



Calendar

<<  October 2024  >>
MoTuWeThFrSaSu
30123456
78910111213
14151617181920
21222324252627
28293031123
45678910

View posts in large calendar