Archive for the ‘SharePoint 2013 Search’ Category

SharePoint online / SharePoint 2013: Sharing variable values between search control and display templates

November 29, 2017 Leave a comment

Recently I came across to a scenario where I need to set the variable in control template and the value of the variable should be accessible in item template and vice versa.
I was showing some data in item templates and using third party pagination for displaying the search result, needed the value of pagination size which is available in control template.
I tried with normal JavaScript variable but due to the scoping in display template it was not available for me inside the item template.
Solution 1:
We can use Windows.variable which is nothing but global variable creation on the scope of the window object and hence will be accessible throughout the scope of windows object.
Control Template:

<!--#_ window.PageSize = "30"; _#-->

Item Template:

<!--#_ alert(window.PageSize); _#-->

Solution 2:
Another better alternative is to use the built-in ctx object which is already shared between the control and item display template.
Control Template:

<!--#_ ctx.PageSize = "30"; _#-->

Item Template:

<!--#_ alert(ctx.PageSize); _#-->

Happy learning.

SharePoint 2013: Creating and calling custom JavaScript function in search display template

October 23, 2017 Leave a comment

Hi Friends,

Recently I came across a requirement where I need to create a custom JavaScript function which will change the date formatting on the provided string date parameter.

I tried creating normal JavaScript function in display template and tried calling it from other part of template but the function was undefined and not found inside the display template, I got little bit confused as I have created the JavaScript function in same display template but still not able to find it in same display template while calling. We can have couple of ways to resolve the issue.

Solution 1:

After some googling I found the cause of the issue and the solution as well, as follows.

The possible reason is scoping of the display template not allowing to consider external JavaScript function as a part of the scope to call inside display template.

The solution is to create new function on window scope and the call it in the desired place inside display template.

window.MyTestFunction = function(myParam)
// your own logic to work on the param 'myParam'

Calling the function:

<a style="cursor:pointer" onclick='window.MyTestFunction(3)'>Call My custom JS function</a>

Solution 2:

  1. Create a JavaScript file (MySampleJS.js) in the Display Templates/Search folder.
  2. Add the custom JS function in to the MySampleJS.js file.
  3. Add the following code into thetag below to include the JS file in display template
$includeScript(this.url, "~sitecollection/_catalogs/masterpage/Display Templates/Search/ MySampleJS.js");

Happy learning.

SharePoint 2013: Friendly URL’s in Publishing HTML Field is broken and showing FIXUPREDIRECT.ASPX as hyperlink in display template

June 12, 2014 4 comments

Hi Friends,

I came across a weird scenario recently, thought to share here so that it will be helpful for some of our friends.


In one of the article page we were having a Publishing HTML Field, content author added relative friendly URL as a hyperlink inside the field.  I.e. help/ithelp/lync

This page was supposed to be crawled and this Publishing HTML Field’s search managed property was used to display the column data in a search display template in content search web part (CBS).

The result displayed in content search web part was perfect except some of the hyperlinks are pointing to wrong pages.

The URL’s were pointing to FIXUPREDIRECT.ASPX page with some GUID’s as query string like following.


Analysis of the issue:

After analysing the issue for couple of hours found that it is something related to managed metadata terms as the query string formed had termsetid and termId as query sting parameters.

Also we found that only the friendly URL’s ware broken. The magic happening while the page is getting saved and SharePoint tries to resolve the friendly URL with available terms and replacing the URL with FIXUPREDIRECT.ASPX URL and same gets crawled. It is nothing to do with search display template.


We tried to find out the solution to resolve this issue but no luck; we have tried workaround to replace the friendly relative URL with non-friendly URL and it worked for us.

help/ithelp/lync   TO   help/ithelp/lync.aspx


SharePoint 2013: Display template in content search web part not retaining selection

March 27, 2014 3 comments

Scenario: While adding content search web part in a page or page layout using all user web part. The selected display template while exporting web part is not retained the selection of custom display template and default display template gets selected if we create new page out of the page layout or page. I have verified couple of times in all user web part and it is correctly pointing to the display template like following-

~sitecollection/_catalogs/masterpage/Display Templates/Content Web Parts/Test_Item_Contacts.js


Solution:  Found out the problem with ~ symbol and solution is to replace it with ASCII equivalent & # 126 ; (Remove space)

~sitecollection/_catalogs/masterpage/Display Templates/Content Web Parts/Test_Item_Contacts.js


SharePoint 2013: Content Search web part query having query variables with multiple values

February 12, 2014 Leave a comment

Hi Friends,

While working with one of the SharePoint 2013 content search web part, I came across a query where query variables may return multiple values which causing my search query to fail, after struggling a lot I found a good TechNet article which solved my issue. Putting the solution here so that it will help someone from developer community.

For demo purpose I have created a Term set like following


I have a user profile property (MyProfileProperty) which is bound to above term set. I have selected the profile property values for current logged in user.


I have created a site column (MyTaxonomyColumn) which is bound to same term set and added that column in a list. After adding some entries in that list, I did a full crawl so that a managed property for the column will be created (owstaxIdMyTaxonomyColumn).

My requirement is to get all records from list having values from user profile property ‘MyProfileProperty’.

I have added a query like   {|owstaxIdMyTaxonomyColumn:{User.MyProfileProperty}}


This query gets transformed as following and worked for me as the above query syntax combines all the query variable values will be combined by using the bitwise OR operation.


SharePoint: issue with $top in ListData.svc

September 10, 2013 2 comments

Hi Friends,

While working on SharePoint REST API’s in SP 2013 I come across a scenario where I need to filter on date column, I found solution to use /_vti_bin/ListData.svc service to filter based on date column like following, we can’t use RESTful call in date filtering –

var date = new Date();

var stringUrl=_spPageContextInfo.webAbsoluteUrl+"/_vti_bin/ListData.svc/EventsCalendar?$filter=StartTime+ge+datetime'"+date.toISOString()+"'&$select=Title,Description,Path,Id,StartTime&$orderby=StartTime&$top=3";

When I tried this I was getting data in browser but not using JavaScript code which is using $.ajax call to get and read the response.

After analyzing I found that $top is the culprit in my case and found following workaround.

When we use $top we will get the result date in ‘data.d’ like following-

var resultsArray = data.d;

If you want old JS code ‘data.d.results’ to work as is with $top we need to use $inlinecount=allpages in URL call like following

var date = new Date();

var stringUrl=_spPageContextInfo.webAbsoluteUrl+"/_vti_bin/ListData.svc/EventsCalendar?$filter=StartTime+ge+datetime'"+date.toISOString()+"'&$select=Title,Description,Path,Id,StartTime&$orderby=StartTime&$top=3&$inlinecount=allpages";

Happy coding….

SharePoint 2013: Avoid default results in search result and content search web part with search box

August 9, 2013 14 comments

Hi Friends,

I faced one more issue recently related to SharePoint 2013 search, that I thought to share with you.


I have ‘Search Box’ web part and ‘Content Search’ web part on a page, search box is sending query keyword to content search web part to display results. Content search web part has couple of additional queries as following screen shot showing.


This query returning the results always and thus showing me the results before even started searching using search box web part, as shown in following image.


I wanted content search web part should show the results only when someone searches for it in search box, else results should not be displayed.


I have modified the Content Search web part’s query like following, which given the expected results.


So after the query changes by default no results are displayed and I am getting results only when I searched using search box web part, as shown in following images.



The same changes will be applicable if you are using search results web part.

Hope this article will helpful for some of our friends, if any other good alternative for this found please let me also know about it.