Write Html code in SharePoint 2010

What are you talking about?
Last year I wrote a blogpost concerning the PathToSharePoint solution to parse html on a SharePoint page. That solution was great and was working really well. It wasn’t however working in SharePoint 2010.
They have released a new script, specifically for SharePoint 2010!

What is it ?
Say, you want to create your own hyperlink, with parameters (to prepopulate some fields for instance)
You can create a string in a calculated field that builds up your url using fields from your list.
That makes the solution very user friendly.

The script

<script type="text/javascript">

// 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>

Don’t forget to delete the spaces in the <script> fields!!

About: Marijn

Marijn Somers (MVP) has over 14 years experience in the SharePoint world, starting out with SP2007. Over the years the focus has grown to Office 365, with a focus on collaboration and document management. He is a business consultant at Balestra and Principal Content Provider for "Mijn 365 Coach" that offers dutch employee video training. His main work tracks are around user adoption, training and coaching and governance. He is also not afraid to dig deeper in the technicalities with PowerShell, adaptive cards or custom formatting in lists and libraries. You can listen to him on the biweekly "Office 365 Distilled" podcast.


4 thoughts on “Write Html code in SharePoint 2010”

  1. Hullo! This is a great script (very useful!), thanks for providing it! Just a quick note, tho – it's not pasted properly onto the page. I had to hit the page source code and tease out the full thing.

  2. This script has some error, i receive the message: "Mising ;" I can´t find where…

Leave a Reply

Your email address will not be published. Required fields are marked *