18.09.2011 I have posted a new solution which makes this one obsolete. You find the new one here
09.09.2010 Updated the function “getUserInfo”. It no longer requires the list GUID for the user list to be specified in the script.
31.10.2009: Small update for default picture in the example CEWP.
This article describes how to access the “user profile” under WSS 3.0 via javascript. The method used is a query trough the webservice lists.asmx.
I use some code (“interaction.js” and stringBuffer.js”) created by Erucy and published on codeplex.
The jQuery-library is found here. The pictures and the sourcecode refers to jquery-1.3.2.min. If you download another version, be sure to update the script reference in the sourcecode.
As always we begin like this:
Create a document library to hold your scripts (or a folder on the root created in SharePoint Designer). In this example i have made a document library with a relative URL of “/test/English/Javascript” (a subsite named “test” with a subsite named “English” with a document library named “Javascript”):
You can call this script from any page. In this example i will place it on Default.aspx.
The sourcecode for the “AccessUserProfileInWSS.js” looks like this:
/* getUserInfo - Returns "user info" data from user list in WSS 3.0 (not MOSS user profile) * --------------------------------------------- * Created by Alexander Bautz * alexander.bautz@gmail.com * https://spjsblog.com * LastMod: 20.09.2009 * --------------------------------------------- Refer these scripts: interaction.js // Erucy - http://spjslib.codeplex.com stringBuffer.js // Erucy - http://spjslib.codeplex.com jQuery // http://jQuery.com Use: var ui = getUserInfo(); // If UserId is not specified it assumes it's logged in user (_spUserId) alert(ui.Title); */ function getUserInfo(UserId){ wsBaseUrl = '/_vti_bin/'; var uiObj = {}; if(typeof(UserId)=="undefined" || UserId=='')UserId = _spUserId; var arrOfFields = ['ID', 'Name', 'Title', 'EMail', 'Department', 'JobTitle', 'Notes', 'Picture', 'IsSiteAdmin', 'Created', 'Author', 'Modified', 'Editor', 'SipAddress', 'Deleted']; var item = getItemById('UserInfo',UserId,arrOfFields); if(item != null){ for(i=0;i<arrOfFields.length;i++){ if(item[arrOfFields[i]]!=null){ uiObj[arrOfFields[i]] = item[arrOfFields[i]]; }else{ uiObj[arrOfFields[i]] = ''; } } return uiObj; }else{ for(i=0;i<arrOfFields.length;i++){ uiObj[arrOfFields[i]] = "User with id " + UserId + " not found."; } return uiObj; } }
Save this as a text file and rename to “AccessUserProfileInWSS.js”, then upload to the library as shown above.
You call the script from a standard CEWP like this:
Sourcecode for CEWP:
<script type="text/javascript" src="/test/English/Javascript/jquery-1.3.2.min.js"></script> <script type="text/javascript" src="/test/English/Javascript/interaction.js"></script> <script type="text/javascript" src="/test/English/Javascript/stringBuffer.js"></script> <script type="text/javascript" src="/test/English/Javascript/AccessUserProfileInWSS.js"></script> <script type="text/javascript"> var ui = getUserInfo(); // If UserId is not specified it assumes it's logged in user (_spUserId) picSrc = '/_layouts/images/person.gif'; if(ui.Picture!=''){ picSrc = ui.Picture.split(', ')[0] } var str = ''; str += "Accountname: " + ui.Name + "<br>"; str += "Full name: " + ui.Title + "<br>"; str += "E-mail: " + ui.EMail + "<br>"; str += "Department: " + ui.Department + "<br>"; str += "<img alt='' src='" + picSrc + "' />"; document.write(str); </script>
And you get a result like this:
I will follow up this article with an article describing how to hide or show form fields based on membership – or not membership – in a SharePoint group.
Alexander
great work. I have been having an issue, but I do not think it is your script. In the past I was using firebug before I found out about IE Dev Tool. Uues a similar method identifying the user. for some reason I come up as user ID 1, which I always felt to be incorrect. Your script does the same thing and return the message no info for user id 1. Do you know why that might happen? what can be done to fix or change that?
I did some more digging and still cannot figure out why it can read the user ID 1, but will not return data for user id 1. the first thing that comes to mind is maybe my network firewall, but reading through the scripts, it does not look like it goes outside the firewall. Your script appears to be functioning because it writes the user id in the message. I will continue to look through this. I think this is some great work. whenever I see a request where I think your work would fit I always point them to this blog. keep up the great work
Hi larry,
The variable _spUserId is supplied by SharePoint, but your page must be attached to a MasterPage for it to be present.
You can check that your id is correct by looking at your own profile (dropdown on your name in the top right corner – My settings – the id is in the URL-field)
Check if the variable is present in your page by typing in the URL-field in your browser while in your page: javascript:alert(_spUserId).
I do believe that it is your ListGuid of your “UserList” that is wrong. please doubleCheck it.
I will update the script i little bit to replace null-values with a blank string.
Alexander
I ran through and everything appears to be correct. My account is userid 1, and your script writes that to the page, just returns not found. I havd come across a similar script a while back that did a similar thing. initially I had the same issue, but I was able to figure out what i did incorrectly, this script http://www.endusersharepoint.com/2009/01/20/jquery-for-everyone-degrading-dynamic-script-loader/ I can write the account Display Name to the page, which appears to doing almost the same thing. I also added this to a different site, updated the listname, still the same results. I will continue to work at this and will let you know what I find
I am not sure why it can write the _spUserId in the error message, but not write the details to the page. going to have to let it go for now. when I look through the interaction.js file I found references to a few URL. I could not open them in my browser, one was not found and another was error loading. could this be part of the problem?
Hi,
Did you check the list GUID of your UserList?
The one supplied in the topmost codeview – line 20 – must be changed to match your List’s GUID.
Alexander
Yup checked and double checked
Hi,
If you have tested with the code supplied unmodified and do not have a picture in your profile – you have to updated the code after i updated it to prevent null-values. Look at the code in line 33-39.
If this is not the issue – try to insert an alert betveen line 33-34 like this:
alert(item[arrOfFields[i]]);
Does it trigger?
Is your user in a access group with permission (from permission levels) to:
Use Remote Interfaces – Use SOAP, Web DAV, or SharePoint Designer interfaces to access the Web site.
And
Browse User Information – View information about users of the Web site.
I have not tested with a user without these permissions, but it may influence.
Alexander
Updated with new code. I do not have a picute uploaded, and the image place holder has that missing “X”. My user account is a site collection admin. I am also added to the site in a group with full control. your script is working as designed because it writes this to the page:
User with id 1 not found.
, and My info is user id 1. I will continue to play with this. Hopefully we can figure this out.
also I added the alert line, but no alert triggers.
Try this in your CEWP:
If this works – try this to alert the loginName:
Alexander
Larry,
Is your system WSS 3.0 or MOSS 2007?
Alexander
wss 3.0, I have not tried the changes yet. I will report back as soon as I get a chance.
thanks for all your support.
I put the alerts in, first got the function to return in the alert. Second got first 2 to alerts, then the last alert “User with ID 1 not found”
this was another helpful post from endusersharepoint http://www.endusersharepoint.com/2008/11/26/power-user-toolbox-javascript-for-sharepoint-pt5/
I was able to get this to work like yours by adding a couple of fields
I would be highly interested in how Larry was able to get the CEWP to show the proper information. I have the same issue with the error “user with ID1 not found” populating each field instead of the accounts information. I’ve run the same tests, all positive, listed above by Alexander as well. The same error with another user login as well (except saying user ID8 in this case). I’m working on toward your next article on hiding form fields based on user permissions and would really like to find this a working solution. I wouldn’t think testing through a VPN would make any difference. Thanks in advanced.
Hi Clint,
I don’t know if Larry got this working, but i have one question that i newer asked Larry:
Does your site collection reside in a managed path, or on the root URL?
Alexander
The site in question is on a managed path. This is being hosted on an SBS 2003 install with the standard ‘Company Web’ and such as well as a few administrative programs that have their own site hosting for making configuration changes (ex. SpiceWorks). This site is on the default sites path. There is another site on the root but that seemed necessary to get the search function to work properly . . . It’s been awhile, but I clearly remember it being quite a pain. At any rate, yes this site is on a managed path.
Well, then i think the answer is to modify line 23 like this:
wsBaseUrl = ‘/sites/_vti_bin/’;
This is the path to where the web service “lists.asmx” is found.
“sites” being the managed path of your site collection – modify as needed.
Alexander
Holy Syntax Batman!!
You hit the nail on the head Alexander, thanks. Although I’m still new in scripting JQuery, I should have seen that one. After I changed the location to “/sites/main/_vti_bin” the script pulled the info in a heartbeat! I’ll be moving on to your next article on hiding fields based on user member ship. Thanks again, I’ll be sure to pass your blog on and add it to my list of good SharePoint resources 🙂
I’m glad it worked! -I will update the post to avoid similar problems.
Alexander
@Alexander, I actually gave up on this script. I did not want to be a pest. I did however get another script to work, which is what I was referring to. Since I have seen the update here I went back to try and implement this again and much success. Thanks again. Now I can move on to the post that followed this one.
Did you update the script to address the “Managed path” and got it working?
Alexander
Yes.
k, I am back playing with this script. two questions, first i’ll start with the easy one. When I run this script I dont have a Pic for my details, so I get red x for missing images. is there a way to show the default shadow image if a pic is missing?
part 2, I have been trying to marry this script to another, manipulate SP fields for javascript. What I am trying to do is one of 2 things. either the form opens and reads who the user is, then populates several fields based off user information, or in a people/group field user selects a name, and that triggers several other fields (email.phone etc) to populate. Can this be done? I am thinking the a few more lines of code in your script should handle it, but when I add javascript I cannot get it to work. I am still learning jquery.
I do appreciate all your efforts. have a great weekend
Hi,
I have updated the code in the CEWP to have a default picture.
Your other question is best answered by pointing you in he direction of the function “setFieldValue” from the file “fieldutility.js” found here:
http://spjslib.codeplex.com/
Alexander
Hello Alexander,
Can u please send me the stringBuffer.js and interaction.js file ?
Help is highly appreciated.
Regards.
Bhavesha
Hi,
The files are found here:
http://spjslib.codeplex.com/.
It is not me who have made these scripts, and i feel it it right that you get the files from the author directly.
Sorry for the inconvenience, but to honor the author, please visit his site and download them from there.
Alexander
hi,
is it possible to display information of a client and not of the current user?
the name of the client stands in the field “client” in dispform.aspx.
thanks
Jan,
there are several ways to prepopulate fields on the “NewForm”. One of the simplest is least secure is through the use of query strings.
The DispForm should already have existing data which would make is easier to manipulate.
Can you provide more details of what you’re trying to do?
Get the userId from the “client” field in dispForm like this:
The “Show field:” in list settings for the people picker must be one of the “Name” options as this script gets the id from the link to the User information page.
Alexander
thanks for your help.
now it works.
jan
You are absolutely awesome Alex. So helpful to all of us. Thank you for the great work!
I have a question about this comment:
// If your site collection resides on a managed path, “wsBaseUrl” must reflect the root of your site collection like this:
25 // wsBaseUrl = ‘/myManagedPath/_vti_bin/’;
I’m new to this, so I appoligize if this is a dumb quesiton… how do I know if my site resides on a managed path?
Hi,
If the site collection is not on the root, it is on a managed path.
If the site collection resides on this address: http://acme.com, it is not a managed path.
If it resides on http://acme.com/sites/YourSiteCollection, it does – and the managed pathe will be “/sites/YourSiteCollection”.
Alexander
Great, working like a charm, also with sharepoint foundation.
Thank you very much
I’ve been using this previously and it’s great. 🙂
I notice that you now say that list ID isn’t necessary, but I’ve a bit confused about what you mean:-
“Define userListGuid (list guid of the user list on your root site) as variable accessible to this script”.
Where do I declare that?
Also, in my set up there’s different “people and groups” lists used for different sites. Is there one overall one I can access, or do I need to update it depending on which site the app’s running on?
Also, the managed path reference seems to have gone. Is that not needed any more, or I am missing it somewhere?
Thanks.
Hi,
After the update 09.09.2010 i didn’t remove all traces of the “list GUID requirements”. I have removed them now – Thank you for noticing!
Managed path is no longer an issue.
In a site collection there are only one “Userinfo list”. Even if a subsite do not inherit security, all users and groups are defined in the site collection root.
Alexander
I got a bit confused by the rating system and seem to have rated my own comment. I finally figured out you vote for the article by clicking on the stars at the top. I assumed that was just a display originally.
Hi,
This scripts is exactly what I was looking for (and I lost count of how many I looked before)!! But I must have done something wrong because the user information that is showing is not mine.. Any ideas?
And have you posted already the article about the membership?
Thanks!
Hi,
I need some more information here:
1. Do you get a completely random result, or do you get the same result every time (the same person)?
2. Is it 2007 or 2010?
3. Is the site on a managed path?
Anything else that might be worth mentioning?
Alexander
Thanks for the reply.
1. I always get the same result. Each person that logs in get a single user, but also not the correct user.
2. It’s 2007
3. Yes
Could you please try to replace the text ‘UserInfo’ in line 25 in the code with the actual list GUID of the user list.
To get the GUID, go to the “People and groups” list, right click – view source – search for ctx.listName.
Alexander
Dear Alexander,
I had the same issue Christianne mentioned before – my subsite is on a managed path. I tried with alerts in the script and identified that the ID of the subsite on a managed path can have a different meaning on the mastersite. I had still a copy of the old sourcecode of your script and added the listname and the managed path and it was working like a charm 🙂
I don’t have access to the master site information, therefore I don’t know the exact reason for the difference.
BR
Wyl
Hi,
Managed paths always make things a bit harder… To have this script pull information from the right list when on a managed path, change the wsBaseUrl in line 20 like this:
/myManagedPath/_vti_bin/
Alexander
Hi Alex,
Great post (this and the related on on filtering views by group). I have 2 questions. The first is about security: do all authenticated users, even very limited ones, have access to the lists API via this sort of client-side scripting? Also, in 2010 (I realize you’re not explicitly supporting it) getItemById is returning null for all users I’ve tried (administrators mostly). I tried replacing the UserInfo reference with the GUID from _layouts/people.aspx.
This is all related to 2010, so I may be swimming in my own creek.
I think it would be available for all users, but it depends upon the configuration. It have to be tested in your local setup to be sure.
This code should work in both 2007 and 2010, but might be a bit tricky if your site resides on a managed path.
Can you provide some more info? – if possible, send me some screenshots.
You find my email here
Alexander
I liked this article. It helped me get the user profile properties. I’m now trying to display them within a content editor web part. But when I placed the “document.write” command in there, it shows only a white screen with my text. The rest of my SharePoint page does not appear.
It’s frustrating. All I want to do is display the text. I look on jquery.com and other places and I find examples for all sorts of complicated things but not displaying text. I don’t want to do an alert window either.
Paul
Hi,
Add a div in the top of the CEWP like this
Then change the line with document.write like this:
Alexander
Alexander, that sounds like it should work, but the text that I want to appear does not appear. I may have a Javascript error. Let me debug it and get back to you. Thanks.
Paul
Hi Team,
Can you tell me what is standard CEWP ? Where i can get that ? What webpart i can add in my page and write this javascript to check ?
Please give me the answer here or email me. It’s really appreciable if you can help me quickly..
Hi, The CEWP is the Content Editor Web Part that is shipped with both WSS, MOSS and SP2010. Edit page and insert it into any webpage.
Alexander
Hi Alexander,
Wow I have checked my installed Sharepoint 2007. But there is no such webpart. I found Picture and other webpart but not this one.
Can you tell me from where i can add this content editor webpart in my sharepoint 2007 ? Or something i need to active or like ?
You should find it under the “Miscellaneous” section. If you do not find it, it may have been removed by your server administrator.
Alexander
Hi,
thanks for the code and it worked with current user.
But when i gave other user id like (domain\userid) it is returning the user is not found.
I tried with different ids but the same result.
Kindly help me out in this
HI,
The code found in this page does not support getting the user by login name. Follow the link to the new version in the top of this article.
Alexander
Hi I have a system used by many users and I have accessuserprofiels.js used on a list. It works for all users , but for one user it says”User with ID not found”. Can anyone help me with this please. I am not sure why it did not work when the script works fro 400 other users.
Please help me out.thanks in advance.
Hi,
Sorry for the late reply have you tested the new version?
Alexander
– this feature is not working in google Chrome v 32>=<v40 Can't find the user id.
– in sharepoint 2013 _spUserId is changed to
_spPageContextInfo.userId
but anyway… in google chrome the feature is not working (neither 210/2013).
Hi,
This solution is 5 years old, and was created for SharePoint 2007. You find a link to a SP2010 solution in the top of the article, and if you change _spUserId to _spPageContextInfo.userId you can get it to work in SP 2013.
Alexander