In general I think that it is good to use out of the box functionality in SharePoint. Sometimes however, out of the box just not good enough.
Today I created a list with URLs and I used the out of the box field URL. SharePoint created a Managed Property for me called OWS_URL.
So all is still good.
Then I created an item display template and updated my ManagedPropertyMapping
<mso:ManagedPropertyMapping msdt:dt=”string”>’Title’:’Title’,’OWS_URL’:’OWS_URL'</mso:ManagedPropertyMapping>
then I’m collecting the title and the URL field in my JavaScript:
var title = ctx.CurrentItem.Title;
var url = ctx.CurrentItem.OWS_URL;
Finally I’m displaying my Title of the items with a link to the URL mentioned in the URL field:
<a href=”_#= url =#_”>_#= title =#_</a>
All easy so far, but …. it doesn’t work. I’m now getting a URL https:/ /mytenant.sharepoint.com/sites/dev/Pages/ which is the location of the page where my search result webpart is added to the page. When I check in my browser I’m finding that ctx.CurrentItem.OWS_URL is set to null.
Then I created a new Managed Property myself called MyURL
And mapped it to ows_URL (I also ticked the boxes Searchable and Retrievable)
<mso:ManagedPropertyMapping msdt:dt=”string”>’Title’:’Title’,’OWS_URL’:’OWS_URL’,’MyURL’:’OMyURL'</mso:ManagedPropertyMapping>
Then I updated the JavaScript a bit:
var title = ctx.CurrentItem.Title;
var url = ctx.CurrentItem.MyURL;
And now my URL is appearing fine.