cancel
Showing results for 
Search instead for 
Did you mean: 
codevenkat

Usage of Encode Uri in Power Automate

Introduction:

In Power Automate, to return a uniform resource identifier (URI) encoded version for a string by replacing URL-unsafe characters with escape characters we can use either encodeUriComponent or uriComponent functions in expressions.

 

Step 1:

Login to the required Power Apps environment using URL make.powerapps.com by providing user name and password and click on Flows on the left hand side as shown in the below figure.

codevenkat_0-1627720010219.jpeg

 

 

Step 2:

After Step 1, click on New Flow and then Instant Flow and under option Choose how to trigger the flow select Manually tigger a flow and click on Create button as shown in the below figure.

codevenkat_1-1627720020236.jpeg

 

Step 3:

After Step 2, provide name as Encode Uri for the flow and then select an operation with name Initialize variable Action  as shown in the below figure.

codevenkat_2-1627720041431.jpeg

 

Step 4:

After Step 3, Initialize variable is used to hold url that needs to be encoded, so provide the name as Uri and Type as String and Value as https://venkatasubbaraopolisetty.com/ and click on New Step as shown in the below figure.

codevenkat_3-1627720049383.jpeg

 

Step 5:

After Step 4, select an Action Compose under Choose an operation as shown in the below figure.

codevenkat_4-1627720057747.jpeg

 

Step 6:

After Step 5, under Inputs  click on Expression and type  

encodeUriComponent(variables('Uri'))

here encodeUriComponent() is the function and variables (‘Uri’) is the variable value which we received from step 4 as shown in the below figure.

codevenkat_5-1627720065681.jpeg

 

Step 7:

After Step 6, Click on Save button and click on Test as shown in the below figure.

codevenkat_6-1627720071289.jpeg

 

Step 8:

After Step 7, Click on Manually and Click on Test  as shown in the below figure.

codevenkat_7-1627720080101.jpeg

 

Step 9:

After Step 8, Click on Run Flow  and you could see below screen and click on Done as shown in the below figure.

codevenkat_8-1627720110575.jpeg

 

Step 10:

After Step 9, input initialize variable with Uri turned to encoded uri which is encoded version for a string by replacing URL-unsafe characters with escape characters as shown in the below figure.

codevenkat_9-1627720116717.jpeg

 

Step 11:

After Step 10, we can also use another recommended function uriComponent by Microsoft by providing under compose operation under expression to encode

uriComponent(variables('Uri'))

 

which will encode to equivalent encoded version for a string by replacing URL-unsafe characters with escape characters and save and Test (Repeat Steps 8 and 9)and can see same expected result as shown in the below figure.

codevenkat_10-1627720130590.jpeg

 

Step 12:

After Step 11, one can find both Power Automate functions provided same result as shown in the below figure.

codevenkat_11-1627720138789.jpeg

 

Note:

  1. Make sure to save and run the flow whenever you try expressions.
  2. MS documentation is found at https://docs.microsoft.com/en-us/azure/logic-apps/workflow-definition-language-functions-reference#u...
  3. As an example my website is taken into consideration to provide as variable in Step 4, you can provide any other value of your choice.

 

Conclusion:  In this way one can easily generate encoded version for a string by replacing URL-unsafe characters with escape characters.

 

Comments
Anonymous

Hi,
can you explain why characters like - and _ are not encoded correctly while = is encoded. 
I try to encode a ConversationID from an outlook action in power automate. 

Hi @Anonymous :

 

-_.!~*'(). If you want to these characters are encoded, you have to replace them with corresponding UTF-8 sequence of characters

An ideal c# code looks like below

const encode = str => encodeURIComponent(str)
.replace(/\\-/g, '%2D')
.replace(/\\_/g, '%5F')
.replace(/\\./g, '%2E')
.replace(/\\!/g, '%21')
.replace(/\\~/g, '%7E')
.replace(/\\*/g, '%2A')
.replace(/\\'/g, '%27')
.replace(/\\(/g, '%28')
.replace(/\\)/g, '%29');

So , you have to try like this expressions in your power automate flow and do testing it

 

Anonymous

Hi @codevenkat 

 

thanks for your tip. In the meantime I've already figured out that I need to set replace rules for this to archive. 

This works also fine, the characters are encoded.

 

Unfortunately Edge automatically decodes these characters again when using the created string as an URL so my initial plan to generate a OWA deeplink to a mail does not work. 

I assume MS is using - and _ in their Mail conversation IDs on purpose to prevent exactly what I try to do.

Anonymous

Cheers for this - it was really useful!  I guess some special characters are handled more equally than others!  Our Dev and Stage testing didn't flag any issues but once in Production we hit a few odd issues with names.

@Anonymous  : Good to know that

 

Anonymous

Actually in the end we went with FetchXML instead of ODATA and then only replaced ampersands if present.  This seemed like the most resilient and least fiddly way forward.

About the Author
  • Experienced Consultant with a demonstrated history of working in the information technology and services industry. Skilled in Office 365, Azure, SharePoint Online, PowerShell, Nintex, K2, SharePoint Designer workflow automation, PowerApps, Microsoft Flow, PowerShell, Active Directory, Operating Systems, Networking, and JavaScript. Strong consulting professional with a Bachelor of Engineering (B.E.) focused in Information Technology from Mumbai University.
  • I am a Microsoft Business Applications MVP and a Senior Manager at EY. I am a technology enthusiast and problem solver. I work/speak/blog/Vlog on Microsoft technology, including Office 365, Power Apps, Power Automate, SharePoint, and Teams Etc. I am helping global clients on Power Platform adoption and empowering them with Power Platform possibilities, capabilities, and easiness. I am a leader of the Houston Power Platform User Group and Power Automate community superuser. I love traveling , exploring new places, and meeting people from different cultures.
  • Read more about me and my achievements at: https://ganeshsanapblogs.wordpress.com/about MCT | SharePoint, Microsoft 365 and Power Platform Consultant | Contributor on SharePoint StackExchange, MSFT Techcommunity
  • Encodian Owner / Founder - Ex Microsoft Consulting Services - Architect / Developer - 20 years in SharePoint - PowerPlatform Fan
  • Founder of SKILLFUL SARDINE, a company focused on productivity and the Power Platform. You can find me on LinkedIn: https://linkedin.com/in/manueltgomes and twitter http://twitter.com/manueltgomes. I also write at https://www.manueltgomes.com, so if you want some Power Automate, SharePoint or Power Apps content I'm your guy 🙂
  • I am the Owner/Principal Architect at Don't Pa..Panic Consulting. I've been working in the information technology industry for over 30 years, and have played key roles in several enterprise SharePoint architectural design review, Intranet deployment, application development, and migration projects. I've been a Microsoft Most Valuable Professional (MVP) 15 consecutive years and am also a Microsoft Certified SharePoint Masters (MCSM) since 2013.
  • Big fan of Power Platform technologies and implemented many solutions.
  • Passionate #Programmer #SharePoint #SPFx #M365 #Power Platform| Microsoft MVP | SharePoint StackOverflow, Github, PnP contributor
  • Web site – https://kamdaryash.wordpress.com Youtube channel - https://www.youtube.com/channel/UCM149rFkLNgerSvgDVeYTZQ/