web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :
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
I have the same question (0)
  • Verified answer
    Guido Preite Profile Picture
    54,086 Moderator 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

  • Raffaele Costanzo Profile Picture
    10 on at
    RE: Get Webresource content from JavaScript

    Thank you, it works!

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Microsoft Dynamics 365 | Integration, Dataverse, and general topics

#1
#ManoVerse Profile Picture

#ManoVerse 101

#2
Siv Sagar Profile Picture

Siv Sagar 93 Super User 2025 Season 2

#3
Martin Dráb Profile Picture

Martin Dráb 66 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans