We help IT Professionals succeed at work.

HTML Calculated Column and ID Field Problem

Grontmij_UK
Grontmij_UK asked
on
I am using the code below to convert a calculated column in a SharePoint 2010 list into HTML.

 
// Copyright (c) 2010 Christophe Humbert - Path to SharePoint

// Find all Web Parts in the page
var listWP=[],calWP=[],divs=document.getElementById("MSO_ContentTable").getElementsByTagName("div");
var count=divs.length;
for (i=0;i<count;i++) {
try {
if (divs[i].id.indexOf("WebPartWPQ")==0){
if (divs[i].innerHTML.indexOf("ViewDefault_CalendarView")>=0) {
// Calendars
calWP.push(divs[i].id);
}
else {
// Other Web Parts
listWP.push(divs[i].id);
}
}
}
catch(e){}
}

function TextToHTML(NodeSet, HTMLregexp) {
var CellContent = "";
var i=0;
while (i < NodeSet.length){
try {
CellContent = NodeSet[i].innerText || NodeSet[i].textContent;
if (HTMLregexp.test(CellContent)) {NodeSet[i].innerHTML = CellContent;}
}
catch(err){}
i=i+1;
}
}

var regexpA = new RegExp("\\s*<([a-zA-Z]*)(.|\\s)*/\\1?>\\s*$");
var regexpTD = new RegExp("^\\s*<([a-zA-Z]*)(.|\\s)*/\\1?>\\s*$");

var WP = new Object;

function UpdateWP() {
if (calWP.length>0){
for (i=0;i<calWP.length;i++) {
WP=document.getElementById(calWP[i]);
if (WP.innerHTML.indexOf("&lt\;")>=0) {TextToHTML(WP.getElementsByTagName("a"),regexpA);}
}
}
if (listWP.length>0){
for (i=0;i<listWP.length;i++) {
WP=document.getElementById(listWP[i]);
if (WP.innerHTML.indexOf("&lt\;")>=0) {TextToHTML(WP.getElementsByTagName("td"),regexpTD);}
}
}
// Check every 200 ms, forever
setTimeout("UpdateWP()",200);
}
UpdateWP();

</script>

Open in new window


Within the calculated column, I use the row ID field to append to the end of a URL like this:

=CONCATENATE("<DIV><a href='http://mysite/newform.aspx?ClientID=",[ID],"'>add</a></DIV>")

This works great and formats my calculated field as HTML like this:

http://mysite/newform.aspx?ClientID=8

However, when the list item is edited, the calculated field does not render the ID of the row and so looks like this:

http://mysite/newform.aspx?ClientID=

To make the ID appear again, I then have to edit the column and save the changes - then it appears again.

However, if I replace the ID field with another field, this problem does not happen!  For example, I can change the calculated field to this:

=CONCATENATE("<DIV><a href='http://mysite/newform.aspx?ClientID=",[City],"'>add</a></DIV>")

This will consistently produce the following link:

http://mysite/newform.aspx?ClientID=London

Also, if I add a calculated field for just the ID it returns this perfectly each time.

Is there something I am doing wrong?

Thanks!
Comment
Watch Question

Top Expert 2011
Commented:
You are not doing  anything wrong. This is known behavior.
Workaround:
Create a new column called MyID. Then create an eventhandler or a SP designer WF to populate this filed on item creation. Now use this filed in your calculated fields.

Author

Commented:
Thank you!

Explore More ContentExplore courses, solutions, and other research materials related to this topic.