Sunday, March 6, 2016

Create Display Templates for SharePoint 2013


If you are planning to expose your custom columns in your SharePoint list or library through Search Results, first of all you need to configure those columns as Managed Properties for the SharePoint Search Service Application.


https://msdn.microsoft.com/en-us/library/office/jj945138.aspx

Create Managed Properties

To make a managed property you need to create site column or Managed Meta data column. In this example I will be creating a site column.

Drop down Values

While creating a site column to use in a drop down value, I was using Lookup field. Once I created the Managed property lookup values did not come as search results. Then I created the site column as choice field. Then I was able to retrieve the data through search results.

Create Site Column  


Create a Site column with choice filed
Create Site Column with Date Time Field

Once you create DATE TIME field, once data is available for the column you will be able to see the Crawled Property for the column.




Refer the Site Column in Document Library/List


Once site column is created, create document library column using newly created site column.



Managed Properties and Crawled Properties


1.       Navigate to Central Administration, Search Service Application
2.       Execute Full Crawl for Relevant Content Source and wait for completion


3.       Once full crawl is completed, Click “Search Schema”
4.       If you search for the managed property, you will be able to see newly created managed properties with Created Crawled Properties
5.      




Create the Display Template


Once you are ready with Managed Properties, you are ready to create your Display Template. In this example I’m creating the Search Default Item Display template.
Best option is use existing Display Template to create your New Display Template.

Download the Default Item Template from the Display Templates


Create your display Template

1.       Download only the HTML file from the Display Template gallery  (Masterpages/Display Templates/Search)
2.       Rename the File as you want.






3.       Change the Template Title
Change the Title of the Template in HTML File




4.       Add Managed Property to Display template
Add your Managed Property in to the ManagedPropertyMapping Section.
NOTE: while adding the Managed Property, I used only Managed Property Name for both Places
'SigiriyaMeetingDateOWSDATEUse':'SigiriyaMeetingDateOWSDATEUse','SigiryaDocumentCategoryOWSCHCS':'SigiryaDocumentCategoryOWSCHCS'


5.       Modify the look and feel of the HTML as you want with your newly added managed Properties



I have added the Sections highlighted in pink to change the look and feel of the Display Template.
6.       Upload the File to Display Template Gallery

While you upload the new Template file, make sure you select Meta Data of the file as follows


Your Managed Properties also will look like this



7.       Once you uploaded the file, you will see associated .js file is automatically created from the SharePoint
8.       Leave .js file as it is and publish the html file.



Associate you Newly Created Display Template in Search Results Web Part

For this example, I have created a New Page with Search Box Web Part and Search Results web part added.


With the existing default Template, search results will display as follows




1.       Edit the Page and navigate to Web Part Properties of the Search Results Web Part
2.       In web part properties, change Item Display Template to newly created display template



3.       Save the Web Part properties and Save the Page.
4.       If you need you can create another Custom Template for Results instead of “Default Result” Template


Sections Highlighted in Pink displayed from our custom managed Properties and Custom display template.

NOTE: First item (document library, All Items View) was displayed in the search results, because we did not restrict the search results in to a particular result type or content type. You can change Search Results web part, Change query to display only items inherit from Document Content Type.

Then search results will display only the library items not the views.