in

SharePoint Blogs

The Best Place for SharePoint-related Blogs

The Dean's Office

SuperGeek Tip: Calculate "Full Name" on the client-side AND use it in lookups!

I mention in just about every Bootcamp that the biggest problem with calculated fields is that you can't use them in lookups.  Well, the solution, as I mention in class, is to perform your calculations on the client-side.  What I don't mention in class (it's a “SuperGeek Moment”, so it's meant to get the gears turning) is the actual code.  Thus I have decided that I'm going to try and write a weekly “SuperGeek Tip”.  This week's tip: Calculating “Full Name” using client-side script, so SharePoint is blissfully unaware that Full Name is anything but a plain text field.

At the bottom of the NewForm.aspx and EditForm.aspx for any default Contacts list, just above the closing </body> element, add this code:

 <script>
  var lastName = document.getElementById("urn:schemas-microsoft-com:office:office#Title")
  var firstName = document.getElementById("urn:schemas-microsoft-com:office:office#FirstName");
  var fullName = document.getElementById("urn:schemas-microsoft-com:office:office#FullName");
  lastName.onchange = fixFullName;
  firstName.onchange = fixFullName;
  
  function fixFullName() {
   fullName.value = firstName.value + " " + lastName.value;
  }
  
 </script>
 

You can do this in FrontPage 2003, or you could modify the list definition files yourself (the so-called “Ghosted” template -- be aware, Microsoft doesn't support you touching those files if you've already deployed sites based on that site definition).

Once you've done this, adding contacts / editing contacts will both cause Full Name to calculate whenever the values of First Name or Last Name change.  Pretty handy, no?

Comments

 

Alan Weiner said:

Hi. This doesn't seem to work.  I'm using Sharepoint 2.0 and FrontPage 2003.  I've tried it with Contact List fields named both "FullName" and "Full Name"

July 11, 2007 7:54 AM
 

presack said:

Dustin-

Great post.  I used it for ripping the file name off of the end of the "Attach File" generated filepath string, and then wrote it to a field on my list so that I could automatically generate the link to the attachment elsewhere, in a dataview web part.  (I guess a calculated column would work there also.)

I do have a question, though.  Is there a way to get this to work for a Lookup field?  I used your method, and it returns the item's number rather than the data.  How can I convert that to the text that is in the other list, and write it to a plain old text field in my current list?

Thanks,

Presack

August 16, 2007 10:54 PM
 

Dustin Miller said:

@Alan: Not sure why it wouldn't work, works fine here. :)

@presack: You'll want to grab the "text" property of the currently selected "option" object for the lookup's "select" object.

August 17, 2007 10:47 AM
 

presack said:

Dustin-

Thanks for the speedy response.  For other readers of the thread (I know Dustin doesn't need it!), here is another useful read:

blogs.msdn.com/.../using-javascript-to-manipulate-a-list-form-field.aspx

presack

August 22, 2007 6:37 AM
 

Andrew Swartzell said:

Just thought I should mention that this is no longer necessary with WSS 3.0/MOSS 2007.  I'm using lookups on calculated fields all over the place.

January 30, 2008 6:37 PM

Leave a Comment

(required )  
(optional )
(required )  
Add

Need SharePoint Training? Attend a SharePoint Bootcamp!

Posts (c) their respective authors. Everything else (c) 2007 SharePoint Experts