setContextProvider

Omnichannel for Customer Service offers a suite of capabilities that extend the power of Dynamics 365 Customer Service Enterprise to enable organizations to instantly connect and engage with their customers across digital messaging channels. An additional license is required to access Omnichannel for Customer Service. For more information, see the Dynamics 365 Customer Service pricing overview and Dynamics 365 Customer Service pricing plan pages.

Sets the context provider for live chat channels. The context provider function, when invoked, returns the context to be used for initializing a chat session.

Important

  • The setContextProvider method is supported only for unauthenticated chat. For authenticated chat, you must use the JSON Web Token (JWT). More information: Send authentication tokens
  • You can pass only 100 custom context variables during each chat session.
  • The live chat SDK methods should be invoked after the lcw:ready event is raised. You can listen for this event by adding your own event listener on the window object.
  • You don't need to use the setContextProvider method if you are using the startChat SDK API to pass the context and initialize the chat session.

Syntax

Microsoft.Omnichannel.LiveChatWidget.SDK.setContextProvider(contextProvider);

Parameters

Parameter Type Description
contextProvider Function Function that when invoked, returns the custom context

Return value

None

Example

window.addEventListener("lcw:ready", function handleLivechatReadyEvent(){
        // Set the custom context provider
        // Throws error if contextProvider is not a function
        Microsoft.Omnichannel.LiveChatWidget.SDK.setContextProvider(function contextProvider(){
                   //Here it is assumed that the corresponding work stream would have context variables with logical name of 'contextKey1', 'contextKey2', 'contextKey3'. If no context variable exists with a matching logical name, items are created assuming Type:string               
	               return {
                             'contextKey1': {'value': 'contextValue1', 'isDisplayable': true},
                             'contextKey2': {'value': 12.34, 'isDisplayable': false},
                             'contextKey3': {'value': true}
                   };
        });
});

In the preceding sample code, the value attribute contains the value of the context variables.

The isDisplayable attribute, if set to true, will display the passed items in the third tab on the Conversation Summary Control for a conversation.

Note

If no context variables have been created under live workstream with a matching logical name, then the variables are created at runtime with the type String. More information: Context variables

Display context keys.

Pass customer's self service as context

You can pass a customer's self service as context at the start of a conversation with that customer. The setContextProvider method passes the recent customer actions as part of context with self service type, for a new conversation between the customer and agent. A visitor journey record is created for each self service action.

Here's a sample code that shows how to use the setContextProvider method.

window.addEventListener("lcw:ready", function handleLivechatReadyEvent(){

Microsoft.Omnichannel.LiveChatWidget.SDK.setContextProvider(function contextProvider(){
    return {
            'SelfService': { 'value' : 
                '[ {"msdyn_displaytitle":"Page visited", "msdyn_starttime":"yyyy-mm-ddThh:mm:ssZ","msdyn_type":192350000}, \
                   {"msdyn_displaytitle":"Phrase searched", "msdyn_starttime":"yyyy-mm-ddThh:mm:ssZ","msdyn_type":192350001}, \
                   {"msdyn_displaytitle":"Knowledge article viewed", "msdyn_starttime":"yyyy-mm-ddThh:mm:ssZ","msdyn_type":192350002}, \
                   {"msdyn_displaytitle":"Custom action performed", "msdyn_starttime":"yyyy-mm-ddThh:mm:ssZ","msdyn_type":192350003} \
                 ]'
             }, // Additional context can be added as shown below
                //Here it is assumed that the corresponding work stream would have context variables with logical name of 'contextKey1', 'contextKey2', 'contextKey3'. If no context variable exists with a matching logical name, items are created assuming Type:string
                'contextKey1': {'value': 'contextValue1', 'isDisplayable': true},
                'contextKey2': {'value': 12.34, 'isDisplayable': false},
                'contextKey3': {'value': true}
         };
    });
});

For the sample script to track self service actions that can be included in the Self Service Portal, see Self Service sample code.

Error codes

The following error can occur for this method.

Error code Error message
1 Context provider method execution failed

See also

JavaScript API reference for live chat SDK
Display custom context