Tuesday, May 21, 2013

SharePoint 2013 Search: Adding Managed Properties to Display Templates

Based on some feedback from a previous post, a reader commented that it would be nice to see how to add managed properties to a search display template. The most common method is a one-to-one mapping, however, you may link a search result item property to multiple managed properties. This post explains both methods.

While the recommended approach is to use Design Manager to upload display templates, I find it much easier to make copies and modifications using SharePoint Designer 2013. You may access the search display templates from SharePoint Designer 2013 using the method explained in this post.

One-to-One Mapping

Each Search display template contains default managed properties. They appear in the <mso:ManagedPropertyMapping> tag at the top of each template:


{Click on image to see larger view}
Sometimes when making copies of the templates or modifying existing ones, the quotes become http encoded:

{Click on image to see larger view}


It doesn't matter as either way will work. The syntax for one-to-one mapping is as follows:

 
'<<property name to be used in current item>>':'<<managed property name>>'
 
 
To add a new managed property, simply add another entry. For example, if I had a managed property named "CustomerID", I would add 'CustomerID':'CustomerID', to the <mso:ManagedPropertyMapping> tag:
 
 
 
For maintainability it is recommended to name the current item property name the same as the managed property name. Once that is added, you may now use the new property by referencing it from the current result item:
 
ctx.CurrentItem.CustomerID
 
Once you save your changes, you need to update your result type such that the query component knows which properties need to be returned for the search result.
 
 
One-to-Many Mapping
If you take a look at the Item_Video_CompactHorizontal.html template, you'll notice that they are mapping a search result item property to multiple managed properties:
 
{Click on image to see larger view}
The syntax here is a little different:
 
'<<search result item property name>>'{search result item property name}:'<<list of managed properties>>'
 
The list of managed properties are separated by semicolons. I am assuming that the value of the search result item is populated with the first managed property that has a value based on the order listed.
 
To use the managed property value within the template code/mark-up, you must get the value from the context:
 
var imageURL = $getItemValue(ctx, "Picture URL");
 
 
 


 



37 comments:

  1. Thank you for this post. I have another question about an AD mapping that returns multiple results for a particular user. In Refinements it shows up with a list of those results, but then the next refinement may also have the same search criteria in it. What is the best way to change the refinement panel to show each search singularly? For example:

    On USer Profile and is shows as refiner:

    A, B, C, F
    B, C, F
    F, G, H
    A, G, H

    How do i make the refiner show each individually and show the results appropriately? Thanks.
    A
    B
    C
    D
    E
    F
    G
    H

    ReplyDelete
    Replies
    1. Easier said than done. To get this to work you need to create a Content Enrichement web service and associate it to the Search Crawl. I eventually got it working but it was a pain.

      There are links to the MSDN article as well as an example here:

      http://social.msdn.microsoft.com/Forums/wpapps/en-US/7fa6fe7b-8ea5-42eb-b9d6-fa396da3eff9/index-a-multiple-value-column-in-bcs-external-content-type-sharepoint-2013

      Delete
  2. Does this work on SharePoint Online (Wave 15)? If not how could you do this on SharePoint Online.

    ReplyDelete
    Replies
    1. I would think it would be the same but I need to confirm. I am going to start investigating search in SharePoint Online.

      Delete
  3. Hi there. I am wanting to add in a field, specifically phone number. However, how does it get the phone number? Does the field have to be named something specific, and if so, how do you find out the name for the phone number field.

    ReplyDelete
    Replies
    1. You may review the Search Schema through the Search Service Application (via Central Admin) or at the site level. The list of managed properties are the ones that you may use here. Make sure the property is set to Retrievable.

      Delete
  4. Hi Steve, I am having trouble getting a managed property to display in a copy of Item_Default.
    In mso:ManagedPropertyMapping I have added GPCLNewsReadMoreOWSURLH:GPCLNewsReadMoreOWSURLH
    then
    var NewsItemLink = $getItemValue(ctx, "GPCLNewsReadMoreOWSURLH");
    then
    _#= ctx.CurrentItem.GPCLNewsReadMoreOWSURLH =#_.
    I can confirm the property is present in search using the rest API (http:///_api/search/query?Querytext='GPCLNewsReadMoreOWSURLH:"google"'&RowLimit='10'&SelectProperties='Title,Path,GPCLNewsReadMoreOWSURLH').
    I have defined a result type and selected "Use result types to display items" in the search results webpart which finds the correct custom Item_Default Display Template but refuses to display the Managed Property (GPCLNewsReadMoreOWSURLH:GPCLNewsReadMoreOWSURLH). If I loop through the properties of ctx.CurrentItem the managed property is not present.
    Can you confirm that this is a bug as per this discussion - http://social.technet.microsoft.com/Forums/sharepoint/en-US/f53c87d0-af81-445c-8338-1bfea6ca780e/managed-properties-in-display-templates.
    Your help will save my sanity.
    Thanks
    Grant

    ReplyDelete
    Replies
    1. I know people are having problems with properties displaying but I can't confirm it as aI bug. I usually can resolve by updating the result type and running a full crawl.

      Delete
  5. Can you please help me to display View Property in SharePoint Foundation 2013 Search Result

    ReplyDelete
    Replies
    1. I don't have a SharePoint Foundation environment to play with at the moment.

      Delete
  6. Hello All. IS there anyone that can help me on my issue. Ive been working at this for about a couple of weeks now, and I believe it is onyl one string that I am possibly overlooking. However I have created a corporate directory- Sharepoint 2013, but as a table layout. I have added the associated columns to the content results display template. I need to properly add it to the item table display template in addition to displaying a thumbnail (picture) of all persons in the directory. I have everything else displaying no problem. Im working on the picture portion that has me pulling out my hair.

    ReplyDelete
    Replies
    1. Look at the People display templates and copy how they are showing the pictures.

      Delete
  7. Hi Steve,

    I am trying to update the people search results with 3 profile properties using "Item_Person" Display template

    1) WorkPhone
    2)WorkEmail
    3)Office

    I am able to display my results with Work Phone and Work email, However I am not able to display Office value. I have used the exact syntax for Office attribute that I have used for the other 2 properties.Any information or help is appreciated

    ReplyDelete
    Replies
    1. Try OfficeNumber or BaseOfficeLocation.

      Delete
    2. Worked like a charm... Thanks a lot Steve.. I am just curious how did you get to know we should use OfficeNumber when the actual attribute name is Office? Just curious

      Delete
    3. You need to look at your Search Schema within the Search Service Application and look for the correct Managed Property name. You also need to make sure that the Retrieve option is selected for the property.

      Delete
    4. Oh ok good to know.. unfortunately we are on Office365.. Thanks for the info..

      Delete
  8. Hi I have created a custom item_person.html to which I have added work phone and email ..and ensure the people_core_search web part results used this new custom display template .- no issues. Next I want include some managed properties in the display tempate (resuts) that act as lookups to some custom AD attributes I store against each user .. To get these to display in my display template, I must added each property to my Results Schema.

    ReplyDelete
  9. Nice article!

    In 2010 people search results, "Browse in organizational chart" is also displayed aside from other user info. Can this be displayed also in 2013?

    Thanks.

    ReplyDelete
    Replies
    1. I assume this would still be available but not sure. We use an external content type for our People data - we don't use the user profiles.

      Delete
  10. Hi,
    I have managed to show up work email and work phone on the result page. Now I also want to show up Mobile number (CellPhone). I have done the same process as I did for work email and work phone. But mobile number doesn't seem to appear? Any idea? I can see the mobile number on my sites, so the value is synched properly from AD.

    This is how my code looks like

    'AboutMe':'AboutMe','AccountName':'AccountName','BaseOfficeLocation':'BaseOfficeLocation','Department':'Department','HitHighlightedProperties':'HitHighlightedProperties','Interests':'Interests','JobTitle':'JobTitle','LastModifiedTime':'LastModifiedTime','Memberships':'Memberships','PastProjects':'PastProjects','Path':'Path','PictureURL':'PictureURL','PreferredName':'PreferredName','Responsibilities':'Responsibilities','Schools':'Schools','ServiceApplicationID':'ServiceApplicationID','SipAddress':'SipAddress','Skills':'Skills','UserProfile_GUID':'UserProfile_GUID','WorkEmail':'WorkEmail','WorkPhone':'WorkPhone','WorkId':'WorkId','YomiDisplayName':'YomiDisplayName','CellPhone':'CellPhone'


    var has_CellPhone = !$isEmptyString(ctx.CurrentItem.CellPhone);

    if(has_CellPhone == true) {
    var encodedCellPhone = $htmlEncode(ctx.CurrentItem.CellPhone);
    var displayCellPhone = Srch.U.getSingleHHXMLNodeValue(hhProps, "CellPhone");
    if ($isEmptyString(displayCellPhone)) { displayCellPhone = encodedCellPhone }

    ReplyDelete
  11. Sorted :) I tried using MobileNumber and it worked .

    Thanks

    ReplyDelete
    Replies
    1. I am trying to update the Item_Person and I have found MobilePhone property but I added it as you have here but it is not showing up for me. What am I missing?

      Delete
    2. Make sure you go into Result Types and click on the Update link at top. There should be a Yellow bar alerting you of changes.

      Delete
  12. Hi I am trying to update Item_Person template to show manager. But the manager is not showing up. When I looked for the property in search schema it returned nothing.
    Am I missing something?

    ReplyDelete
    Replies
    1. The crawled property is People:Manager. It does not appear that this crawled property is mapped to a managed property. So you would need to create a Managed Property that maps to People:Manager. You will probably want to make it Searchable, Queryable, and Retrievable. Run a full crawl and then use that new managed property in your template.

      Delete
    2. Hey Steve - The Manager's and Assistant's Name are appearing in the format domainname\loginname. How can it be display in Preferred Name or Full Name Any idea?

      Thanks,
      Khushi

      Delete
    3. Do you have your User Profile sync running?

      Delete
    4. Hello, I am also looking to display the Manager User Profile Property as the Preferred/Friendly Name instead of the domain\username. Is there a way to do this?

      Thank you,
      Amy

      Delete
  13. Hi Steve, I have a requirement to load the picture from external url. each image is named with employeeId. when clicked, it should take to that application. Basically, 2 fields - image field to redirect and load the image from external site and the hyperlink of username should redirect/open in new window to the external site. For ex: Lexisnexus InterAction has user profile details including pictures. I just want to display Interaction photo in people search result page and when click display name hyperlink to redirect to the InterAction user page.

    Appreciate your assistance.

    Thanks
    Shyam

    ReplyDelete
    Replies
    1. You cold add custom properties to the user profiles that contained the image url and link url. Then map those properties to new managed properties and include in your display template. You would add HTML on the template to create an anchor around an image using the appropriate urls for the href and src.

      Or you can construct the urls in the templacement itself I using the people account property but that would be a hard coded solution.

      Delete
    2. You cold add custom properties to the user profiles that contained the image url and link url. Then map those properties to new managed properties and include in your display template. You would add HTML on the template to create an anchor around an image using the appropriate urls for the href and src.

      Or you can construct the urls in the templacement itself I using the people account property but that would be a hard coded solution.

      Delete
  14. Do you have any advice on getting BaseOfficeLocation to show up, Steve? The property is obviously part of the template (in the managed property tag) and should be available, but the property never populates (that I can see when looking through the properties in F12. I know that the property is being populated through the UPS because it is visible in the mysite Person.aspx

    ReplyDelete
    Replies
    1. There is also an Office property. is that blank too?

      Delete
    2. CTX Current Item does not show a property called Office nor can I find one in search schema Managed Properties. The crawled property People:Office is mapped to Managed Property "OfficeNumber". I'm using the managed property "WorkPhone" to display the TN in my display template.

      Delete
  15. Hi Steve,

    I am trying to figure out how I can add properties to the JSON that gets returned when a user searches. I have created three managed properties and they are retrievable.

    What I am trying to do is get the JSON in the control template process that JSON with a custom function and then display the results.

    In order for me to process the JSON I need three additional properties for each item returned. I can't seem to figure out how to have search add the additional properties to the returned JSON.

    Is this even possible?

    Thanks in advance.

    ReplyDelete