We help IT Professionals succeed at work.
Get Started

HTML Calculated Column and ID Field Problem

Grontmij_UK
Grontmij_UK asked
on
718 Views
Last Modified: 2012-05-12
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:
This problem has been solved!
Unlock 1 Answer and 2 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE