Thursday, February 11, 2010

Using JavaScript to get a value using the SharePoint FieldInternalName

From the SharePoint JavaScripts blog:


<script type="text/javascript">  
/*************************************************************
Type:
Singelline tekst and multiline tekst (Plain text), Number, Currency = text
Multichoice checkbox = checkbox (returns commaseparated values)
Date (returns date only, but works with both DateOnly and DateAndTime) = date
RadioButton = radio
Dropdown = dropdown
Yes/No = bool
Hyperlink or picture (returns commaseparated: description,url) = url
Lookup - single (below and over 20 items), MultiLookup (returns array) = lookup
Lookup Multiple - you can use plain 'lookup', but if you want only "multi" = lookupMulti
Person - DisplayName = personDisplay
Person - LoginName = personLogin
Use: var value = returnValue('checkbox','FieldInternalNameOfYourField');
*************************************************************/
function returnValue(type, fieldInternalName) {
var arr = document.getElementsByTagName('!');//get all comments
for (var i=0;i < arr.length; i++ ) {
// Date
if (type=='date' && arr[i].innerHTML.match('FieldInternalName="' + fieldInternalName + '"')) {
var fieldTitle = arr[i].innerHTML.substring(arr[i].innerHTML.indexOf('FieldName=')+11,arr[i].innerHTML.indexOf('\n')-2);
var tags = document.getElementsByTagName('input');
for (var i=0; i < tags.length; i++) {
if (tags[i].title == fieldTitle && tags[i].parentElement.parentElement.parentElement.parentElement.parentElement.parentElement.innerHTML.indexOf('FieldName="'+fieldTitle+'"') > -1)
{
return tags[i].value;
}
}
// RadioButton - Credit to http://blog.markuso.com/ - slightly modified by me to use FieldInternalName
}else if(type=='radio' && arr[i].innerHTML.match('FieldInternalName="' + fieldInternalName + '"')) {
var tags = document.getElementsByTagName("input");
for (var i=0; i < tags.length; i++) {
var nameString = tags[i].name;
// get selected radio button value only
if (tags[i].type == "radio") {
var tagParentHTML = tags[i].parentElement.parentElement.parentElement.parentElement.parentElement.parentElement.parentElement.innerHTML;
if (tagParentHTML.indexOf('FieldInternalName="'+fieldInternalName+'"') > -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.value;
}
}
}
return null;
// Checkbox
}else if(type=='checkbox') {
var arr = [];
var arrValue = [];
var tags = document.getElementsByTagName("input");
for (var i=0; i < tags.length; i++) {
if (tags[i].type == "checkbox") {
var tagParentHTML = tags[i].parentElement.parentElement.parentElement.parentElement.parentElement.parentElement.parentElement.innerHTML;
if (tagParentHTML.indexOf('FieldInternalName="'+fieldInternalName+'"') > -1) {
arr.push(tags[i].id);
}
}
}
for (var x=0; x < arr.length; x++) {
var chkBox = document.getElementById(arr[x]);
if (chkBox.checked) {
arrValue.push(chkBox.parentElement.title);
}
}
return arrValue.toString();
// Text or Dropdown
}else if((type=='text' || type=='dropdown') && arr[i].innerHTML.match('FieldInternalName="' + fieldInternalName + '"')) {
return arr[i].parentNode.childNodes[1].childNodes[0].value;
// Hyperlink or Picture
}else if(type=='url' && arr[i].innerHTML.match('FieldInternalName="' + fieldInternalName + '"')) {
// returns commaseparated (description,url) so it can be split later
var hyperlink = [];
hyperlink.push(arr[i].parentNode.childNodes[1].childNodes[4].value);
hyperlink.push(arr[i].parentNode.childNodes[1].childNodes[1].value);
return hyperlink;
// Bool
}else if(type=='bool' && arr[i].innerHTML.match('FieldInternalName="' + fieldInternalName + '"')) {
return arr[i].parentNode.childNodes[1].childNodes[0].checked;
// Lookup
}else if(type=='lookup' && arr[i].innerHTML.match('FieldInternalName="' + fieldInternalName + '"')) {
// Under 20 items
var opt = arr[i].parentNode.childNodes[1].childNodes[0].options;
if(opt!=null) {
for (var x=0;x<opt.length;x++) {
if(opt[x].selected == true) {
return (opt[x].innerHTML);
}
}
}else if(opt==null){
if(arr[i].parentNode.childNodes[1].childNodes[0].childNodes[0]!= null) {
// Over 20 items
return arr[i].parentNode.childNodes[1].childNodes[0].childNodes[0].value;
}else{
// MultiLookup - optionally you can call the script with type='lookupMulti'
var arrSelected = [];
var fieldId = arr[i].parentNode.childNodes[1].childNodes[0].id;
var selectResultId = fieldId.substr(0,fieldId.indexOf('MultiLookupPicker')) + 'SelectResult';
var selectResultField = document.getElementById(selectResultId);
for(var x=0;x < selectResultField.length; x++){
arrSelected.push (selectResultField[x].innerHTML);
}
return arrSelected.toString();
}
}
}
// Lookup Multi (can also use plain 'lookup' as spacified above
else if(type=='lookupMulti' && arr[i].innerHTML.match('FieldInternalName="' + fieldInternalName + '"')) {
var arrSelected = [];
var fieldId = arr[i].parentNode.childNodes[1].childNodes[0].id;
var selectResultId = fieldId.substr(0,fieldId.indexOf('MultiLookupPicker')) + 'SelectResult';
var selectResultField = document.getElementById(selectResultId);
for(var x=0;x < selectResultField.length; x++){
arrSelected.push (selectResultField[x].innerHTML);
}
return arrSelected.toString();
// Person
}else if(type=='personDisplay' && arr[i].innerHTML.match('FieldInternalName="' + fieldInternalName + '"')) {
var rawString = arr[i].parentNode.childNodes[1].innerHTML;
if(rawString.indexOf('isresolved="True"')>0){
// user resolved OK
// Use these for DisplayName
var displayName = rawString.indexOf('displaytext="');
var start = displayName + 13;
var stopp = displayName + rawString.substring(rawString.indexOf('displaytext="')).indexOf('key="')-2;
var user = rawString.substring(start, stopp);
return user;
}else{
// user not resolved
return "";
}
}else if(type=='personLogin' && arr[i].innerHTML.match('FieldInternalName="' + fieldInternalName + '"')) {
var rawString = arr[i].parentNode.childNodes[1].innerHTML;
if(rawString.indexOf('isresolved="True"')>0){
// Use these for Loginname
var loginName = rawString.indexOf('isresolved="True"');
var start = loginName + rawString.substring(rawString.indexOf('isresolved="True"')).indexOf('key="')+5;
var stopp = loginName + rawString.substring(rawString.indexOf('isresolved="True"')).indexOf('">');
var user = rawString.substring(start, stopp);
return user;
}else{
// user not resolved
return "";
}
}

}
}
</script>

No comments: