Archive

Posts Tagged ‘Search’

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.

Advertisements

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
<script>
$includeScript(this.url, "~sitecollection/_catalogs/masterpage/Display Templates/Search/ MySampleJS.js");
</script>

Happy learning.

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

before_change

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

After_change

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

TermSet

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.

User_Profile_Property

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}}

Search_Query

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.

Query_Transform

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.

Scenario:

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.

Search_Box_1

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.

Search_Box_2

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.

Solution:

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

Search_Box_3

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.

Search_Box_4

Search_Box_5

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.

SharePoint 2013 Remove ‘Nothing here matches your search’ from search results web part

July 3, 2013 9 comments

Hi Friends,

SharePoint 2013 search have huge potential, with the integration of FAST and SharePoint search it becomes very powerful search solution. There are couple of new search web parts added like Content Search web part which is excellent search web part. New search result rendering mechanism called display templates is provided which are similar like JSON to HTML templates.

We will discuss a scenario about Search result web part and the solution related to the problem using display templates.

Scenario: If we add a search result web part on page and above that a search box web part to enter search term and according to that search term we want search result web part will display results. When we add both these web parts on page and before entering the search term also the results web part show  “Nothing here matches your search” (as shown in below image), which we don’t want to display unless we enter something in search box.

Search_Result_SP_2013

Obviously we may think that there will be some web part property available which will help us to resolve this issue, but web part do not have any property like this. Here come display templates in our help, we will see how we can use display templates to achieve this.

Search Results web part of SP 2013 uses ‘Control_SearchResults.html’ display template which is available under Master Page Library –> Display Templates –> Search.

Solution:

  1. Rename the ‘Control_SearchResults.html’ file to some other name e.g. ‘Control_Test_SearchResults.html’
  2. Open this file in any text editor like notepad.
  3. Change the Title in this HTML file(I have used Test Default Result for demo), this title will be available for selection in web part properties.
  4. Now we want to get the search text entered in to search box web part, for that we will use ctx object as following ctx.DataProvider.get_currentQueryState().k
  5. Search     if(ctx.ClientControl.get_shouldShowNoResultMessage()){    line in HTML file (as shown in below image).
    Search_Result_SP_2013_Display_Template_1
  6. Replace this line with
    var currentQueryTerm = ctx.DataProvider.get_currentQueryState().k; if(ctx.ClientControl.get_shouldShowNoResultMessage() && currentQueryTerm!=””){
    (As shown in below image)
    Search_Result_SP_2013_Display_Template_2
  7. Save the modified HTML file.
  8. Go to Master Page Library –> Display Templates –> Search and upload new control template.
  9. You will see that respective ‘Control_Test_SearchResults.js’ will be created by SharePoint.
  10. Now go to your page where Search Result web part is added, edit Search results web part. Under Display Templates section select newly uploaded display template.( as shown in below image).
    Search_Result_SP_2013_Display_Template_3
  11. Save the changes and that’s it, you will see the  expected result.

SharePoint 2013 Search tool

November 22, 2012 2 comments

Hi Friends,

After long time I thought of writing a something. Bit busy with SharePoint 2013.

There are lot of changes done in SharePoint 2013 search. While working with SP 13  search I found painful to debug search related issues. As object model changed old 2010 search tools ware not working with Sp2013.

I come across a good tool on codeplex developed by Nadeem. This will provide you multiple options to test the query and check the returned managed properties values. Even we can check which display template is used for the row of data returned by search.This tool uses search REST API’s in SharePoint 2013.

You can use following URL http://sp2013searchtool.codeplex.com/

Note: complete contribution, development and license rights are with the team who have developed this. This post is just to make readers aware that this kind of tool exists.