Found a way to “Address” a SharePoint field using JavaScript

One of the things that makes it a bit tough working with SharePoint pages is the fact that you cannot predict what the form fields will be called. They get random names like ctl00$m$g_b38c402f_5254_4ae1_8677_39beff22ef48$ctl00$ctl02$ctl00$ctl01$ctl00$ctl00$ctl02$ctl00$ctl00$ctl04$ctl00$ctl00$TextField and they change every time you change the form fields. Bah!

Hopeless.

However… in my hunt around for a solution to the Outlook Calendar times in GMT issue for one client, I came across this little gem of a script that solves this problem: http://blogs.msdn.com/sharepointdesigner/archive/2007/06/13/using-javascript-to-manipulate-a-list-form-field.aspx

The getTagFromIdentifierAndTitle code below grabs a Form field tag (id) based on the tag Name, Identifier and Title you pass in. In this code sample, they are using that information to pre-populate form fields in SharePoint from values in the querystring. One of the projects we’re working on at the moment has a common requirement to have the form fields pre-populated – this may be useful in that solution somewhere.


<script type="text/javascript">   
// This javascript sets the default value of a lookup field identified
// by <<FIELD DISPLAY NAME>> to the value stored in the querysting variable
// identified by <<QUERYSTRING VARIABLE NAME>>     
// Customize this javascript by replacing <<FIELD DISPLAY NAME>> and
// <<QUERYSTRING VARIABLE NAME>> with appropriate values.
// Then just paste it into NewForm.aspx inside PlaceHolderMain   
_spBodyOnLoadFunctionNames.push("fillDefaultValues");   
function fillDefaultValues() {
   var qs = location.search.substring(1, location.search.length);
   var args = qs.split("&");
   var vals = new Object();
   for (var i=0; i < args.length; i++) {
     var nameVal = args[i].split("=");
     var temp = unescape(nameVal[1]).split('+');
     nameVal[1] = temp.join(' ');
     vals[nameVal[0]] = nameVal[1];
   }
    setLookupFromFieldName("<<FIELD DISPLAY NAME>>", vals["<<QUERYSTRING VARIABLE NAME>>"]); }

function setLookupFromFieldName(fieldName, value) {
   if (value == undefined) return;
   var theSelect = getTagFromIdentifierAndTitle("select","Lookup",fieldName);
  // if theSelect is null, it means that the target list has more than
  // 20 items, and the Lookup is being rendered with an input element
    if (theSelect == null) {
       var theInput = getTagFromIdentifierAndTitle("input","",fieldName);
       ShowDropdown(theInput.id);
      //this function is provided by SharePoint
       var opt=document.getElementById(theInput.opt);
       setSelectedOption(opt, value);
       OptLoseFocus(opt);
      //this function is provided by SharePoint  
    } else {
     setSelectedOption(theSelect, value);
   }
}  

function setSelectedOption(select, value) {
   var opts = select.options;   var l = opts.length;
   if (select == null) return;   for (var i=0; i < l; i++) {
     if (opts[i].value == value) {
       select.selectedIndex = i;
       return true;
     }
   }
   return false;
}

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;
}
</script>


Here’s a list of the most common SharePoint Field types and their associated Identifier and tagName:

SharePoint Field Type

identifier

tagName

Single Line of Text

TextField

input

Multiple Lines of Text

TextField

input

Number

TextField

input

Currency

TextField

input

Choice (dropdown)

DropDownChoice

select

Lookup (single)*

Lookup

select

Lookup (multiple)

SelectCandidate; SelectResult

select

Yes/No

BooleanField

input

*Lookups are a bit more complicated because Lookup FormFields render differently when the target list contains more than 20 items. That’s why the code above has a special function for Lookups

Advertisements

About Brad Saide

I'm a SharePoint consultant. I'm also slowly going bald, seem to have a permanent spare tyre around my waist and enjoy socialising with friends over a beer or 10. The last 2 may possibly be related. Started working with SharePoint when the first version was in limited beta release (participated in the Technology Adoption Program while at Woolworths) and have been committed to the adoption of the technology as a business enabler ever since.
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s