Forum Discussion
XML Data in Deep Link to Personal Tab has to be URI Encoded Twice?
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.
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 the subEntityId
is part of the context
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.