Introduction:
As we all know, Microsoft’s most recent release of the Client API References includes a slew of new features. Microsoft offers several Client APIs that allow developers to seamlessly interface with Dynamics 365 CRM.
In this blog we’ll look at one of the Client APIs that uses Knowledge Base Search Control and call specific function whenever any Knowledge Article is opened from the search result.
addOnResultOpened Client API Reference:
When we search any article in Knowledge Base Search Control using the Keywords based on our requirement, we get the list of knowledge articles that has been published and after selecting/opening any article from the search result, addOnResultOpened Client API will call some other function and perform the specified task which has been defined in that function.
The addOnResultOpened method accepts only one parameter of type Function, which will be added to the OnResultOpened Event of Knowledge Base Search Control.
OnResultOpened event occurs when a knowledge base article is opened in knowledge base search control in pop-out window or in inline.
Use Case:
In this blog, we will demonstrate how to use the above Client API by implementing a simple use case in which when user opens any knowledge article from the knowledge base search result, then using addOnResultOpened we will call custom JavaScript function which will create Email Activity record in CRM to acknowledge a user that their knowledge article has been opened by some user.
In this blog we have used Account Entity form on which we are going to implement this use case. We have added new tab on the Account entity form named “Knowledge Base Tab” which contains the Knowledge Base Search Control as shown in below screenshot:
To add Knowledge Base Search Control, you can follow the steps given below:
Go to Advance Settings -> Customizations -> Customize the System -> Expand the Entity.
Click on the entity on which you want to add Knowledge Base Search Control. Before adding the Knowledge Base Search Control, you need to make sure that the Entity on which you are adding this control is enabled for Knowledge Management. If Knowledge Management is not enabled for that entity, then you will not be able to add this control.
To enable the Knowledge Management, you need to select the checkbox for Knowledge Management on Entity form as shown in below screenshot.
After enabling this feature, you need to save and publish the customization to make sure that the settings have been successfully saved and enabled.
Now Expand the Account entity and click on form and open the form on which you want to add Knowledge Base Search Control.
Once the form is opened, then go to Insert tab and add Knowledge Base Search Control on the form section where you want to add this control.
We have added Knowledge Base Search Control on the “Knowledge Base Tab” tab inside “Knowledge Base Section” section.
Once Knowledge Base Search Control is successfully added on the entity form, now we are going to write JavaScript code to implement addOnResultOpened Client API.
// A namespace defined for New var New = window.New || {}; New.KBSearchOnResultOpened = function (executionContext) { try { //Getting the Form Context Object var formContext = executionContext.getFormContext(); //Getting the knowledge base search control var myKBSearchControl = formContext.getControl('Account_KBArticles'); //Calling addOnResultOpened function myKBSearchControl.addOnResultOpened(New.OnResultOpened); } catch (err) { console.log("Inside Catch -> " + err.message); } } //Function that need to be called when knowledge article is opened from knowledge search result New.OnResultOpened = function () { var confirmStringsMessage = { text: "Do you require any assistance?", title: "Confirmation Dialog" }; var confirmMessageOptions = { height: 200, width: 450 }; Xrm.Navigation.openConfirmDialog(confirmStringsMessage, confirmMessageOptions).then( function (success) { if (success.confirmed) { //Getting logged in user's userid var userID = Xrm.Page.context.getUserId().replaceAll("{", "").replaceAll("}", ""); //getting username of logged in user var userName = Xrm.Page.context.getUserName(); //getting the current record ID var currentRecordID = Xrm.Page.data.entity.getId().replaceAll("{", "").replaceAll("}", ""); console.log("userID : " + userID + " and userName : " + userName); //creating data object which is used to passed as parameter while creating email activity var data = { "description": "Knowledge Base Article has beed viewed by user - " + userName, ///Email Body "regardingobjectid_account@odata.bind": "/accounts(" + currentRecordID + ")", "subject": "Knowledge Base Search Article bas been viewed by somenone", "email_activity_parties": [ { "partyid_systemuser@odata.bind": "/systemusers(" + userID + ")", "participationtypemask": 1 //From Email }, { "partyid_account@odata.bind": "/systemusers(6a6b321f-e897-ec11-b400-000d3aca14a7)", "participationtypemask": 2 //To Email } ] }; //Creating the Email activity record Xrm.WebApi.createRecord("email", data).then( function success(result) { console.log("Email has been created with ID : " + result.id); }, function Error(e) { console.log("New.OnResultOpened -> " + e.message); } ) } else { console.log("Confirmation dialog has been closed by close button."); } }); }
After done with the JavaScript we are going to add this JavaScript file as a WebResource in our CRM Environment. And call New.KBSearchOnResultOpened function on tab selection changes event, since Knowledge Base Search Control does not have any event.
After doing this we are all set to go and test what will happen when we select any knowledge article in knowledge base search result.
Demonstration:
The scenario which we are considering is that there would be a support user whose job is to help the salespersons with their queries. So, the functionality which we have implemented is any time a salesperson opens a knowledge article, our logic would ask if they require any assistance. If yes then a notification will be send via email to the support user.
So based on this scenario the logic in our WebResource is – when we select/open any record, then addOnResultOpened will trigger and it will display a dialog message asking “Do you require any assistance?”. If the users opt for ‘Yes’ then an Email activity will get created which we can see in the Timeline section of the current account record.
In the Timeline section we can see that the Email activity has been created as shown in below image:
Conclusion:
Thus, we saw how using the addOnResultOpened Client API, we can further implement additional functionality on opening of a knowledge article.