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>
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?
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.
Cool, good to know. I was able to get it to work!
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
Pavan:
It’s really impossible to answer your question without seeing your code and understanding what is happening when you run it.
Posting code here is problematic, so I would suggest using the discussions on the SPServices Codeplex site. http://SPServices.codeplex.com
M.
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
Harmeet:
The User Information List is still there in SharePoint 2007. It’s a hidden list at the root of the Site Collection.
M.
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!
Gina:
I’d suggest adding some alerts into your scipt to be sure that you’re getting good values back from the calls to the SPServices functions. If you have more than one People Picker on the page, you’ll need a more specific selector. Take a look at this post:
http://sympmarc.com/2010/03/16/set-a-people-pickers-value-on-a-form-revisited-with-jquery/
M.
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
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.
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
Hi Marc
This is great.
I tried to populate the people picker field but to no avail. Do you have any pointers?
Thanks so much!
HC Chan
var userName = $().SPServices.SPGetCurrentUser({
fieldName: “Account”,
debug: true
});
$(“input[Title=’AttorneyName’]”).val(userName);
http://spservices.codeplex.com/wikipage?title=$().SPServices.SPGetCurrentUser.
HC:
A People Picker isn’t a simple input element, but a control made up of multiple elements. Take a look at this post.
M.
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.
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.
You sir are a genius! I keep coming back for great solutions. Keep up the great work!
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
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.
can you give an example?
The code in the post is really the example. If you stuck that in a file called “form_xyz.txt”, added a CEWP to the form page
http://sympmarc.com/2010/10/27/add-web-parts-to-sharepoint-forms-without-sharepoint-designer/
and pointed to the “form_xyz.txt” file in the Content Link, you’d have the basic set up done. You’d then need to adapt the contents of the file to match your column name(s) and file locations.
M.
Excellent solution. Works for me. Thanks.