Get query string parameter using JavaScript

Following code will help in fetching the value of a query string parameter.


If URL is http://server/page.html?id=1

Then usage would be GetQueryStringParameters(“id”) which would return 1

Remove duplicate list items from SharePoint REST call result using JavaScript

The following code snippet show how to remove duplicate list items in the JSON result of a SharePoint REST call using JavaScript.

Function Definition:

Function Usage:
In the below code, assumption is that, the REST call returns data.d.results and the column for which duplicate items need to be removed is Title


Pass multiple parameters in SetTimeout JavaScript Function

Following is a code snippet which show how to pass multiple parameters in JavaScript’s SetTimeout() function.

Refresh web part without refreshing page in SharePoint

The following code shows how to refresh web part without refreshing page in SharePoint.


Load scripts in SharePoint within custom Javascript or Workflow

Following is the code which can be used to load JavaScript in sequence.

This code for example loads the reputation.js from SharePoint’s layouts folder & jQuery from site assets.


File Icons in SharePoint Search Results using Display Template

In SharePoint 2013 search results, the icon for a file type like .msg, .txt shows up as .html icon.
In SharePoint 2010 this was overcome by mapping the icon file type in DocIcon.xml at WFE Servers.

But now since access to WFE servers are restricted in on-prem environment and no access in O-365 environment, the only solution available is to do the following.

  1. Edit the existing display template (I use custom display template with results shown in table and following is based on that) or create a new template for existing for the following located at (SiteCollection/All Files/_catalogs/masterpage/Display Templates/Search) accessible by using SharePoint Designer.
    1. xxxSearchTableResults.html
    2. xxxSearchTableItem.html
  2. Add the following codes and it should show correct icons.

Search Results Display Template:

Search Item Display Template:

ctx.CurrentItem.FileExtension always return the file extension name which seems to match with the file name in the /_layouts/15/images/ folder.

For example msg = icmsg.gif or icmsg.png

Once done, the search results will show-up as following


Execute Custom JavaScript code in SharePoint Content Editor webpart

When we try to execute a custom java script code in SharePoint content editor web part, it may not work. The reason behind is that, there might be a conflict occurring during load.

Microsoft provides ways to launch your function after full page load and following is one of the method.

Add count to drop down refiners in SharePoint search refinement webpart

While working on designing display template for drop down based refiners in SharePoint Search there was a requirement to show counts along with refiners in refiners list.

Following is the change which I made in the refiner’s display template.

Actual code

 Updated Code

Hide Available Refiners in SharePoint search refinement panel

Recently one of the customer had a strange request where the customer wanted to Hide “Available Refiners” in SharePoint search refinement panel.

The “Available Refiners” is available in “Drop Down” type refinement panel.

SharePoint Refinement Panel

When the refinement panel is being loaded, SharePoint executes a JavaScript function named AddPostRenderCallback. This would be available in the Refinement Display Template located under MasterPage/Search Gallery. The actual method looks like below code which is taken from O365.

To the above original code I made a small change so that “Clone all the elements” code executes only when user has selected a refiner.

To the above orignal code I added the following code to hide the “Available Refiners” option.

The above code will hide the “unSelGroup”‘s “Option Group” HTML to hide the Options for “Available Refiners”.

Final code would look like below.

End Result is following
Hidden Available Refiners Option Group

Clear SharePoint Search Results

Clear SharePoint Search Results

Recently I had a requirement for OOTB search Box + Result where the customer wanted to clear the search results regardless of any refinement selected or not.

Clear SharePoint Search Results

To implement this, in the display template HTML the following was added.

Do note that if refinements are there, the commented single line of code didn’t work and I had to replace the entire # as blank. Do check the original source (mentioned below) for more information.

The source for the above code is Add a “Clear Filters” link to your search page in SharePoint 2013