2009/06/23

Using Javascript to access a List Form Field in SharePoint Designer


An article was posted showing how to access and manipulate a List Form Field within SharePoint Designer using Javascript over at Microsoft SharePoint Designer Team Blog. The code in the article supported the following types of SharePoint 2007 form fields, but not the Radio Buttons Choice field:



Single Line of Text
Multiple Lines of Text
Number
Currency
Choice (dropdown)
Lookup (single)
Lookup (multiple)
Yes/No
So I’ve modified the function getTagFromIdentifierAndTitle and added onto it the code to allow accessing Radio Buttons Choice fields. The following is the modified function:
/*******
SharePoint Field Type identifier tagName Option
--------------------- -------------- --------- ---------
Single Line of Text TextField input
Multiple Lines of Text TextArea input
Number TextField input
Currency TextField input
Choice (radio buttons) RadioButtons input
Choice (radio buttons) RadioButtons input value
Choice (dropdown) DropDownChoice select
Lookup (single)* Lookup select
Lookup (multiple) SelectCandidate;
SelectResult select
Yes/No BooleanField input
******/
function getTagFromIdentifierAndTitle(tagName, identifier, title, option) {
var len = identifier.length;
var tags = document.getElementsByTagName(tagName);
for (var i=0; i < tags.length; i++) {
var idString = tags[i].id;
var nameString = tags[i].name;
// get selected radio button value only
if (option == "value" && tags[i].type == "radio" && (identifier == "RadioButtons" && nameString.indexOf(identifier) == nameString.length - len)) {
var tagParentHTML = tags[i].parentElement.parentElement.parentElement.parentElement.parentElement.parentElement.parentElement.innerHTML;
if (tagParentHTML.indexOf('FieldName="'+title+'"') > -1) {
var radioButtons = document.getElementsByName(nameString);
var radioValue = "";
for (var x=0; x < radioButtons.length; x++) {
if (radioButtons[x].checked) {
radioValue = radioButtons[x].parentElement.title;
break;
}
}
var o = document.createElement("INPUT");
o.type = "hidden";
o.value = radioValue;
return o;
}
}
// get radio buttons group
if (tags[i].type == "radio" && (identifier == "RadioButtons" && nameString.indexOf(identifier) == nameString.length - len)) {
var tagParentHTML = tags[i].parentElement.parentElement.parentElement.parentElement.parentElement.parentElement.parentElement.innerHTML;
if (tagParentHTML.indexOf('FieldName="'+title+'"') > -1) {
return document.getElementsByName(nameString);
}
}
// all other input or select type
else if (tags[i].title == title && (identifier == "" idString.indexOf(identifier) == idString.length - len)) {
return tags[i];
}
}
return null;
}



Here is how you would use the function above for Radio Buttons:
// to get the selected radio button and its value
var myRadioButtonsSelected = getTagFromIdentifierAndTitle("input","RadioButtons","MyFieldName","value");
var myRadioButtonsValue = myRadioButtonsSelected.value;

// to get the array for radio buttons
var myRadioButtonsArray = getTagFromIdentifierAndTitle("input","RadioButtons","MyFieldName");
for (var x=0; x < myRadioButtonsArray.length; x++) {
// do whatever you like here...
// for example:
myRadioButtonsArray[x].parentElement.onclick = function(){alert(myRadioButtonsArray[x].value);};
}


The original post can be found here: Using Javascript to Manipulate a List Form Field
For a basic function, you could also check out Boris Gomiunik’s Blog for another post about referencing SharePoint form fields using Javascript.

compare dates in sharepoint using JavaScript

you can use the current code to check the differance between dates before saving data using JavaScript code.

The following code I used to use it in case of check dates I hope it will be useful.

function PreSaveAction()

{

var date1 = getTagFromIdentifierAndTitle("INPUT","DateTimeFieldDate","Letter Date");

var date2 = getTagFromIdentifierAndTitle("INPUT","DateTimeFieldDate","Decision Date");

var date3 = getTagFromIdentifierAndTitle("INPUT","DateTimeFieldDate","Request Date");

var arrDate1 = date1.value.split("/");

var useDate1 = new Date(arrDate1[2], arrDate1[1]-1, arrDate1[0]);

var arrDate2 = date2.value.split("/");

var useDate2 = new Date(arrDate2[2], arrDate2[1]-1, arrDate2[0]);

var arrDate3 = date3.value.split("/");

var useDate3 = new Date(arrDate3[2], arrDate3[1]-1, arrDate3[0]);

if(useDate1 >= useDate2)

{

alert("Letter date must be before Decision Date");

return false; // Cancel the item save process

}

else if(useDate2 >= useDate3)

{

alert("Decision Date must be before Request Date");

return false; // Cancel the item save process

}

else

{

return true; // OK to proceed with the save item

}

}

//getTagFromIdentifierAndTitle function

function getTagFromIdentifierAndTitle(tagName, identifier, title)

{

var len = identifier.length;

var tags = document.getElementsByTagName(tagName);


for (var i=0; i < tags.length; i++ )
{

var tempString = tags[i].id;

if (tags[i].title == title && (identifier == "" || tempString.indexOf(identifier) == tempString.length - len))

{

return tags[i];

}

}

return null;

}


2009/06/22

To disable right click on webpage

you can disable the right click on webpage using the following javascript code:

<script language="JavaScript1.1">

<!-- Begin function right(e)

{

if (navigator.appName == 'Netscape' && (e.which == 3 e.which == 2))

return false;

else if (navigator.appName == 'Microsoft Internet Explorer' && (event.button == 2 event.button == 3))

{

alert(" ");

return false;

}

return true;

}

document.onmousedown=right;

document.onmouseup=right;

if (document.layers) window.captureEvents(Event.MOUSEDOWN);

if (document.layers) window.captureEvents(Event.MOUSEUP);

window.onmousedown=right;

window.onmouseup=right;


// End -->
</script>

2009/06/03

WPSC undefined JavaScript error.


While using Forms Authontication I met the following JS error "WPSC" object isnot defiend, and I added the following code then this error disappeared.


<script type="text/javascript">


if(typeof(WPSC) == "undefined"){


WPSC = new Object();


WPSC.Init = function(){


//do nothing
}


WPSC.WebPartPage = new Object();


WPSC.WebPartPage.Parts = new Object();


WPSC.WebPartPage.Parts.Register = function()


{
//do nothing
}


}


</script>