Setting values of SharePoint Lookup field


/ Published in: JavaScript
Save to your folder(s)

this snippet allows you to dynamically set the value of a SharePoint lookup field based on a URL parameter


Copy this code and paste it in your HTML
  1. <script type="text/javascript">
  2.  
  3.  
  4.  
  5. // This javascript sets the default value of a lookup field identified
  6.  
  7. // by <<FIELD DISPLAY NAME>> to the value stored in the querysting variable
  8.  
  9. // identified by <<QUERYSTRING VARIABLE NAME>>
  10.  
  11.  
  12.  
  13.  
  14.  
  15. // Customize this javascript by replacing <<FIELD DISPLAY NAME>> and
  16.  
  17. // <<QUERYSTRING VARIABLE NAME>> with appropriate values.
  18.  
  19. // Then just paste it into NewForm.aspx inside PlaceHolderMain
  20.  
  21.  
  22.  
  23. _spBodyOnLoadFunctionNames.push("fillDefaultValues");
  24.  
  25.  
  26.  
  27. function fillDefaultValues() {
  28.  
  29. var qs = location.search.substring(1, location.search.length);
  30.  
  31. var args = qs.split("&");
  32.  
  33. var vals = new Object();
  34.  
  35. for (var i=0; i < args.length; i++) {
  36.  
  37. var nameVal = args[i].split("=");
  38.  
  39. var temp = unescape(nameVal[1]).split('+');
  40.  
  41. nameVal[1] = temp.join(' ');
  42.  
  43. vals[nameVal[0]] = nameVal[1];
  44.  
  45. }
  46.  
  47. setLookupFromFieldName("<<FIELD DISPLAY NAME>>", vals["<<QUERYSTRING VARIABLE NAME>>"]);
  48.  
  49. }
  50.  
  51.  
  52.  
  53. function setLookupFromFieldName(fieldName, value) {
  54.  
  55. if (value == undefined) return;
  56.  
  57. var theSelect = getTagFromIdentifierAndTitle("select","Lookup",fieldName);
  58.  
  59.  
  60.  
  61. // if theSelect is null, it means that the target list has more than
  62.  
  63. // 20 items, and the Lookup is being rendered with an input element
  64.  
  65.  
  66.  
  67. if (theSelect == null) {
  68.  
  69. var theInput = getTagFromIdentifierAndTitle("input","",fieldName);
  70.  
  71. ShowDropdown(theInput.id); //this function is provided by SharePoint
  72.  
  73. var opt=document.getElementById(theInput.opt);
  74.  
  75. setSelectedOption(opt, value);
  76.  
  77. OptLoseFocus(opt); //this function is provided by SharePoint
  78.  
  79. } else {
  80.  
  81. setSelectedOption(theSelect, value);
  82.  
  83. }
  84.  
  85. }
  86.  
  87.  
  88.  
  89. function setSelectedOption(select, value) {
  90.  
  91. var opts = select.options;
  92.  
  93. var l = opts.length;
  94.  
  95. if (select == null) return;
  96.  
  97. for (var i=0; i < l; i++) {
  98.  
  99. if (opts[i].value == value) {
  100.  
  101. select.selectedIndex = i;
  102.  
  103. return true;
  104.  
  105. }
  106.  
  107. }
  108.  
  109. return false;
  110.  
  111. }
  112.  
  113.  
  114.  
  115. function getTagFromIdentifierAndTitle(tagName, identifier, title) {
  116.  
  117. var len = identifier.length;
  118.  
  119. var tags = document.getElementsByTagName(tagName);
  120.  
  121. for (var i=0; i < tags.length; i++) {
  122.  
  123. var tempString = tags[i].id;
  124.  
  125. if (tags[i].title == title && (identifier == "" || tempString.indexOf(identifier) == tempString.length - len)) {
  126.  
  127. return tags[i];
  128.  
  129. }
  130.  
  131. }
  132.  
  133. return null;
  134.  
  135. }
  136.  
  137. </script>

URL: http://blogs.msdn.com/sharepointdesigner/archive/2007/06/13/using-javascript-to-manipulate-a-list-form-field.aspx

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.