I am trying to use javascript functions within an calculated column which is working pretty fine e.g. if I use the following formula:

="<img src='/_layouts/images/blank.gif' onload=""{"
&"  this.parentNode.innerHTML= new Date().toString() ;"

This gets converted to the following html code on the site:

<td class="ms-vb-lastCell ms-cellstyle ms-vb2 ms-vb-lastCell">
    <div align="right" class="ms-number">Fri Jun 17 11:36:29 UTC+0200 2016</div>

All fine with this. But when I declare and try to use a function, its not working anymore even its getting converted to an working javascript code.

I use the following formula:

&"function myfun(){"
&"  return new Date().toString();"

&"<img src='/_layouts/images/blank.gif' onload=""{"

&"  this.parentNode.innerHTML= myfun() ;"

This gets converted to:

<td class="ms-cellstyle ms-vb2">
    <div align="right" class="ms-number">
            function myfun(){   
                return new Date().toString();
        <img onload="{ this.parentNode.innerHTML= myfun();}" src="/_layouts/images/blank.gif">

But: the value is not displayed as you can see.

Any suggestions?



in June 2017, Microsoft disabled the use of JavaScript in a Calculated Column

That means given answers may not apply for newer SharePoint versions

For long explanation and work arounds see:
June 13th 2017 Microsoft blocked handling HTML markup in SharePoint calculated fields - how to get the same functionality back

**Original answer:**

Sometime summer 2013 Microsoft added checks which made the use of a SCRIPT tag inside Calculated Columns impossible.

So you have to rewrite it to:

&"<img src='/_layouts/images/blank.gif' onload=""{"

    &"function myfun(){"
    &"  return new Date().toString();"

&"  this.parentNode.innerHTML= myfun() ;"

Note that this will be added/executed for every ListItem, it works (And I have used it to the maximum of 4000 bytes per Calculated Column Formula) but will add that myFun() over and over again (in its own scope) So for lengthy codeyou might want to offload it to one script (load it once with a MasterPage Script, CustomAction, JSLink, or CEWP/SEWP or ..)



You can Add Javascript file using Content editor or Script editor on the list view Page. So now only function call needs to be made on image load functionality, which should work smoothly.

Kindly try to separate JS from the HTML content, this should work for you.


This may not be the most elegant way to do it, but I found I could make text columns whose default values are forbidden code and reference those text columns in calculated columns.

So you could:

Make a text column called ScriptStartTag with a default value of <script> , and another text column called ScriptEnd tag with a default value of </script>.

(New list items will take the default values in those columns, but you will have to enter the default values into existing list items.)

Compose the calculated column as follows:

=ScriptStartTag &"function myfun(){" &" return new Date().toString();" &"}" &ScriptEndTag

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.