Recently I had been working on a new mechanism to deploy pre-built Content Search WebParts into new Office 365 tenant environments. The process included pre-creating a handful of display templates and then creating a Provider-hosed app to provision all the new display templates into the Master Gallery. Once I did this and used the Client Object model to create these files in the new environment, I started plugging away at building my pages and I noticed something strange. All of my deployed display templates were showing up twice in the available templates list!
I was bewildered! When I selected the first of the duplicates, the content search webpart would always fail. However if I selected second duplicate, my deployed display template would work just fine. So my first thought was maybe somehow the display template was duplicated in the master gallery. If the deployment was successful I should have one html and one js file created for each display template respectively. When I went to look inside my Master Gallery that is exactly what I saw!
Needless to say, this left me a little stumped. When I opened up the file properties, they looked identical to the OOTB display templates from Microsoft. This lead me to believe that something with my app which deploys the files had to be the problem. Lo and behold this was exacatly the problem!
When I was deploying the files in the Master Gallery and then updating the new list item, I had to set properties such as ContentTypeId, TargetControlType, UIVersion, etc... Well one of the properties which I thought I was supposed to update was the DisplayTemplateLevel.
I have commented out the line of code which was the culprit! The issue could be a result of me only deploying out the HTML file for my display template and having the JS file be autocreated. I think the reason that it is related is because a coworker of mine noticed something strange in the available list items. We opened up Chrome Debugging tools and inspected the available templates dropdown list and found this!
Lesson Learned: Do not update the DisplayTemplateLevel when deploying these files into your master gallery!