Cards in Microsoft Teams Notifications with action templates
In the previous post, I have explained how to configure SQLWATCH To send simple, plain text notifications to Microsoft Teams.
Today, we are going to look at how to send formatted cards:

Action templates
Formatted cards require slightly more complex JSON structure and for that reason, we are going to create a new message template. SQLWATCH has always been designed with flexibility in mind. Adding a new message template is a very simple task:
USE [SQLWATCH]
GO
declare @template varchar(max) = '{
"@context": "https://schema.org/extensions",
"@type": "MessageCard",
"sections": [
{
"facts": [
{
"name": "Check Staus:",
"value": "{CHECK_STATUS}"
},
{
"name": "Check Name:",
"value": "{CHECK_NAME}"
},
{
"name": "Host Name:",
"value": "{SQL_INSTANCE}"
}
],
"text": "{CHECK_DESCRIPTION}"
}
],
"summary": "Check Summary",
"title": "{CHECK_STATUS}: {CHECK_NAME} on {SQL_INSTANCE}"
}
'
INSERT INTO [dbo].[sqlwatch_config_check_action_template]
([action_template_description]
,[action_template_fail_subject]
,[action_template_fail_body]
,[action_template_repeat_subject]
,[action_template_repeat_body]
,[action_template_recover_subject]
,[action_template_recover_body]
,[action_template_type])
VALUES
('Microsoft Teams Card'
,''
,@template
,''
,@template
,''
,@template
,'TEXT' )
GO
SELECT action_id = SCOPE_IDENTITY()
The next step is to re-assign existing action to use the new template. In our case this is action_id=1
(as in the previous post) and action_template_id=1
:
update [dbo].[sqlwatch_config_check_action]
set action_template_id = 1
where action_id = 1
The last step is to update the existing action_exec
in [dbo].[sqlwatch_config_action]
and only pass {BODY}
as the entire payload formatting is now done in the template:
$webhookurl = "https://outlook.office.com/webhook/..."
Invoke-RestMethod `
-Uri $webhookurl `
-Method Post `
-Body '{BODY}' `
-ContentType 'application/json'
If you are using the original action from the previous post, you can simply update the action:
UPDATE [dbo].[sqlwatch_config_action]
SET action_exec = REPLACE(action_exec,'{"text":"{BODY}", "title":"{SUBJECT}"}','{BODY}')
WHERE action_id = 1
Conclusion
That’s it. You are now going to receive formatted notifications and you are free to modify the card template to fit your purpose. If you are new to SQLWATCH please check our documentation and the getting started guide.
This post was originally published on 10th June 2020.
2 Comments
Leave A Comment Cancel reply
Recent Posts
Want to learn about SQLWATCH?
Marcin Gminski2023-03-28T22:07:01+01:0028th March 2023|0 Comments
SQLWATCH 4.4
Marcin Gminski2022-10-06T15:32:56+01:006th October 2022|0 Comments
T-SQL Tuesday #147 – SQL Server Upgrade Strategies
Marcin Gminski2023-07-06T23:15:07+01:008th February 2022|0 Comments
Should we run SQL Server on the default port 1433
Marcin Gminski2022-03-25T12:40:05+00:007th February 2022|3 Comments
Happy New Year!
Marcin Gminski2022-01-01T18:28:43+00:001st January 2022|0 Comments
I am a Microsoft MVP!
Marcin Gminski2021-12-02T23:15:16+00:002nd December 2021|10 Comments
Hi, How do you pass values to the variables in the JSON template? for example, CHECK_NAME
Hi, you just use the actual variable name in curly brackets `{CHECK_NAME}`, here’s the full list of variables exposed to templates:
https://docs.sqlwatch.io/notifications/actions-notifications/#list-of-variables-exposed-to-templates