Access managed properties in Control template

Those who have worked with display templates would have worked with accessing managed properties in the item template of the Content Search Web Part (CSWP). There might be a requirement when you want to access the values of the managed properties of each result item in the control template of your CSWP rather than your item template.

The below code does exactly that:

clip_image001

a. $.each() is the jQuery loop function that is used to loop through each result returned

b. ctx.ListData.ResultTables[0].ResultRows retrieves the “RelevantResults” results from the 4 result tables available in the CSWP as shown below:

clip_image002

c. function (key, val) is the function that is going to process each search result.

d. val.EmailDate.toString(): Here only we are accessing the managed property called “EmailDate” from the variable “val” which would hold the values of all managed properties of the result item.

Debug SharePoint display templates in Google Chrome

In spite of being a Microsoft .NET and SharePoint developer I have no regards for Internet Explorer no matter whatsoever. Google chrome is the best browser in the history of browsers.

You may also like to have a look at the below blog posts regarding Content Search Web Parts:

Let me you explain a small tip on how to debug the display templates of your Content Search Web Parts in SharePoint in Google Chrome.

There will always going to be 2 files created for your display template. One is the .HTML file in which you would be working and the other is the .JS file which gets automatically created and used by SharePoint once you upload your .HTML file.

a. Open the page in which you have your CSWP.

b. Press Ctrl+Shift+J. This will open your developer dashboard.

c. Click on “Sources” from the tabs as shown below:

clip_image002[6]

d. Expand _catalogs/masterpage and then Display Templates/Content Web Parts

clip_image003[4]

e. You should be seeing the .JS file of your display template. Click on the .JS file to open it in the workspace.

f. Put a breakpoint anywhere and refresh the page and the page load would pause when the break point is hit in the display template as shown below:

clip_image004[4]

g. From now you can debug like how you do in Visual Studio using F10 and F11.

Maximum before and after current page properties in SharePoint 2013 search

If you have used or worked with SharePoint search, you must have come across the pagination control that appears in the bottom of the search results web part in the search results page.

clip_image001

This control came as a separate web part in SP 2010 where we had the below 2 properties in the tool pane that can be set to show the maximum no. of page links to be shown before the current page and the maximum no. of links to be shown after the current page.

clip_image002

Whereas in SP 2013 the pagination has become a part of the core search results web part itself and is being created in the control template of the web part (more on that later). And the above 2 properties of the pagination are not seen in the tool pane of the search results web part which came as a surprise to me.

Later, to my surprise when I exported the search results web part I was able to see the 2 properties in the .webpart file as shown below:

PaginationProp

Setting the properties in the .webpart file and importing it back to SharePoint made the necessary changes in the pagination reflecting. Hope you find it useful.

Auto Suggestion/Auto Complete for SharePoint 2010 Search sites

Introduction:

It will be very useful for the users to have the search being suggested based on the past searches while they are typing in the search text box in a SharePoint search site. Yes I’m talking about the same concept that you find in the Google search but with a limited functionality.

This feature is available as an OOTB feature in SharePoint 2013 enterprise search. Look at this link to know more about this cool feature. The process is completely different though.

Description:

This is a simple JavaScript application which when added to a Content editor web part in home page and in the search results page of your SharePoint search site would display suggestions for the users as they type their search query in the search text box. The suggestion keywords are stored and retrieved from a SharePoint custom list called “Search Keywords”. Look at the below screenshot for a better understanding:

As you can see in the above image, as I type “I love s”, I get “I love SharePoint” and “I love Silverlight” as suggestions. These 2 are retrieved from a SharePoint list called “Search Keywords”.

So how and when are these suggestion keywords stored in the “Search Keywords” list? When a user is searching in the search site and the results page finished loading, the JS code added in the CEWP in the results page would add the search query in the “Search Keywords” list as new list item. Thus the matching suggestion keywords are retrieved from the list as the user is typing in the search text box later.

Limitations:

Since the suggestion keywords are added to a SharePoint list as a new list item every time a user searches, the performance may reduce when 1000’s of items get stored in the list. So it is advised to manually delete few keywords often from the list.

Code:

The code is developed using JavaScript, jQuery, Autocomplete jQuery ui widget, and SharePoint Client object model. There are 2 different JavaScript codes. One is for the search home page and the other is for the search results page.

I have explained the code in two parts in the below blog posts:

Part 1: How to give the Auto suggestion/complete functionality to the Search box with search terms retrieved from a SharePoint list.

Part 2: How to store the users’ searched queries to a SharePoint list dynamically and then show them as suggestions in the search box while searching.

Since it is purely a client side application, we can simply deploy it by just moving the files. I assume that you have worked with Content editor web part before. You need to create 2 CEWPs, one for search home page and the other for the search results page. I have explained the deployment process through the below steps:

Deployment process:

  1. Download the SPAutosuggestion.zip file and unzip it.
  2.  Create a folder called SPAutosuggestion under the layouts folder of your SharePoint server.
  3. Copy the files jquery-1.7.js, jquery.ui.autocomplete.js, jquery.ui.core.js, jquery.ui.complete.js, jquery.ui.widget.js, and jquery-ui.CSS from the unzipped folder in to the above create folder.
  4. Go to your SharePoint search site and create a list called “Search Keywords”
  5. Go the search home page and edit the page.
  6. Add a Content editor web part in to the page.
  7. Click on “Click here to add new content” as shown in the below image:  
  8. Now in the ribbon control click on HTML -> Edit HTML Source as shown below:  
  9. In the HTML source editor window copy the content of the AutoSuggectionLandPage.js which would be present in the folder unzipped in step 1.
  10. Click on “Save & Close” under the page tab in the top ribbon.
  11. Go to the search results page of you SharePoint site and edit it.
  12. Repeat step 6 to 10. But instead of copying the content of AutoSuggectionLandPage.js, copy the content of AutoSuggestionResultsPage.js.
  13. Search with different queries twice or thrice to see them getting suggested as you type in the search text box.