Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics 365 | Integration, Dataverse...
Answered

Get Webresource content from JavaScript

(0) ShareShare
ReportReport
Posted on by 10

Hello there,

I have a button on a ribbon which generates an eml file and downloads it. The goal is to generate an e-mail template based on field values, entity status, etc.

The e-mail templates I have to use are long formatted text, with different fonts, colors, etc., so I put them in different html webresources.

In my JavaScript function "generateEml", I compose the eml file, and I have to get the content of an html webresource, but I'm unable to do it.

Here's the function:

function generateEml(object, emailBody, id) {
const emailTo = ""
let emlCont = 'To: ' + emailTo + '\n'

emlCont += 'Subject: ' + object + '\n'
emlCont += 'X-Unsent: 1' + '\n'

emlCont += 'Content-Type: text/html' + '\n'
emlCont += '\n'

emlCont += "<!DOCTYPE html>" + emailBody
console.log(emailBody)

let textFile = null
const data = new Blob([emlCont], { type: 'text/plain' })
if (textFile !== null) window.URL.revokeObjectURL(textFile)

textFile = window.URL.createObjectURL(data)

const a = document.createElement('a')
const linkText = document.createTextNode(id)
a.appendChild(linkText)

a.href = textFile

a.id = id
a.download = object + ".eml"
a.style.visibility = "hidden"

document.body.appendChild(a)

document.getElementById(id).click()
}
For example if I call generateEml("Greetings", "<html><head></head><body>Hey there, my name is Raffaele</body></html>", 1), it works.
What I have to do is something like:
const emailTemplate = getContentFromWebresource("../html/templates/greetingTemplate.html")
generateEml("Greetings", emailTemplate1)
I didn't find any supported way to do that...
Thank you
  • RE: Get Webresource content from JavaScript

    Thank you, it works!

  • Verified answer
    Guido Preite Profile Picture
    Guido Preite 54,069 Super User 2024 Season 1 on at
    RE: Get Webresource content from JavaScript

    so you are looking at the code for the function called getContentFromWebresource right?

    you can't do a retrieve but a retrieve multiple as you are looking for the name,

    the code will be something like

    fetch(Xrm.Utility.getGlobalContext().getClientUrl()   "/api/data/v9.2/webresourceset?$select=content,name&$filter=contains(name,'[NAME OF THE WEB RESOURCE HERE]')", {
    	method: "GET",
    	headers: {
    		"OData-MaxVersion": "4.0",
    		"OData-Version": "4.0",
    		"Content-Type": "application/json; charset=utf-8",
    		"Accept": "application/json",
    		"Prefer": "odata.include-annotations=*"
    	}
    }).then(
    	function success(response) {
    		return response.json().then((json) => { if (response.ok) { return [response, json]; } else { throw json.error; } });
    	}
    ).then(function (responseObjects) {
    	var response = responseObjects[0];
    	var responseBody = responseObjects[1];
    	var results = responseBody;
    	console.log(results);
    	for (var i = 0; i < results.value.length; i  ) {
    		var result = results.value[i];
    		// Columns
    		var webresourceid = result["webresourceid"]; // Guid
    		var content = result["content"]; // Text
    		var name = result["name"]; // Text
    	}
    }).catch(function (error) {
    	console.log(error.message);
    });

    and the content field contains the base64 encoded text, that you need to decode

    hope it helps

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Congratulations 2024 Spotlight Honorees!

Kudos to all of our 2024 community stars! 🎉

Meet the Top 10 leaders for December!

Congratulations to our December super stars! 🥳

Get Started Blogging in the Community

Hosted or syndicated blogging is available! ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,622 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,354 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans