Tuesday, 4 March 2014

Difference between Search Result web part and Content by Search web –part I: AvailableSortsJson property

Hi Again,

Since long time I was thinking to write this post and finally started. Since this post may get very long J I thought to divide in to number of posts. So this is first post.
Today first difference I am sharing is regarding the “AvailableSortsJson” property.

Scenario / Background:
You might be wondering why I am specifying the difference for this property. Because I spend at least 30-60 minutes to resolve the issue regarding this property. We have one CBS (Content By Search) web part provisioned for news articles and we are doing sorting on our custom managed property news article date(NewsArticleDate) and seems to be everything is fine. We have "DataProviderJSON" property in web part looks like as follows, I have shorten it for understanding and also please have a look for sorting made on NewsArticleDate shown in bold.

<property name="DataProviderJSON" type="string">{"QueryGroupName":"Default","QueryPropertiesTemplateUrl":"sitesearch://webroot","IgnoreQueryPropertiesTemplateUrl":false,"SourceID":"8413cd39-2156-4e00-b54d-11efd9abdb89","SourceName":"Local SharePoint Results","SourceLevel":"Ssa","CollapseSpecification":"","QueryTemplate"  ContentTypeId:0x010100 6B0DE1C0F3B84A5E8E60-607433E01D8A*", "FallbackSort":[{"p":"NewsArticleDate","d":1}],"FallbackSortJson":"[{\"p\":\"NewsarticleDate\",\"d\":1}]","RankRules":null,"RankRulesJson":"null","AsynchronousResultRetrieval":false,"SendContentBeforeQuery":true,"BatchClientQuery":true}</property>
Also please notice the “AvailableSortsJson” property which is null as shown follows.

<property name="AvailableSortsJson" type="string">null</property>
This web part is working perfectly fine, showing the sorted result based on our custom managed property NewsArticleDate.

Then we have requirement of searching news on news listing page and we are using the Search Result webpart for it. We have to do the sorting here also on our custom managed property NewsArticleDate. So we did the same thing as did for above CBS web part.

<property name="DataProviderJSON" type="string">{"QueryGroupName":"66bc0bb9-5b82-4b5e-a065-dbae1738ae20","QueryPropertiesTemplateUrl":"sitesearch://webroot","IgnoreQueryPropertiesTemplateUrl":false,"SourceID":"8b16ca39-1e06-494f-a0a4-a86b0aa89daf","SourceName":"Local SharePoint Results","SourceLevel":"Ssa","CollapseSpecification":"","QueryTemplate":"{SearchBoxQuery} ContentTypeId: 0x010100 6B0DE1C0F3B84A5E8E60-607433E01D8A* ","FallbackSort":[{"p":" NewsArticleDate ","d":1}],"FallbackSortJson":"[{\"p\":\" NewsArticleDate \",\"d\":1}]", "RankRules":null,"RankRulesJson":"null", "AsynchronousResultRetrieval":false,"SendContentBeforeQuery":true,"BatchClientQuery":true}</property>
Here also please notice the “AvailableSortsJson” property which is shown follows.

<property name="AvailableSortsJson" type="string">[{"name":"Relevance","sorts":[]},{"name":"Date(Newest)","sorts":[{"p":"Write","d":1}]},{"name":"Date(Oldest)","sorts":[{"p":"Write","d":0}]},{"name":"Lifetime Views","sorts":[{"p":"ViewsLifeTime","d":1}]},{"name":"Recent Views","sorts":[{"p":"ViewsRecent","d":1}]}]</property>

But unfortunately this doesn’t seem to be working. Search result is not getting sorted on NewsArticleDate.

Then I took the help of my best friend ILSpy, trying to dig the very complex architecture of Content by search and Search Result web part. Then I noticed that SearchResult web part considers the AvailableSortJson property. This means that we also need to update the AvailableSortJson property of Search Result web part for doing sorting on our custom managed property so I changed the AvailableSortJson as follows
<property name="AvailableSortsJson" type="string">[{"name":"Date(Newest)","sorts":[{"p":" NewsArticleDate","d":1}]}]</property>

And wonder happens now result is sorted in Search Result web part on my custom managed property.
Difference:

1. Search result web part gives the default options for sorting but CBS not as

SearchResult: <property name="AvailableSortsJson" type="string">[{"name":"Relevance","sorts":[]},{"name":"Date(Newest)","sorts":[{"p":"Write","d":1}]},{"name":"Date(Oldest)","sorts":[{"p":"Write","d":0}]},{"name":"Lifetime Views","sorts":[{"p":"ViewsLifeTime","d":1}]},{"name":"Recent Views","sorts":[{"p":"ViewsRecent","d":1}]}]</property>

CBS: <property name="AvailableSortsJson" type="string">null</property>

2. To have sorting on our custom managed property in Search Result web part, we need to update the make it available first for sorting using AvailableSortsJson”. This change is not required for CBS web part.
That’s all here for now, I’ll try to keep posting such interesting differences.

Thanks!          
Enjoy reading J
 
Feel free to ideas / comment / feedback if any or if you have any query

No comments: