SharePoint 2007: Set Default values for form fields when uploading documents

Situation: customer would like to have the ability to set default values for any metadata site columns (lookup or choice) automatically. If Department 1 is uploading documents and they have to select their department name from the metadata columns (content types –> site columns) every time they upload a document then its few more clicks for every document. Instead if you can default the department name during the check-in process, it would reduce number of clicks.

add the following code to Documents/Forms/EditForm.aspx (better create a copy of this and set the list form to be this new edit form). add the following jcvascript code to PlaceHolderMain web part zone. Notice in the code mode=[“Upload”] is used to make sure we are default values only during upload. If the user has already selected a value, it should not be overwritten when editing it.

 

You can extend this functionality where you can read the values from either sharepoint list or config file.

 

<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];

  } 

  var mode = vals["Mode"];

  if(mode == "Upload")

  {

           var source = vals["Source"];

          if(source.indexOf("CareerAndBenefits") != -1)

          {

                 setBusinessGroup("Business Group", "Corporate Services Group");

                 setDivision("Division", "Human Resources");

          }

          if(source.indexOf("CorporateCommunications") != -1)

          {

                 setBusinessGroup("Business Group", "Agribusiness Banking Group");

                 setDivision("Division", "Corporate Communications");

          }

  }

}

function setBusinessGroup(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);

    setBusinessGroupOption(opt, value);

    OptLoseFocus(opt); //this function is provided by SharePoint

  } else {

    setBusinessGroupOption(theSelect, value);

  }

}

function setBusinessGroupOption(select, value) {

  var opts = select.options;

  var l = opts.length;

  if (select == null) return;

  for (var i=0; i < l; i++) {

    if (opts[i].text == value) {

      select.selectedIndex = i;

      return true;

    }

  }

  return false;

}

function setDivision(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);

    setDivisionOption(opt, value);

    OptLoseFocus(opt); //this function is provided by SharePoint

  } else {

    setDivisionOption(theSelect, value);

  }

}

function setDivisionOption(select, value) {

  var opts = select.options;

  var l = opts.length;

  if (select == null) return;

  for (var i=0; i < l; i++) {

     if (opts[i].text == 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) {

      return tags[i];

    }

  }

  return null;

}

</script>

This entry was posted in SharePoint. Bookmark the permalink.

Leave a comment