Creating Teams channels in a Power Automate Flow: How to avoid unsupported characters errors

If you want to automate Microsoft Teams Channels using Power Automate Flows, you can use a simple action named ‘Create a channel’ from the Teams Connector in your Flows (still in preview at the time of writing this post). 000

But due to naming restrictions, if you use unsupported characters (~#%&*{}+/\:<>?|'”..) in the channel name, you will get errors related to that and the channel is not created. Of course, those could be blocked in the source for the channel name outside of the Flow, but in a scenario this is not manipulated before, ideally, we want to remove them in the Flow to avoid the errors.

Check this post if you want to know how to remove those special characters from your channel name in a simple way before creating it in a Power Automate Flow:

Sample Flow trigger and Source

Scenario: Under a Microsoft Teams related SharePoint team site, there is a list with a register of critical cases. When an item is added to this list, we want to provision a new channel in teams to discuss it. The channel name should be the value from the Title field.

To begin the Flow, use the trigger When an item is created selecting the list:Capture0

Storing content in variables

To be able to manipulate the channel name, store the Title field value in a variable named ChannelName.Capture0b

Then initialize a variable named UnsupportedCharacters with the type Array and having as content the unsupported characters:

[“~”, “#”, “%”, “&”, “*”, “{“, “}”, “+”, “/”, “\\”, “:”, “<“, “>”, “?”, “|”, “‘”,  “\””,”.”,”_”]

Capture1a
Manipulating channel name

The trick to simplifying character manipulation is to loop through the array of unsupported characters and remove them from the ChannelName variable. As it’s not possible to self-reference variables in Flows, we process the manipulated value using the compose action and then assign it back to the variable. Of course, we could use nested character replacements instead of this approach, but this one is more readable and maintainable.

Formula for the compose action (remove the unsupported char from the channel name and trim the string if it ends with spaces after the replacement):

trim(replace(variables(‘ChannelName’),items(‘Apply_to_each’),”))

By calling the items(‘Apply_to_each’) expression, we are accessing the item from the current iteration in the loop.

Capture2

Important note: Enable concurrency in the Apply to each loop and set the degree of parallelism to 1. This makes sure the ChannelName variable is manipulated in sequence (no concurrent calls to set variable function will happen):
Capture1
After running the loop and the name for the channel is ‘clean’, one more check is needed to create it without having errors.
The channel name shouldn’t be bigger than 50 chars. So we simply truncate the ChannelName variable to 50 chars if it’s bigger than that by using the following formula as the Display Name for the channel when creating it:

if(greater(length(variables(‘ChannelName’)),50),  substring(variables(‘ChannelName’),0,50) ,variables(‘ChannelName’))

Capture3

To wrap up, why not posting a message to the team explaining the reason for that channel to exist?

Capture4

The final layout for the Flow is as follows:

final

Result

After any item is created in that list even with the problematic characters, a channel will be provisioned, and a message will be sent to the team.

For example, if a list item with the following title is created:

_A long name with characters as ~#%&*{}+/\:<>?|'”.. shouldn’t be creating my channel..but I just want to see if the flow that I’ve just built to create the channel anyway really works!…

A teams channel will be properly provisioned removing unwanted characters (with a truncated name) and a message will be sent to the channel:

message

Note: Channel names can’t begin or end with a single dot (‘.’), and can’t begin with an underscore (‘_’) also. Those are already included in the UnsupportedCharacters array to simplify the logic. If you want to remove them only from the beginning or end of the sentence, remove those characters from the array declaration (and add ‘..’, which is also not supported but would be removed by having the character ‘.’ in the list), and after the loop, you can do separate checks for those and use the same technique of a Compose action (with the substring expression instead of replace in this case) and a Set variable action for the ChannelName variable to the output of a Compose action.

Hope this post helps you to start automating some actions in Microsoft Teams!

2 comments

Comments are closed.