Forum Discussion
XML Data in Deep Link to Personal Tab has to be URI Encoded Twice?
Hello ssj_springctin - Thanks for raising your query.
You need to encode both webUrl and data context.
Please refer this code:
var encodedWebUrl = encodeURIComponent('https://tasklist.example.com/123/456&label=Task 456');
var encodedContext = encodeURIComponent(JSON.stringify({"subEntityId": "task456"}));
var taskItemUrl = 'https://teams.microsoft.com/l/entity/fe4a8eba-2a31-4737-8e33-e5fae6fee194/tasklist123?webUrl=' + encodedWebUrl + '&context=' + encodedContext;
Reference doc: Deep link to an application - Teams | Microsoft Learn
Could you please share your valuable feedback via Microsoft Teams Developer Feedback link.
Please let us know if you have any further query.
Thanks!!
- ssj_springctinOct 31, 2023Copper Contributor
Hi ChetanSharma-msft ,
Thank you for your reply.I had referred the doc you mentioned.
But if you notice in the snippet you shared, neither the encodedContext nor encodeWebUrl are embedded in the other.In my use case, the encoded XML string has to be embedded within the context, which is then encoded again. The XML data is essentially encoded twice.
This is not an issue for me per se. I just want to be sure that this is expected behavior, and that I am not missing anything.
- SaiPratap-MSFTNov 06, 2023Microsoft
Hello ssj_springctin -
The behavior you're experiencing is expected. The
subEntityId
field in the context object is a string, and it's used to identify a specific item within your tab. When you're passing XML data or any other special characters in the URL, it's necessary to encode it to ensure that the URL is correctly parsed.In your case, you're encoding the XML data and then encoding it again when it's included in the
subEntityId
field. This is necessary because thesubEntityId
is part of thecontext
query parameter in the URL, which is a JSON object. The JSON object itself needs to be encoded to be correctly included in the URL, and any special characters within the JSON object also need to be encoded to ensure they're correctly included in the JSON object.Here's your code with comments explaining each step:
// Encode the XML data to ensure any special characters are correctly included in the JSON object const encodedData = encodeURIComponent(xmlData); // Create the context JSON object, including the encoded XML data in the subEntityId field // Then encode the entire JSON object to ensure it's correctly included in the URL const encodedContext = encodeURIComponent(`{"subEntityId":"${encodedData}"}`); // Create the deep link, including the encoded context object in the context query parameter const deepLink = `https://teams.microsoft.com/l/entity/${process.env.APP_ID}/${process.env.ENTITY_ID}?&context=${encodedContext}`;
In your Teams personal tab, you can then decode the
subEntityId
twice to retrieve the original XML data:// Get the context object const context = microsoftTeams.getContext(); // Decode the subEntityId field to retrieve the encoded XML data const encodedData = decodeURIComponent(context.subEntityId); // Decode the encoded XML data to retrieve the original XML data const xmlData = decodeURIComponent(encodedData);
This double encoding and decoding is necessary because you're including special characters (from the XML data) within a JSON object within a URL, each of which requires its own encoding.