Get the current user without writing code
I've just read IP team blog post about getting the current user's details using Web Services.
Actually the exact same result can be achieved without writing any code. It's a very common thing to do and I couldn't find any guide for that, so here it is:
* If you are already familiar with this, this post might still be useful. I have included the complete set of default properties get returned and their proper naming syntax at the bottom.
* This works at InfoPath as well as InfoPath Forms Services.
*Clayton Cobb has a great follow up post if you to take the this a step further.
- With InfoPath opened go to Tools > Data Connections, and click 'add...' to add a new data connection to the form. This opens up the Data Connection Wizard.
- We want to receive data from the WS about the current user, so choose receive data' and click next.
- Our data source is a WS so choose 'Web Service' and next.
- Now you will have to point the wizard to the WS. Type an address similar to this: http://ServerName/_vti_bin/UserProfileService.asmx and click next.
- Here you get a list of all methods for that WS, choose GetUserProfileByName and click next.
- In this screen you can specify what parameters are sent to the method, we are relying on the method's ability to return the current user name if no value is passed to it, so we will leave this as is (no value is passed to the method) and click next.
- Click next and make sure 'Automatically retrieve data when form is opened' is checked.
- Finish the wizard.
So Now that you have a data connection that can get the current users, you can use it values. In this example I will show the user's first name in a textbox.
- Add a textbox to the form.
- Go to the first textbox's properties (double click it).
- In the 'Default Value' part, click the 'fx' button next to the 'Value' field. this opens up the formula builder dialog.
4. Click 'Insert field or group'.
5. In the data sources drop down, choose the GetUserProfileByName data source.
6. Expand all groups under the 'dataFields' group, and choose the 'value' field. Don't click OK yet!
7. With data 'value' field selected, click the 'Filter Data...' button and 'Add...'.
8. In the first drop down (value) select 'Select a field or group...' and choose the 'Name' field under the 'PropertyData' group.
9 Leave the middle drop down as is ('is equal to') and in the last drop down choose 'type a text...'.
10. This is the part where you specify which property to put in the textbox. As we said the method returns multiple properties about the user. For this textbox we want to put the user's first name in, so type 'FirstName' (this is case sensitive!). I have included the property list you can use here (just below), so if you want some other property, just type its name instead.
11. that's it, all we have to do is to confirm everything so Click 'OK' for every open dialog box until you are back in the design mode.
12. click 'Preview' and see the wonder!13. If you want more details repeat steps 1-11 and enter different property names in step 10.
Finally, as I Promised, here is the complete list of default profile properties get returned by the userprofileservice. I think they are pretty self explained:
- UserProfile_GUID
- AccountName
- FirstName
LastName
PreferredName
WorkPhone
Office
Department
Title
Manager
AboutMe
PersonalSpace
PictureURL
UserName
QuickLinks
WebSite
PublicSiteRedirect
SPS-Dotted-line
SPS-Peers
SPS-Responsibility
SPS-Skills
SPS-PastProjects
SPS-Interests
SPS-School
SPS-SipAddress
SPS-Birthday
SPS-MySiteUpgrade
SPS-DontSuggestList
SPS-ProxyAddresses
SPS-HireDate
SPS-LastColleagueAdded
SPS-OWAUrl
SPS-ResourceAccountName
SPS-MasterAccountName
Assistant
WorkEmail
CellPhone
Fax
HomePhone