Using [Today] or [Me] in SharePoint calculated columns

If you try this in the browser you will get a message alon the line of Calculated columns cannot contain volatile functions like Today and Me'. At this point you go D'Oh!

But.... you can !!!

Before creating your calculated column you will need to create a column called Today or Me (depending on the calculation you want to create). Once this has been created, SharePoint lets you use the [Today] or [Me] functions in the calculation.

This example shows how to create an age calculated column.

[code:c#]

1. In your SharePoint list, create a column, title = DOB, type = Date, format = date only.

2. Create a column, title = Today, type = text.

3. Create a column, title = Age, type = calculated, calculation = DATEDIF([DOB],[Today],"Y")

4. Delete the column titled 'Today'

Now add a new item to the list. Set the DOB date (to somewhere in the past!) and save. The Age should have now been correctly calculated in Years.

[/code]

Note that once you have deleted the Today or Me column, if you try to edit the calculation in the future, SharePoint will complain again. However, you can simply create another column (Today or Me), edit your calculation, then Delete teh today / Me column again.

Currently rated 4.5 by 2 people

  • Currently 4.5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Pre-Populating SharePoint fileds with values

There are occasions when we need to pre-populate SharePoint fields with data, such as from a query string. the solution is not that difficult, using a bit of Javascript. Paul blogged the solution of at Autosponge.

(Code below copied from Autosponge and full credit to Paul)

[code:html]

<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>>"]);
  setTextFromFieldName("<<FIELD DISPLAY NAME>>", vals["<<QUERYSTRING VARIABLE NAME>>"]);
  setPeoplePicker("People Picker", vals["<<QUERYSTRING VARIABLE NAME>>"]);
  //do not change the fieldname for setPeoplePicker
}
function setTextFromFieldName(fieldName, value) {
 if (value == undefined) return;
   var theInput = getTagFromIdentifierAndTitle("input","",fieldName);
theInput.value=value;
}
function setPeoplePicker(fieldName, value) {
  if (value == undefined) return;
    var assignedToInput = getTagFromIdentifierAndTitle("div", "",fieldName);
assignedToInput.innerHTML = value;
}
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>

[/code]

 

To get the code onto the page, we simply add a Content Editor Web Part onto Newform.aspx , or whichever page we have the fileds on

Currently rated 4.5 by 2 people

  • Currently 4.5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

 

Dilbert of the day