SharePoint Customization: Lookup Columns and Calculated Values

Great tip from Dustin Miller

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 element, add this code (inside a script block of course)

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

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.

— Nice Tip Dustin!

You may also like...

7 Responses

  1. jake says:

    hi dustin,
    do you know how to redirect the Title hyperlink in list to the other field of the list?

    eg. i have Title(with context menu), Project Name, Project Id, Status Report Title

    Title is a calculated field with format Status Report – PM/AM
    Status Report Title is also a calculated field with format Project Name – Project ID

    now, on the all items view of the list. The Title column has all this hyperlinks named with the format given above.
    what i want to achieve is upon clicking this link, i would be brought to the dispform.aspx of that item but the Title would then be replaced with the Status Report Title value, so that the header would also change

    do you know how to do this? thanks!

  2. Silent Mule says:

    Great but I couldn’t get it to work, though I really want to. So, can you maybe clarify what type of script (I presume Javascript) and what you mean by the ‘closing element’ (I assumed it was either or or or something similar but nothing I try works. That would be most helpful. Ta

  3. presack says:

    Will this work on a lookup column? That is, if I have a list with a column that is a lookup of information from another list, can I use this method to make a calculated column based on the lookup value the user supplied?

    Thanks,
    presack

    P.S. Sorry for the duplicate post, but I accidentally typed “column” where I meant to type “list”? Maybe the first can be removed in the moderation process?

  4. presack says:

    Will this work on a lookup column? That is, if I have a list with a column that is a lookup of information from another column, can I use this method to make a calculated column based on the lookup value the user supplied?

    Thanks,
    presack

  5. Kera says:

    I think this will help with a huge problem I’ve been having, generating unique client ID’s for this company’s portal site. Any other neat tricks in that vein would be much appareciated.

  6. Jennifer Endicott says:

    Is there anyway to do this within Sharepoint? The site I have for my company does not allow edits within frontpage, which makes these things very difficult.

    Thanks!

  7. Thanks, Shane, for the plug. :)

I would love to hear from you.

%d bloggers like this: