To Replace The Labels In Entity Form Using JavaScript In Dynamics CRM

In this article, I have explained the steps to replacing the labels in an Entity Form, using JavaScript, XML and CSS files in Dynamics 365.

The total procedure contains 3 steps, which are given below.

  1. XML file contains the label names to be replaced.
  2. Two JavaScript files are used here, where one is to replace the label and the other one is to load CSS dynamically.
  3. CSS file to change the styles of labels to wrap the text in the label.

XML filename

Tasks.XML

  1. <?xml version="1.0" ?>  
  2. <Tasks>  
  3.     <TaskName id="PnP">Promoted best practices, such as design, error handling and documentation. </TaskName>  
  4.     <TaskName id="Architecture">To build the process flow and infrastructure required for system. </TaskName>  
  5.     <TaskName id="SustainPlan">Worked on a sustainability plan</TaskName>  
  6. </Tasks>  

JavaScript files given below will take care of the label text change.

JavaScript

ReplaceLabels.js

  1. function ChangeLabelTaskNameFrmXML() {  
  2.     var xmlPath = "../WebResources/new_Tasks.xml";  
  3.     //Put the <TaskName> element into an object.  
  4.     var xmlnodePath = "//Tasks/TaskName";  
  5.     var xmldoc = new ActiveXObject("Microsoft.XMLDOM");  
  6.     xmldoc.preserveWhiteSpace = true;  
  7.     xmldoc.async = false;  
  8.     xmldoc.load(xmlPath);  
  9.     //Extract the different values using a loop.  
  10.     var xmlnodelist;  
  11.     xmlnodelist = xmldoc.selectNodes(xmlnodePath);  
  12.     for (var i = 0; i < xmlnodelist.length; i++) {  
  13.         var lbltaskname = xmlnodelist(i).getAttribute('id');  
  14.         var newlbltaskname = xmlnodelist(i).text;  
  15.         ChangeLabelTaskName(lbltaskname, newlbltaskname);  
  16.     }  
  17. }  
  18.   
  19. function ChangeLabelTaskName(lblTaskName, newlblTaskName) {  
  20.     var lbl = "new_" + lblTaskName.toLowerCase();  
  21.     Xrm.Page.ui.controls.get(lbl).setLabel(newlblTaskName);  
  22. }  

The JavaScript given below will load CSS (Online or On-Premises), which contains the style to wrap the text in the label.

LoadCSS.js

  1. function LoadCSS(path) {  
  2.     var head = window.parent.document.getElementsByTagName('head')[0];  
  3.     var link = window.parent.document.createElement('link');  
  4.     link.rel = 'stylesheet';  
  5.     link.type = 'text/css';  
  6.     link.href = path;  
  7.     link.media = 'all';  
  8.     window.parent.document.head.appendChild(link);  
  9. }  

Web Resources

To upload files (JavaScript, XML and CSS) to Web Resource, proceed, as shown below.

While uploading the files to Web Resource, you should provide few values like name, display name, file type and subsequently upload the file, using Browse button.

Form Properties

After uploading the files to Web Resources, you must select Entity Form, where the labels must be replaced.

Now, go to Form Properties, as shown above and add required JavaScript files, as shown below. Once you have included JavaScript files, you must map the functions from JavaScript and provide parameter values in Event Handlers section, as shown in the screenshots given below.


Please remember that we need to call these JavaScript functions on Form load (Control = Form, Event= OnLoad). CSS should be loaded first, followed by replacing label text.

Here, the function name is ChangeLabelTaskNameFrmXML and the parameters are provided as comma separated values.

Hope, this article will help you to solve the issue in replacing form control labels with the desired text.

Up Next
    Ebook Download
    View all
    Learn
    View all