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. Hi Marc,

    Thank you for your contributions to the SharePoint community. I recently discovered your blog and subscribed for your great solutions and original approaches to achieving things in SP. You are a star man!

    I posted a question to you yesterday in another topic (Looking up MM columns) and found out that it’s not really possible. As a workaround I was thinking of creating a single text column next to MM column and copy the value of MM column there. Then, I would be able to reference that column in my lookup in another list. Apparently it requires developing a workflow (in SP Designer- have no access-don’t ask why…), stripping the MM value from its GUID etc. It is not as easy as I have thought.

    So, what I am looking into now (and this is where my reply becomes relevant to your post), was: In a NewForm.aspx to Auto populate a single line of text column called “Department” with the value chosen from a term set in Managed Metadata column “Dept”.
    in other words, the script would pick up the MM term selected in one column, and insert it automatically as text into another column.

    Is it possible with the jQuery as I am liking this functionality and prefer to use. Or maybe there is another, more robust approach?
    At the moment I am banging my head against the wall!!!!

  2. Thanks Marc for your instant reply.

    I don’t know if I understand this but it looks like the above function accesses a term set in MMS and just displays terms in a filed regardless of what user has chosen from that term set for a particular item already in a list. Ufff!

    What I am actually after is some kind of event handler (I don’t know, I’m not a programmer) to just take the value the user chose in one filed (happens to be a MM term) and insert it automatically in a field below in a form as text, so I can look up that column elsewhere in the site.
    In the list there will be an “Employee” column (text), “Dept” (MM column) and “Department” (text). “Department” column would be a look-up column in another list and I want it to match the value of “Dept” for a particular “Employee”.

    It’s extremely frustrating that such a simple thing is still not available OOTB in SP2013. Argh!
    :)

    1. Lukas:

      I think you may be overengineering whatever it is that you are trying to accomplish. How do you want to use the Department value in the item after the item is created?

      M.

      1. Ok, here is my scenario:

        ‘Staff List’ with columns: Employee (single text), Dept (MM), Department (single text/hidden in view).
        ‘Payroll List’ with columns: Employee (lookup:StaffList), Department (lookup:StaffList), etc….

        In ‘Staff List’ on New or Edit I want user to create/edit a new employee, choose their Dept (from term store), then Department column would be auto populated in a form so user will not have to pick the term again (to minimise inconsistencies and overcome metadata -> lookup limitation).

  3. Marc,

    Great Example. I have following scenario: In SharePoint 2010 I have a web part page. In this page, there is a Document library and a List added. Both these library and list have a column called – Store. I have added a Text filter on this page and Current User filter on this web part page. Current User filter will pass a property value called – office to the Text filter via web part connection. The only problem is that the Store column has just 3 characters which is store acronym. The value which is returned by Current User Filter property has some values appended to the actual store value. How do I apply formatting to the Text filter to remove these extra characters or even better how do I use your example to pre-populate this Text Filter with the store value. I believe I can parse out the extra characters in JQuery or maybe not?

    Hope this explains my scenario and hope to get a solution or a hint

  4. Marc,
    I know all of this is over 6 years old, but i am curious if this would be usable with custom coded form in a Content Editor. Any Ideas? I haven’t tried it yet, but wanted your input if you are able to give it.

      1. You are absolutely right. Including the picture.
        Example of the code in case someone else would like to do similar.

        $(document).ready(function() {
        var userdetails= $().SPServices.SPGetCurrentUser(
        {
        fieldNames:[“ID”,”EMail”,”UserName”,”FirstName”,”LastName”,”Picture”],
        debug:false
        }
        );
        alert(userdetails.Picture);
        document.getElementById(‘Name’).value = (userdetails.FirstName + ” ” + userdetails.LastName);
        document.getElementById(‘Picture’).value = (userdetails.Picture);

        });

        Name

        Photo

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.