Populating a SharePoint List Form with the Current User Information

Laura Rogers (aka @WonderLaura) posted a neato way to populate a list form with the current user’s information today in her post SharePoint List Form – Default User Information.

As I always say to Laura, it’s amazing how many different ways there are to do something in SharePoint! I would probably do it differently, but that’s not to say that Laura’s approach isn’t a perfectly good one.

In this case, I would use SPServices and the $().SPServices.SPGetCurrentUser function to populate the values with script. The upside of this approach is that you wouldn’t need to customize the form if you didn’t need to for some other reason.  You could just add the script to the page, either in the page itself (my recommendation) or in a Content Editor Web Part (CEWP).

One other note on Laura’s approach. If you are populating the list columns with the current user’s Department or Phone, it *might* be a bad idea.  If you are really duplicating those values, you run the risk of them changing in the User Information List (via an update from Active Directory, most likely) and having the values in your list be wrong.  If, on the other hand, you are using them for some point in time thing (like “What’s your current Department?” in a survey), then you’re fine.

Here’s the jQuery script which would do it:

<script language="javascript" type="text/javascript" src="../../jQuery%20Libraries/jquery-1.4.2.min.js"></script>
<script language="javascript" type="text/javascript" src="../../jQuery%20Libraries/jquery.SPServices-0.5.4.min.js"></script>
<script language="javascript" type="text/javascript">
$(document).ready(function() {
	var userDepartment = $().SPServices.SPGetCurrentUser({
		fieldName: "Department"
	});
	$("input[Title='Department']").val(userDepartment);
	var userPhone = $().SPServices.SPGetCurrentUser({
		fieldName: "WorkPhone"
	});
	$("input[Title='Phone']").val(userPhone);
});
</script>

74 Comments

  1. Both yours and LR’s are great methods, however, your technique also has the huge advantage that it can be done in WSS – I believe LR’s method is MOSS-only since it depends on the Current User Filter web part. Thanks!

    Reply
  2. I have a similiar but slightly different function I’d like to get to work. Instead of pulling in the current users profile information when the page loads, I need to be able to retrieve user profile information from a person/id entered and verified in the people picker field and then populate fields on the form with that information (department, office, manager). Thank you for any information you can provide.

    Reply
    • Rick:

      To do that, you’ll want to bind to the change event in the People Picker, and then use my SPServices library to read from the User Information List to get the fields for the given user.

      M.

      Reply
  3. I work with Rick above, and I’m trying to figure out where to put this code. We have a list called User Info Test, and inside it we have an aspx page called NewForm.aspx. The code as you have it works in in this aspx page, however we are trying to modify it so it will use the onLoad for the PeoplePicker field called Name. The code goes through the page and searches all the elements, looking for Name. Unfortunately it only comes back with one – Search Scope.

    I’m new to Sharepoint, so I’m not sure what is going on here. There are 8 different fields on the page.
    Not sure if you can help us with this, but here is the code we are using:

    _spBodyOnLoadFunctionNames.push(“PageOnLoad”);

    function PageOnLoad() {
    //find your lookupElement from the page.
    var lookupElement = GetElementByTypeAndTitle(‘SELECT’,’Name’);
    if ( lookupElement != null)
    {
    lookupElement.onchange = function()
    {
    OnChanged(lookupElement.options[lookupElement.selectedIndex].text)
    };
    } }
    function OnChanged(){
    $(document).ready(function() {
    var userDepartment = $().SPServices.SPGetCurrentUser({
    fieldName: “Department”
    });
    $(“input[Title=’Department’]”).val(userDepartment);
    var userPhone = $().SPServices.SPGetCurrentUser({
    fieldName: “WorkPhone”
    });
    $(“input[Title=’Phone’]”).val(userPhone);
    });
    alert(‘hello’);
    }

    function GetElementByTypeAndTitle(elementType, elementTitle) {
    //get the Element by tag name.
    var allElements = document.getElementsByTagName(elementType);
    for (var i = 0; i < allElements.length; i++) {
    alert(allElements [i].title);
    if (allElements [i].title == elementTitle)
    return allElements [i];
    }
    return null;
    }

    Reply
    • Michael:

      Sorry for the delayed reply, but your comment was banished to the spam bucket. You’re doing a little mixing and matching above of JavaScript and jQuery, but probably too much JavaScript. You’ll want to use the Developer Tools (in IE8) or the like to understand how the People Picker is represented in the DOM so that you can bind to the right element’s event. It’s a span that looks something like:

      <span tabIndex="-1" id="content" contentEditable="true" onmousedown="onMouseDownRw();" oncontextmenu="onContextMenuSpnRw();">

      You also don’t want to use _spBodyOnLoadFunctionNames.push when you’re working with jQuery; the equivalent with jQuery is $(document).ready. As it is, I’m not sure what’s going to happen with the code above.

      M.

      Reply
  4. Great!!! I’m able to populate the current user’s Dpartment with your script, yet how about obtaining the current user’s Manager? The following link indicates that doesn’t seem to be part of SPGetCurrentUser http://spservices.codeplex.com/wikipage?title=$().SPServices.SPGetCurrentUser Is there a way to pull the preferred name of my manger with Get_CurrentUserInfo I would like to have a SP list NewForm auto-populate the current User, their Department & Manager.

    Reply
    • The fields I have listed in the documentation are the default out-of-the-box fields. If you’ve got a Manager field and it’s exposed on the userdisp.aspx page, then you should be able to retrieve it. The SPGetCurrentUser function does an AJAX call to grab _layouts/userdisp.aspx and “scrapes” the values from the page based on the internal field name (aka StaticName).

      I just added this additional information to the documentation.

      M.

      Reply
  5. Hi Marc,

    Thanks for the post I’m still a bit new to Sharepoint and I was wondering if I could populate a field when a checkbox is checked. For example: I have a Grammar Approved box and a Grammar Approved By field. I would like the Grammar Approved By field to be populate with the name of whoever checks the box. I think the code you provided has the general structure to do this, I just need to know how to adapt it. I’m already using jQuery for some other stuff on my site.

    Thanks for your help.

    Reply
    • Tim:

      Absolutely. What you’d need to do is bind to the change event on the checkbox and then populate the Grammar Approved field when the box is checked. You can use the SPGetCurrentUser function in SPServices to find out the name of the current user to populate the People Picker.

      M.

      Reply
  6. Hi Marc,

    I have a similar requirement but i want to populate with the User name and e- mail .How can i do that can you please help me .

    Thanks in adv

    Reply
      • Hi ,

        Thanks for your response. Actually i need to populate the User name and email when user create new item or edit the item so that user no need to enter his information again and again. I have check with Lauras article but it not fully works for my requirement as because of i have more calculated columns in my custom list .

        Can you please help me how can i get the user name and email populating

        Thanks in advance,
        SS

        Reply
  7. Hi Mark. Thank you for providing this article. I am new to SharePoint and I need help knowing where to place the code that was supplied. The goal is to filter a list by a value specified in the User Information List for the current user. The AllItems.aspx should display the filtered list. Thank you.
    – Richard

    Reply
    • Richard:

      What value for the user do you want to filter on? It may be that you don’t need code at all, but more likely you’ll want to use a DVWP rather than script for this.

      M.

      Reply
  8. There are two custom columns in the User Information List. A Division lookup, and a Section lookup. If a user is a member of the Division Manager group, I need to find out which Division they manage, and filter task orders by the DivisionID field. Division managers should have access to all task orders assigned to their Division. If they are in the Section Managers group, I would like to retrieve the Section they supervise, and filter task orders by SectionID. Section Managers should see all task orders assigned to their section. Please let me know the simplest way to do this. Thanks.

    Reply
    • Richard:

      Keep in mind that what you display in a view does not guarantee that those subsets of users will be prohibited from accessing the items they don’t see. In other words, you’re trying to layer in some obscurity, but not security.

      Your situation is pretty unique, and it will require some custom coding. I would still say that a Data View Web Part (DVWP) with an AggregateDataSource is probably the way to go, though you may need to layer in some script to tie it all together. While what you describe is not tremendously complex, it’ll take some work.

      Of course, your other alternative is managed code on the server side, but given the way you’ve asked your questions it doesn’t sound like that’s a realistic path for you.

      M.

      Reply

Have a thought or opinion?