| | Stumble It! | Add to Mixx! | | diigo it | | Slashdot |

Wednesday, July 22, 2009

formWorks 0.1a - SharePoint Form Editing Tool

A lot of the work that I do revolves around tweaking SharePoint forms and I have been getting tired of typing the same code over and over so I've created formWorks to save me (and you!) the hassle of "popping the hood" on SharePoint forms.

Copy/pasting this code into the top of your Content Editor Web Part "Source Editor..." (or better yet, the linked file) will give the rest of your code access to the elements on the form (save for radio buttons & multiple checkboxes).

Here's the code:
<script type="text/javascript">
/*
#################
formWorks - v0.1a
2009-07-22
Ben Bradley - ben[at]bradleyit.com
#################
Adding formWorks code to a Content Editor Web Part (CEWP) will give your JavaScript
code access to the controls on the form in an easy and intuitive manner

1 - Open the form that you want to manipulate.
2 - Add "&ToolPaneView=2" to the end of the URL.
3 - Add a CEWP below the form.
4 - Add this code to the top of the "Source Editor" or the file that is linked.
5 - You can now reference the rows, cells and controls on the form in the following manner:
row['Title'].row.style.display = "none";
row['Title'].control.value = "New Title!!!";
if (row['Priority'].control.value == "1 - High") then {
alert("HIGH PRIORITY!");
}

Known ToDos -
Make compatible with Radio buttons & Check boxes
*/

var row = new Object();
var aTables = document.getElementsByTagName("TABLE");
for (var t in aTables) {
if (aTables[t].className == "ms-formtable") {
var aRows = aTables[t].getElementsByTagName("TR");
for (var r in aRows) {
if (aRows[r].getElementsByTagName && aRows[r].getElementsByTagName("TD")[0].className == "ms-formlabel") {
var rowTitle = trim(aRows[r].getElementsByTagName("TD")[0].innerText) || trim(aRows[r].getElementsByTagName("TD")[0].textContent);
row[rowTitle] = new newRow(aRows[r]);
}
}
}
}

function newRow(x) {
this.title = trim(x.getElementsByTagName("TD")[0].innerText) || trim(x.getElementsByTagName("TD")[0].textContent);
this.titleCell = x.getElementsByTagName("TD")[0];
this.control = x.getElementsByTagName("INPUT")[0] || x.getElementsByTagName("SELECT")[0] || x.getElementsByTagName("TEXTAREA")[0];
this.controlCell = x.getElementsByTagName("TD")[1];
this.row = x;
}

function trim(x) {
x = x.replace(/(^\t)(\t*)/,"").replace(/(\t*)(\t$)/,"") // tabs
.replace(/(^\n)(\n*)/,"").replace(/(\n*)(\n$)/,"") // new lines
.replace(/(^\r)(\r*)/,"").replace(/(\r*)(\r$)/,"") // return characters
.replace(/(^
)(
*)/,"").replace(/(
*)(
$)/,"") // html returns
.replace(/(^ )( *)/,"").replace(/( *)( $)/,"") // spaces
.replace(/(^ )( *)/,"").replace(/( *)( $)/,""); // html spaces
return x;
}

<:/script>

0 comments: