SharePoint HTTP connector: Send a no-reply email in Power Automate Flow

Problem

There are many blog posts for sending no-reply email from Power Automate.

The question from the customer is how to create the JSON object body ‘dynamically’ to put in the “SharePoint HTTP connector”?

The following is the sample list with Subject, Body and TO (with multi-person) fields data. How to read this information and create the JSON body for the no reply email?

Sample list for sending and email using “SharePoint HTTP connector”
Note: This is the format of the JSON object you need to pass
Please note the To field is a JSON array.

{
  "properties": {
    "Subject": "This is a new request from test system.",
    "To": [
      "BobK@GOV019539.OnMicrosoft.com",
      "DebraB@GOV019539.OnMicrosoft.com",
      "JordanM@GOV019539.OnMicrosoft.com"
    ],
    "Body": "**removed for brevity***"
  }
}
Note: In the above 'To' is a JSON array.

Step by Step Solution

This solution will required some basic understanding of the JSON object and JSON array.

We will make use of the setProperty expression to set the property of the JSON object.

IMPORTANT: The ‘Set Variable’ action does not allow self reference, in other words you can not do Index = Index + 1. See here for the details. The alternative approach is to use the Compose action to manipulate the variable value and then assign the output of the Compose in the Set Variable.

Step # 1 Define a manual trigger, two variables and a Get Item action.

JSON Object and JSON Array variables are defined.

Step # 2 Set the ‘Subject’ property to the ‘PropertiesObjectVar’ variable

Set the Subject Property using Compose action and use the Set Variable action
Note: First time 'PropertiesObjectVar' variable is an empty object.

setProperty(
          variables('PropertiesObjectVar'),
          'Subject',
           outputs('Get_item')?['body/Subject']
)

Step # 3 Set the ‘To’ property to the ‘PropertiesObjectVar’ variable

Now there is a trick here, we need the JSON Array object. In the Apply Each action first get all values in the Array variable ‘ToFieldArrayVar’.

Apply Each action to extract Email and append to Array variable ‘ToFieldArrayVar’

Using the output of the array variable set this to the JSON object ‘PropertiesObjectVar’

Set the To property of the JSON Object
Note: The 'PropertiesObjectVar' variable is not with 'Subject'.
After the set Property it will have 'To' property

setProperty(
          variables('PropertiesObjectVar'),
          'To',
           variables('ToFieldArrayVar')
)

Note: The array variable is assigned to 'To' property.

Step #4 Set the ‘Body’ property to the ‘PropertiesObjectVar’ variable

Set the Body property for the JSON Object
Note: The 'PropertiesObjectVar' variable will have 'Subject' and 'To', the following call with add 'Body'

setProperty(
          variables('PropertiesObjectVar'),
          'Body',
           outputs('Get_item')?['body/Body']
)

Step #5 Finally construct the Final Object as required for the no reply email.

A Compose action to create a final JSON object.
Note: setProperty trick is start with empty JSON object and add 'properties' property to it with a above constructed variable.

setProperty(
       json('{  }'), 
       'properties', 
        variables('PropertiesObjectVar')
)

Step #5 Now use Send HTTP to send an email.

Note: Copy the following in the Uri section

_api/SP.Utilities.Utility.SendEmail

Note: copy the following in the Headers section
{
   "Content-Type": "application/json;odata=nometadata",
   "Accept": "application/json;odata=nometadata"
}

Result

Using the setProperty, Compose and some tricks this posts may help you with constructing the JSON Object dynamically.

FYI the list of properties for the EmailProperties.

About Pankaj

I am a Developer and my linked profile is https://www.linkedin.com/in/pankajsurti/
This entry was posted in Power Automate. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s