View Customization With JavaScript In Dynamics 365

In Dynamics 365, now you can even use JS scripting in Views to add icons in the view cells.

What is offered?

Dynamics 365
As you can see in the above image, those icons in View cells were already available in some OOB Views. Now, you can use it in custom View as well.

Where to customize?

Dynamics 365

Goto any View’s property, you can see 2 new fields - Web Resource and Function Name, very similar to forms when you write JS.

What to write in Web Resource?

You need to write a JS function with 2 parameters, when you’ll add it to view, first parameters will receive rowData(value with metadata shown below for one cell) and userLCID(i.e. language code of the current user). Based on those 2 inputs, you can decide what icon to show. And the name of this function goes in Function Name: field in View’s property.

  1. /// rowData for just one cell  
  2. {  
  3.    "RowId":"{B69E62A8-90DF-E311-9565-A45D36FC5FE8}",  
  4.    "RowType":"112",  
  5.    "title":"Average order shipment time",  
  6.    "title_Value":"Average order shipment time",  
  7.    "ticketnumber":"CAS-01213-P8B3X0",  
  8.    "ticketnumber_Value":"CAS-01213-P8B3X0",  
  9.    "prioritycode":"Normal",  
  10.    "prioritycode_Value":2,  
  11.    "caseorigincode":"Web",  
  12.    "caseorigincode_Value":3,  
  13.    "customerid":"Litware",  
  14.    "customerid_Value":{  
  15.       "Data":{  
  16.          "Type":1,  
  17.          "Name":"Litware",  
  18.          "ID":"{BCA19CDD-88DF-E311-B8E5-6C3BE5A8B200}",  
  19.          "Dsc":0  
  20.       },  
  21.       "NeedsIconRendering":false  
  22.    },  
  23.    "ownerid":"Christa Geller (Sample Data)",  
  24.    "ownerid_Value":{  
  25.       "Data":{  
  26.          "Type":8,  
  27.          "Name":"Christa Geller (Sample Data)",  
  28.          "ID":"{C02D43FF-16A8-4E1E-8DD1-0B78EB058AB4}",  
  29.          "Dsc":0  
  30.       },  
  31.       "NeedsIconRendering":false  
  32.    },  
  33.    "statecode":"Active",  
  34.    "statecode_Value":"Active",  
  35.    "createdon":"1/21/2017 4:20 AM",  
  36.    "createdon_Value":"2017-01-21T04:20:45+00:00",  
  37.    "processid":"{0FFBCDE4-61C1-4355-AA89-AA1D7B2B8792}",  
  38.    "processid_Value":"0ffbcde4-61c1-4355-aa89-aa1d7b2b8792",  
  39.    "processts":"424505",  
  40.    "processts_Value":null,  
  41.    "processid_Hidden":"0ffbcde4-61c1-4355-aa89-aa1d7b2b8792",  
  42.    "processts_Hidden":"424505"  
  43. }  

Let’s Implement!

I’ll be doing all the customizations in "All Cases" view of Case entity. So, you can try it right away in the new trial instance without creating any custom Entity/View.

Add one JS web resource

Dynamics 365

And write the below code in it.

  1. function casePriorityIcons(rowData, userLCID) {  
  2.   var data = JSON.parse(rowData);  
  3.   var priority = data.prioritycode;  
  4.   var imgWebResource = "";  
  5.   var imgTooltip = "";  
  6.   switch (priority) {  
  7.     case "High":  
  8.       imgWebResource = "ashv_high";  
  9.       imgTooltip = "High Priority Case";  
  10.       break;  
  11.     case "Low":  
  12.       imgWebResource = "ashv_low";  
  13.       imgTooltip = "Low Priority Case";  
  14.       break;  
  15.     default:  
  16.       imgWebResource = "ashv_normal";  
  17.       imgTooltip = "Normal Priority Case";  
  18.       break;  
  19.   }  
  20.   return [imgWebResource, imgTooltip];  
  21. }  

Dynamics 365

In the above code, I’m reading the cell data which is priority field of a case, and according to value, it will show color icon in front of it. I have added three 16x16 (if you’ll take different size it will be scaled back to 16x16, so better take optimized size) icons with color Red, Yellow, & Green for High, Normal, & Low priorities respectively. The function returns an array of size 2; the first element tells the name of image web resource to show and the second is tooltip text. You can use the below images in your web resource.

High : Dynamics 365 Normal : Dynamics 365 Low : Dynamics 365

I’ve set the Web resource and function name in "All Cases" view property for Priority field.

Dynamics 365

Publish everything and navigate to "All Cases" view.

Dynamics 365

Hooray! It’s working! I didn’t play anything with the second parameter, i.e., LCID. You can explore use cases and try.

Looks good, but what happens behind the scenes is - All this magic happens in layout XML of View you can see below.

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <grid name="resultset" object="112" jump="title" select="1" icon="1" preview="1">  
  3.    <row name="result" id="incidentid">  
  4.       <cell name="title" width="300" />  
  5.       <cell name="ticketnumber" width="125" imageproviderfunctionname="" imageproviderwebresource="$webresource:" />  
  6.       <cell name="prioritycode" width="150" imageproviderfunctionname="casePriorityIcons" imageproviderwebresource="$webresource:ashv_ViewCustomizations" />  
  7.       <cell name="caseorigincode" width="100" />  
  8.       <cell name="customerid" width="150" />  
  9.       <cell name="ownerid" width="150" />  
  10.       <cell name="statecode" width="150" />  
  11.       <cell name="createdon" width="150" />  
  12.    </row>  
  13. </grid>  

Here, <cell name="prioritycode" width="150" imageproviderfunctionname="casePriorityIcons" imageproviderwebresource="$webresource:ashv_ViewCustomizations" /> got 2 extra attributes with the name of web resource and function. They set the image source and title while rendering the View (see image below). 

Dynamics 365

Feel free to get in touch with me to resolve any query regarding this article.


Similar Articles