{"id":31009,"date":"2022-03-16T16:48:54","date_gmt":"2022-03-16T11:18:54","guid":{"rendered":"https:\/\/www.inogic.com\/blog\/?p=31009"},"modified":"2022-03-16T16:48:54","modified_gmt":"2022-03-16T11:18:54","slug":"open-the-respective-knowledge-base-search-result-using-the-opensearchresult-api","status":"publish","type":"post","link":"https:\/\/www.inogic.com\/blog\/2022\/03\/open-the-respective-knowledge-base-search-result-using-the-opensearchresult-api\/","title":{"rendered":"Open the respective Knowledge Base search result using the openSearchResult API"},"content":{"rendered":"<h2><strong>Introduction:<\/strong><\/h2>\n<p>Microsoft presents a range of client APIs that enable developers to interact flawlessly with Dynamics 365 CRM. One of its recent addition is the <a href=\"https:\/\/docs.microsoft.com\/en-us\/powerapps\/developer\/model-driven-apps\/clientapi\/reference\/controls\/opensearchresult\" target=\"_blank\" rel=\"noopener\"><strong><em>openSearchResult<\/em><\/strong><\/a>, which opens one of the knowledge base article listed as per the search in the search control.<\/p>\n<p>Now whenever we search for any article in knowledge based search control, we get the list of available articles as a result. A user can click on any of the listed article to open it and read it. Now what if we want to automatically open the first article every time we search. This is where this new API <strong><em>openSearchResult<\/em><\/strong> plays its part.<\/p>\n<p>The <strong><em>openSearchResult <\/em><\/strong>accepts two parameters.<\/p>\n<ul>\n<li>The first one is the resultNumber which is nothing but any numeric value starting from 1. This is the parameter where we can mention which article from the list of search results we want to open directly on every search. If we want to open the first record the value should be 1, for the second record the value should be 2 and so on.<\/li>\n<li>The second parameter defines how we want to view the opened article. Whether we want to view it as an \u201cInline\u201d or \u201cPopout\u201d. If nothing is passed then by default it is \u201cInline\u201d.<\/li>\n<\/ul>\n<p>Also the <strong><em>openSearchResult <\/em><\/strong>API returns the following three values:<\/p>\n<ol>\n<li>Returns 1 on successful Status of opening the specified search result.<\/li>\n<li>Returns 0 on unsuccessful Status of opening the specified search result.<\/li>\n<li>Return -1 if the specified resultNumber value is not present, or if the specified mode value is invalid.<\/li>\n<\/ol>\n<p>Scenario:<\/p>\n<p>Now let\u2019s look into an approach by which every user will be able to pass the resultnumber dynamically as per their convenience and based on that value on every search the respective article shall auto open. Also we shall store the outcome of this search on the form i.e. if any article was found at the specified resultnumber or not.<\/p>\n<p>To proceed with the demonstration we have used \u2018Product\u2019 entity\/table. In the product form we have added a new tab called \u2018Knowledge Base\u2019 on the main form.<\/p>\n<p><img decoding=\"async\" class=\"alignnone size-full wp-image-31017\" style=\"border: 1px solid #0a0a0a; padding: 1px; margin: 1px;\" src=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2022\/03\/1openSearchResult.jpeg\" alt=\"openSearchResult\" width=\"628\" height=\"215\" srcset=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2022\/03\/1openSearchResult.jpeg 628w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2022\/03\/1openSearchResult-300x103.jpeg 300w\" sizes=\"(max-width: 628px) 100vw, 628px\" \/><\/p>\n<p>We will add two fields on the \u2018Knowledge base\u2019 tab.<\/p>\n<ul>\n<li><strong>Record to be opened \u2013 <\/strong>This will be a whole number field on the form which would be used by the user to enter the result number to be opened.<\/li>\n<li><strong>Record Present? \u2013 <\/strong>This will be a Boolean field which will act as a flag to notify the users that the specified record is present or not.<\/li>\n<\/ul>\n<p><img decoding=\"async\" class=\"alignnone size-full wp-image-31016\" style=\"border: 1px solid #0a0a0a; padding: 1px; margin: 1px;\" src=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2022\/03\/2openSearchResult.jpeg\" alt=\"openSearchResult\" width=\"624\" height=\"264\" srcset=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2022\/03\/2openSearchResult.jpeg 624w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2022\/03\/2openSearchResult-300x127.jpeg 300w\" sizes=\"(max-width: 624px) 100vw, 624px\" \/><\/p>\n<p>Now we will add knowledge base control to the same tab, for that we will have to enable \u2018Knowledge Management\u2019 on the selected entity, in our case it\u2019s the \u2018Product\u2019 entity\/table.<\/p>\n<p><img decoding=\"async\" class=\"alignnone size-full wp-image-31015\" style=\"border: 1px solid #0a0a0a; padding: 1px; margin: 1px;\" src=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2022\/03\/3openSearchResult.jpeg\" alt=\"openSearchResult\" width=\"554\" height=\"318\" srcset=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2022\/03\/3openSearchResult.jpeg 554w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2022\/03\/3openSearchResult-300x172.jpeg 300w\" sizes=\"(max-width: 554px) 100vw, 554px\" \/><\/p>\n<p>To understand how to add the knowledge base search to the form follow this <a href=\"https:\/\/docs.microsoft.com\/en-us\/dynamics365\/customer-service\/add-knowledge-base-search-control-forms\" target=\"_blank\" rel=\"noopener\">link<\/a>. We have added the Knowledge base search control to the \u2018Knowledge Base\u2019 tab.<\/p>\n<p><img decoding=\"async\" class=\"alignnone size-full wp-image-31014\" style=\"border: 1px solid #0a0a0a; padding: 1px; margin: 1px;\" src=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2022\/03\/4openSearchResult.jpeg\" alt=\"openSearchResult\" width=\"627\" height=\"469\" srcset=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2022\/03\/4openSearchResult.jpeg 627w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2022\/03\/4openSearchResult-300x224.jpeg 300w\" sizes=\"(max-width: 627px) 100vw, 627px\" \/><\/p>\n<p>Now we will write the JavaScript code to implement the OpenSearchResult API.<\/p>\n<pre class=\"lang:css gutter:true start:1\"><code>var new = window.Sdk || {};\r\nnew.KBSearchOnResultOpened = function (executionContext) {\r\n\/\/Getting the formcontext\r\nvar formContext = executionContext.getFormContext();\r\n\/\/Getting field control\r\nvar productKBControl = formContext.getControl('Product_Knowledge_Base');\r\n\/\/Getting the user specified number\r\nvar count = formContext.getAttribute('new_searchresultcount').getValue();\r\n\/\/Calling the openSearchResult client API\r\nvar openResultStatus = productKBControl.openSearchResult(count);\r\n\/\/Setting the recordPresent field to Yes\/No\r\nopenResultStatus == 1\r\n? formContext.getAttribute('new_countpresent').setValue(true)\r\n: formContext.getAttribute('new_countpresent').setValue(false);\r\n};\r\n<\/code><\/pre>\n<p>As you can see on the line number 3, using formcontext.getAttribute(\u2018Product_Knowledge_Base\u2019) function, we get the control of knowledge base search.<\/p>\n<p>We are getting the result number from the user on line number 5 which we are passing as the first parameter at line number 6. We shall not pass any value for the second parameter as the second parameter is mode which by default is \u2018Inline\u2019.<\/p>\n<p>The above piece of code has been added as a webresource and the method Sdk.<strong><em>KBSearchOnResultOpened()<\/em><\/strong> has been called on change of \u2018Record To Be opened\u2019 field.<\/p>\n<p>If user provided result number is present then openSearchResult API will open the record and based on the return value of client API\u00a0 we will set the \u2018Record present?\u2019 field value as \u2018Yes\u2019, otherwise \u2018No\u2019 on line number 7.<\/p>\n<p><strong>Demonstation :<\/strong><\/p>\n<p><strong>\u00a0<\/strong>Search for any Article \/ Record.<\/p>\n<p><img decoding=\"async\" class=\"alignnone size-full wp-image-31013\" style=\"border: 1px solid #0a0a0a; padding: 1px; margin: 1px;\" src=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2022\/03\/5openSearchResult.jpeg\" alt=\"openSearchResult\" width=\"858\" height=\"353\" srcset=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2022\/03\/5openSearchResult.jpeg 858w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2022\/03\/5openSearchResult-300x123.jpeg 300w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2022\/03\/5openSearchResult-768x316.jpeg 768w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2022\/03\/5openSearchResult-660x272.jpeg 660w\" sizes=\"(max-width: 858px) 100vw, 858px\" \/><\/p>\n<p>Enter the result number to be opened in custom field. On change of custom field, client API will open the result. As you can see in the below screenshot we have entered \u20182\u2019 as a resultNumber and second record is opened in the reading pane. You can try other resultNumbers.<\/p>\n<p><img decoding=\"async\" class=\"alignnone size-full wp-image-31012\" style=\"border: 1px solid #0a0a0a; padding: 1px; margin: 1px;\" src=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2022\/03\/6openSearchResult.jpeg\" alt=\"openSearchResult\" width=\"637\" height=\"387\" srcset=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2022\/03\/6openSearchResult.jpeg 637w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2022\/03\/6openSearchResult-300x182.jpeg 300w\" sizes=\"(max-width: 637px) 100vw, 637px\" \/><\/p>\n<p>\u2018Record present?\u2019 field is set to \u2018Yes\u2019 as the record number is present in the search results.<\/p>\n<p><img decoding=\"async\" class=\"alignnone size-full wp-image-31011\" style=\"border: 1px solid #0a0a0a; padding: 1px; margin: 1px;\" src=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2022\/03\/7openSearchResult.jpeg\" alt=\"openSearchResult\" width=\"636\" height=\"387\" srcset=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2022\/03\/7openSearchResult.jpeg 636w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2022\/03\/7openSearchResult-300x183.jpeg 300w\" sizes=\"(max-width: 636px) 100vw, 636px\" \/><\/p>\n<p>What if we enter a number which does not have any records in the search results ?<\/p>\n<p>So for the same search let\u2019s enter the number 11 as shown in the screenshot below.<\/p>\n<p><img decoding=\"async\" class=\"alignnone size-full wp-image-31010\" style=\"border: 1px solid #0a0a0a; padding: 1px; margin: 1px;\" src=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2022\/03\/8openSearchResult.jpeg\" alt=\"openSearchResult\" width=\"651\" height=\"377\" srcset=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2022\/03\/8openSearchResult.jpeg 651w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2022\/03\/8openSearchResult-300x174.jpeg 300w\" sizes=\"(max-width: 651px) 100vw, 651px\" \/><\/p>\n<p>And as expected no record will be opened and Record Present field will show \u2018No\u2019 value.<\/p>\n<h2><strong>Conclusion:<\/strong><\/h2>\n<p>As illustrated above, OpenSearchResult Client API will improve the user experience and make the existing feature more flexible to use.<\/p>\n<p><a href=\"https:\/\/www.inogic.com\/product\/productivity-apps\/dynamics-365-crm-sharepoint-security-metadata-sync\" target=\"_blank\" rel=\"noopener\"><img decoding=\"async\" class=\"aligncenter wp-image-30807 size-full\" src=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2022\/02\/sss.jpg\" alt=\"SSS\" width=\"800\" height=\"200\" srcset=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2022\/02\/sss.jpg 800w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2022\/02\/sss-300x75.jpg 300w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2022\/02\/sss-768x192.jpg 768w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2022\/02\/sss-660x165.jpg 660w\" sizes=\"(max-width: 800px) 100vw, 800px\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction: Microsoft presents a range of client APIs that enable developers to interact flawlessly with Dynamics 365 CRM. One of its recent addition is the openSearchResult, which opens one of the knowledge base article listed as per the search in the search control. Now whenever we search for any article in knowledge based search control,\u2026 <span class=\"read-more\"><a href=\"https:\/\/www.inogic.com\/blog\/2022\/03\/open-the-respective-knowledge-base-search-result-using-the-opensearchresult-api\/\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":13,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[16,19,2361],"tags":[2579],"class_list":["post-31009","post","type-post","status-publish","format-standard","hentry","category-dynamics-365","category-dynamics-crm","category-technical","tag-opensearchresult-api"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/posts\/31009","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/users\/13"}],"replies":[{"embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/comments?post=31009"}],"version-history":[{"count":0,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/posts\/31009\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/media?parent=31009"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/categories?post=31009"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/tags?post=31009"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}