I'm using ThingWorx 9.3.9
My service creates a file in a file repository. I would then like the service to email a link to the file that the user can click on to download the file.
I've tried using the repository's GetFileListingWithLinks service that returns an infotable that has a hyperlink field named downloadLink. However, the value that is returned is in this format:
/Thingworx/FileRepositories/MLLC.Common.Repository/LineZRouteID_2025-08-07.csv
When I email that value Outlook does not recognize that value as a link. So, I think I need to somehow convert it to this format:
http://thingworxu15/Thingworx/FileRepositories/MLLC.Common.Repository/LineZRouteID_2025-08-07.csv
Here's my code. The commented lines show the ways I tried to get it to email a proper link.
// result: INFOTABLE dataShape: "FileSystemFileWithLinks"
let fileInfo = Things["MLLC.Common.Repository"].GetFileListingWithLinks({
path: "/" /* STRING */ ,
nameMask: validationID + "_" + formattedDate + ".csv" /* STRING */
});
//linkToCSV = "<a href=" + fileInfo.downloadLink + "></a>";
//linkToCSV = "<a href=" + fileInfo.downloadLink.toString() + "></a>";
//linkToCSV = fileInfo.downloadLink.removeAttribute('href');
linkToCSV = fileInfo.downloadLink;
let body = "<html><body>" + linkToCSV + "</body></html>";
// send message
Things[me.emailThingName].SendMessage({
cc: undefined /* STRING */ ,
bcc: undefined /* STRING */ ,
subject: validationID + " Updates Needed" /* STRING */ ,
from: undefined /* STRING */ ,
to: emailList /* STRING */ ,
body: body /* HTML */
});
However, this is what I get - no link.
I know that I can build the link, but that would require the code to be updated with the new host as we move from Dev to Test to Production. Something, I'd like to avoid.
Thanks,
Steve
Solved! Go to Solution.
The server does not know the URL itself. You have to set it somewhere manually and adapt it for each instance.
Our best practice is to create an "Environment"-Thing with a ConfigurationTable which holds different useful values like hostname, envName, sendEmails,.. These can be then used by different UseCases to get the information.
In your service you would then do a
let link = "https://"+ Things["EnvironmentThing"].GetConfigTable().hostName + fileInfo.downloadLink; // (pseudocode)
but you can also use a property or something else to store the information. (But properties may change value on extension update, so configtable is our prefference).
The server does not know the URL itself. You have to set it somewhere manually and adapt it for each instance.
Our best practice is to create an "Environment"-Thing with a ConfigurationTable which holds different useful values like hostname, envName, sendEmails,.. These can be then used by different UseCases to get the information.
In your service you would then do a
let link = "https://"+ Things["EnvironmentThing"].GetConfigTable().hostName + fileInfo.downloadLink; // (pseudocode)
but you can also use a property or something else to store the information. (But properties may change value on extension update, so configtable is our prefference).
Hello @steve237
Have you considered using:
"IPAddress = Resources["CurrentSessionInfo"].GetCurrentIPAddress();"
This does not get the entire URL but it does get part. There are 2 items which I think you might be missing.
The protocal: "http" or "https"
The port number would be the other item which you seem to be missing.
I am checking to see if that is available?
Regards,
pehowe
Hello @steve237 ,
I did a little example and using http worked.
Please review the information presented and let me know if you have any questions.
Regards,
Pehowe
Thanks, but when I tried using your suggestion (IPAddress = Resources["CurrentSessionInfo"].GetCurrentIPAddress();) it returned the IP address of my laptop, not the server,
Hello @steve237 ,
I have looked into this issue. The ThingWorx function "GetFileListingWithLinks" Builds the end of a URL to access a file.
The question you ask is in ThingWorx, can click on this link and it provides the requested file. When you tried to send that information in an EMail, the link is no longer complete. When in ThingWorx the results of the function "GetFileListingWithLinks" are passed to the Tomcat instance. The Tomcat instance, then adds the protocol and FQDN.
There is no method in ThingWorx to get the Protocal and Fully Qualified Domain Name (FQDN) for the ThingWorx instance.
The only solution is to build this information from you code.
The function I suggested to the the IP address is also not going to be of any help for your case.
Regards,
Pehowe