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>

Similar Posts

74 Comments

  1. We can use following code to Populate People Picker
    var accountName = $().SPServices.SPGetCurrentUser({
    fieldName: “Name”
    });
    $(“div[Title=’People Picker’]”).html(“”).append(accountName);

  2. Hi Marc,

    My requirement is to show certain people in people picker control based on the user’s region. Could you please guide me how to achieve this in SharePoint 2010 lists?

    Thanks,
    Hemant

  3. I’m hoping someone here can shine the light in the right direction for me. I am creating the ability to nominate a co-worker for an award and would like to be able to do the following:

    *Primary Need:
    Person enters the name of their co-worker via a name field (checks name with address book)
    – Based off that entry, other fields will be auto-populated based off information stored in Address Book and/or Active Directory (such as the nominee’s Title/Position).

    *Secondary Need:
    In another field, I would like to populate data about the submitter (such as phone number)

    I am very familiar with SharePoint from a WYSIWYG standpoint (Designer & Web)
    I do not have admin access to the SharePoint but do have full control over the site & parent site

    Any help would be greatly appreciated!

    Thanks in Advance

    – Kyle

    1. Kyle:

      Do you really want to store that information in the list or would you simply like to display it when people need to see it? The former is a risk if the information might change over the life of the process; the latter can be done with a DVWP.

      M.

      1. Marc,
        Thank you for your quick response!
        Ultimately the nominee’s information (title/location) only needs to be visible when we go in to retrieve the results (visible in a View or when exported to Spreadsheet). So to answer your question, no it does not need to be stored, just visible (same as when a field is calculated off the data in another column/field).

        The simple solution would be to just have a regular text field for the person completing the form to type this information into. However, when dealing with a department of 400+ people with 100+ titles (in which the “working titles” and “official titles” are different), this would be a much more accurate system if we were able to pull this data right from AD.

        Is this making any sense?

        – Kyle

        1. Kyle:

          In that case, I’d just collect the person’s name in the nominee Person or Group column and then use a DVWP to show the data how you’d like to see it on the reporting side of things.

          SPGetCurrentUser is written right now to only get the details for the current user; you could probably fairly easily extend it to get anyone’s details.

          M.

  4. On a side thought… is there a way to modify the “$().SPServices.SPGetCurrentUser” function you talk about to get the user information of the person that is entered/validated through the Person/Group field? Something like a “SetUser” function that would override the actual Current User?

  5. Hi, I have a slightly different question. I have to populate two fields in a list based on values from another list. I created an aspx page and it has webparts from both lists. From the “Users” list, I can display the current user’s company ID and company Name. The second webpart is a new item form for the “Meetings” list, and I need to pass the current user’s company ID and company Name to prepopulate two text boxes in this form.
    Is there a way to do this?

    1. Blanca:

      Sorry for the delayed reply. Yes, you can certainly do this. From the sounds of it, you’ll want to do it with script in the page. From what I understand, you would write some script that copies the values in the first Web Part when the user enters them and populate the right fields in the second Web Part.

      M.

  6. Hi Marc,

    Is there a way to get the other fields from user prfile like Mobile no, State, City for the given user using SPServices.
    I am trying with GetUserInfo operation, ut with noluck.

    Thanks in Advance for any suggestions or samples on getting this info by using User Profile web service.

    1. mswin:

      Yes, you should be able to get any available field from the user profile. Post your code over on the SPServices Discussions if you’d like help.

      M.

  7. Hi Marc,

    i am new to sharepoint and this is what i want in my custom form.

    i have a field Name – employeename (Person type) and i want that if user selects the name of employee from people picker and another field whose name is employee department gets populated automatically.pls tell me how to use the above code to fulfill my purpose

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.