Showing posts with label Managed Properties. Show all posts
Showing posts with label Managed Properties. Show all posts

Wednesday, June 12, 2013

SharePoint 2013 Search: Getting a Value From the Previous Search Result

If you have been playing around with the new Search Display Templates, you have probably seen lots of references to ctx.CurrentItem. This is the current search result item from the result set. However, what about the previous item? Maybe you want to group results or only display certain values if the previous value is different than the current value. The value may be of any managed property or regular property of the CurrentItem object.

There isn't a PreviousItem object so I needed to figure out a different way to get to the previous item. The CurrentItem has a ParentTableReference which contains a collection of ResultRows. Perfect!

The ctx object stores the CurrentItemIdx which can be used to get a particular ResultRow and any property of the search result object (treat it like the CurrentItem).

So now I can get any result value relative to the current result item by using the index as a pointer. The previous item would be ctx.CurrentItemIdx - 1 and the next item would be ctx.CurrentItemIdx + 1.

Here is the logic in a nutshell:

<!--#_ 
        if (!Srch.U.n(ctx.CurrentItem.ParentTableReference) && ctx.CurrentItem.ParentTableReference.TotalRows > 1) {
           var previousIndex = ctx.CurrentItemIdx - 1;
           if (previousIndex >= 0)
           {
                var previousValue = ctx.CurrentItem.ParentTableReference.ResultRows[previousIndex].<<property>>;
               <<<add other conditions and set flags here>>
           }
        }
_#-->



You can now check the previous value of a property against the ctx.CurrentItem value of the property and alter the display or perform other functions accordingly.

Let me know if you find a better way!


 

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");
 
 
 


 



Matched Content