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. I’ve been trying to use JQuery to simply display a user’s email address within a Sharepoint page (actually within the Master Page). I have enabled the settings in web.config to allow code in the Master Page. I’m just not sure how to make JQuery pull and display this information?

    1. Matt:

      You don’t need to make any changes to web.config to use jQuery. You just need to store the .js file somewhere and add a reference to it in your master page.

      M.

  2. Hi Mark

    I writing JQueary in my SPD , i have taken Manager,Phone and EMP Id as a text boxes in my list
    with above code i am not able to retrive those fileds

    Can you please tell me how to do that

  3. Hi Marc

    Thanx for sharing this wonderful article.

    I want to use “User Information List” as a lookup source in another list.

    I had done this in SharePoint 2003 but i can’t find “User Information List” in SharePoint 2007 when i am trying to make new column with lookup.

    Can you put some light on this issue?

    Thanx in advance
    Harmeet

  4. I am trying to display the current user name in a People Picker & their corresponding Title in a text box.

    Here is the script I’m using, but nothing appears – no even an annoying error message :(

    $(document).ready(function() {
    var accountName = $().SPService.SPGetCurrentUser({
    fieldName: “Name”});

    $(“div[Title=’People Picker’]”).html(“”).append(accountName);

    var userTitle = $().SPServices.SPGetCurrentUser({
    fieldName: “JobTitle”
    });
    $(“input[Title=’RcvdTitle’]”).val(userTitle);
    });

    Any suggestions on how to solve? Thanks!

  5. Hi Marc – Great stuff. My question is related to custom properties. I’ve been able to get the out-of-the-box fields to display with no issues. However, when I try to return any of the custom properties nothing is returned. However, when I view the userdisp.aspx page I can see all the values listed in those custom properties. Any ideas? Do the custom properties need some special settings configured on them?

    Thanks!
    Scott

    1. Scott:

      GetCurrentUser looks for the comment above each field to grab the right one using a regex.
      var thisTextValue = RegExp("FieldInternalName=\"" + opt.fieldName + "\"", "gi");
      You’ll need to mimic that for the function to find your custom fields. I’d be surprised if they weren’t rendered that way if SharePoint is doing it, though.

      M.

      1. Marc – Thank you for the response. I think I might not have explained my issue well enough. I have a custom profile property called “OtherPager”. Here is the code I’m using.

        $(document).ready(function() {
        var userWorkPhone = $().SPServices.SPGetCurrentUser({
        fieldName: “WorkPhone”,
        debug: true
        });
        $(“input[Title=’WorkPhone’]”).val(userWorkPhone);

        var userOtherPager = $().SPServices.SPGetCurrentUser({
        fieldName: “OtherPager”,
        debug: true
        });
        $(“input[Title=’OtherPager’]”).val(userOtherPager);

        var userFullName = $().SPServices.SPGetCurrentUser({
        fieldName: “Title”,
        debug: true
        });
        $(“input[Title=’Title’]”).val(userFullName);

        });

        I’m able to get the “WorkPhone” and “Title” of the user but I am unable to return the “OtherPager” value. I have verified the name of the property and that it does have a value in it from this page – /_layouts/EditProfile.aspx.

        Am I missing something when making the calls?

        Thank you in advance!
        Scott

  6. If I add the code you provided in the NewForm.aspx I get an error message: “Only Content controls are allowed directly in a content page that contains Content controls.” I new to Sharepoint and was wondering where to add the code.

    1. I’m not familiar with that error, so I’m not sure what’s happening. That post is from pre- SharePoint 2010. I would recommend putting the script into a text file and using the Content Link in the CEWP to reference it. Also, the versions of the libraries shown are quite old; use the up-to-date ones.

      M.

  7. I like your code, but I don’t know where to put those code in to the newform.aspx. Also, how do I know my site is supporting the jquery. Sorry, I am new to the Sharepoint

    1. Eric:

      Ideally you’d store the code in a file and reference it with the Content Link in a CEWP. You can also reference jQuery and SPServices from CDNs if you’d prefer.

      M.

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.