Write Html code in SharePoint 2010

April 12th, 2011 | Posted by Marijn in customization | jquery / javascript | 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!!

You can follow any responses to this entry through the RSS 2.0 You can leave a response, or trackback.

4 Responses



Leave a Reply

%d bloggers like this: