I saw several threads on community forums that state “Xrm.Utility.getResourceString” functionality doesn’t work in Html webresources. I decided to spend some time digging to find out why it doesn’t work and how to make it work.
During investigation I found out that Html window missed several objects that were available on regular entity form. Unfortunately I was not able to find easy way to create those object so I composed solution that composes and initiates all the required objects on the form. Here is what should be done to make “Xrm.Utility.getResourceString” work again:
- download and import to your system following managed solution – https://github.com/a33ik/HtmlLocalization/releases/tag/2.0.0.0
- add a reference to “HtmlLocalization.js” to your Html webresource. Be attentive with relative paths when you reference this file!
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title></title> <script type="text/javascript" src="../ClientGlobalContext.js.aspx"></script> <script type="text/javascript" src="HtmlLocalization.js"></script> <script type="text/javascript" src="Localization.js"></script> </head> <body onload="Localization.LocalizeLabel();"> </body> </html> |
- add following code to your code before using “Xrm.Utility.getResourceString”. As a parameter for AB.HtmlLocalization.Initialize I pass an array of JavaScript that have “Resx” dependent webresources. It builds required structure of objects in window and returns promise.
1 2 3 4 5 6 7 8 |
AB.HtmlLocalization.Initialize(["ab_/Localization.js"]) .then(function() { var a = Xrm.Utility.getResourceString("ab_/Messages", "LocalizedMessage"); console.log("Localized Value - " + a); }, function(e) { console.log(e); }); |
This is the first release of the product and I haven’t done massive testing so if you found a bug or have suggestions feel free to leave your comment here or open an issue on the GitHub repository.
Hi,
I faced the same problem in CRM V9.0 and find that if you give the full name of the resource file, it returns the value. First, I add the Resource file as a dependency to the Html file. Then call the getResourceString method using the full name of the resource file.
the full name of the resource file is: ab_/Messages.1033
Working code:
var userLCID = Xrm.Page.context.getUserLcid();
var noDataMessage = Xrm.Utility.getResourceString(‘ab_/Messages.’ + userLCID, ‘LocalizedMessage’);
Not Working Code:
var noDataMessage = Xrm.Utility.getResourceString(‘ab_/Messages.’ + userLCID, ‘LocalizedMessage’);
Sema,
And you’re saying it will work in Html webresource without any additional injections?
Andrew
This solution doesn’t need additional JS/Plugin:
return window.parent.Xrm.Utility.getResourceString(“xx_x”, messageKey);
Hello Robert,
Thanks for your comment! What is your scenario are you talking specifically? Did you do any additional configurations in order to make it work?
Thanks,
Andrew
Same scenario of HTM web resource in a Form accessing RESX content from the corresponding JS referenced in the HTM web resource. With ClientGlobalContext.js.aspx loaded, it only needs window.parent to access Xrm.Utility.getResourceString as long as the RESX files are configured as dependencies on the HTM web resource. Without window.parent it is not properly initialized in the current window as you found out.
Robert,
That could work. Thanks for the tip!
Andrew