Thursday, July 03, 2008

Get Current User for Infopath and set file name - PART I

Today we'll look at retrieving current users' Information as well as naming the infopath files using the fields on the form.
for the sake of length, I'll divide this post in two part.

In this part, we'll see how to get current users' profile properties in Infopath Browser forms using UserProfileService.asmx

Let's consider an example called Product Request Form where a particular sales employee of the company request for shipment of a particular product

1. First Open the Infopath( this can be done by simply typing 'infopath' in run prompt) and choose a blank template to start with. Make sure you have selected Enable Browser Compatible Features only

2. Add Following Fields to the Main Data source

products: Text
expectedDate: Date
CurrentUserName: Text
IsFormLocked: Boolean
fileName: Text
Error: Text

3. Create the form Layout as below:

Lets insert Some values in the products dropdown:

4. Firstly, we have to get the Current user's name in the Requesting User text box
and the text box be read-only. For this, we will consume the SharePoints' much hyped userprofileservice.asmx's method called GetUserProfileByName

5. For this, go to Design Task -> Data Source -> Manage Data Connections -> Add...
In the Wizard:
   a. Choose Create a new connection to: Receive data

   b. from the Source of Data, select Web Service

   c. In Url, enter your Sites' url follwed by _vti_bin/UserProfileService.asmx

   d. Out of the list of methods, choose GetUserProfileByName

   e. In Next screen, you'll be asked for Account Name, leave it blank as doing so would get the current User.

   f. Make sure in consecutive screens that Store a copy of the data in the form template is unchecked and Automatically retrieve data when form is open is checked

6. In the text box mapped to the field CurrentUserName, we would need to format the display value to that retrieved from UserProfileService.asmx
thats configured to populate its values on form Load. For this, go to Textbox's properties and in the Value field, go to formula builder by clicking fx

7. Click Insert Field or Group.... Choose the secondary receive data source we just created and select value field but don't click Ok yet!!

8. Instead, click on Filter Data... -> Add... to open the filter condition dialog and choose Select a field or group...

9. Choose Name Field again from the Secondary Data Source just created:

10. In the second drop down, select is equal to and in the value drop down, choose Type a Text... and type AccountName

Note: you can choose from a list of profile properties you want:

11. Finally, your formula should look something like this:

12. I Guess, this is it, for getting the current users' loginName.. you could have opted for any properties from the list of available properties above.

In the Next Post we'll see how to save the file with a predefined value.. might be.. a format like:


rejoyce said...

This is great. I have already implemented it, but I am only returning the Administrators profile, not the users information. I have searched the internet and cannot find the answer to my problem. Any help would be greatly appreciated.

Francisco Javier Rojas Castro said...

that web service, only return users who created one site on My Site

Anonymous said...

good one.....

Rouquin said...

Dude, thank you so much, your tutorial helped me a lot!

Muhammad Fahad said...

I was trying this by using FirstName as field but got nothing back, tried yours as AccountName and it worked. Also I tried Manager but no luck. Any workaround for this? why it only works with AccountName. thanks

William said...

Hello. Is there a way to alter this method, so when I create the InfoPath form for the first time and the Current User Name appears in the text box, it stays the same name and does not change? Reason I ask is because this form will be submitted to different users for approval and I want to keep this field constant in order to identify whom originall created it. Thanks!

Adil A. Baig said...

There are lot of ways to go about this. You may a have a field (label/textbox) on the form not connected to any of the list columns. Then on the first load, when the user is filling out the details and submitting for review, you can copy the data from the field originally displaying current user into this field.
This field then would never change and you'd have the data from the first user, the submitter of the form saved on that field for other reviwers.
You might want to apply the stage check, as in which stage the workflow has reached through some variable.
then based on the status of this variable, you can decide whether or not to copy the data from the live "current user" field or not to the static field.

William said...

That is a good idea. I created a satic txt box under the Current User Name field. What is the best way to copy the data from the Current User Name field? I was going to click on the Fx icon and set the Current User Name field as the default but I dont think this is correct?

Anonymous said...

I am using SharePoint online office 365. I want to do the same thing in my site. I am unable to create the connection. Can any one help? How can I create a connection to access the user profile or GetUserbyProfileName?

Thanks in advance.
M. A. Khan

Vipin Menon said...

Thanks Adil.. I have been looking for this since long.. blame my poor Google skills, but this is first time I stumbled on a complete end-to-end solution

Imran Ansari said...

Hello, I would like to fetch all users in his current department. How to do that?

Anonymous said...

Any suggestions for when you do not have MOSS and only WSS, as the UserProfile web service is only available in MOSS?

Adil A. Baig said...

Nope, UserProfile web service is a component only available with MOSS 2007, so this method would not apply to WSS 3.0

e signatures said...

THANK-YOU so much for taking the time to create this easy-to-understand post!