Welcome to MSDN Blogs Sign in | Join | Help

InfoPath Team Blog

Tips and tricks to get the most out of Microsoft InfoPath

News

  • For questions, comments, and feedback please use the public newsgroup: microsoft.public.infopath
    This is provided "AS IS" with no warranties, and confers no rights. Use of included script samples and forms are subject to the terms specified in the Terms of Use.
Goodbye WhoAmI! Hello userName()
Anyone who's tried to get the username of the person filling out their form in InfoPath 2003 knows the WhoAmI web service. That was the only way to get the username without writing Visual Basic or C# managed code and using System.Environment.UserName.
 
In InfoPath 2007 we built an easier way.
 
Announcing a new formula function: userName()
Now you can simply set the default value to the current form-filler's username. No code, no data connections, no custom web service. Just use the formula dialog and select the function from the category "All":
 
 
To get a default value like this:
 
 
If that's too easy for you, use it in script
If you're writing script anyway, or if you need to check this value as part of a bigger algorithm and don't want to persist the username anywhere in the form, then you can also access this value in script:
 
Application.User.UserName
 
Of course, you can also use this in C# or Visual Basic, but you could always use System.Environment.UserName, so that's nothing new.
 
Either way requires Domain Trust
For security reasons, either approach will make your form require domain trust, so you'll need to publish the form to a shared location (like Windows SharePoint Services) for the function to work.
 
Enjoy!
-ned
Posted: Friday, May 26, 2006 8:39 AM by infopath
Filed under:

Comments

infopath said:

Kudos to David Gerhardt for covering a scenario around userName in-depth in his blog article: http://blogs.3sharp.com/Blog/davidg/archive/2006/11/14/2380.aspx

- Alex

# November 16, 2006 11:19 PM

sayangh said:

I have my Infopath 2007 form published to a Sharepoint server and it has been granted domain trust, but still a text-box with default value  userName() shows up blank. Am I doing anything wrong?

# April 13, 2007 8:12 AM

Wosiu said:

I have my InfoPath 2007 form published to a Sharepoint server with FULL TRUST and keep getting 'system' returned by userName() function. Is there any way to overcome that?

# May 22, 2007 10:24 AM

Aziz said:

i was trying to make a form with a field that contain Employee name i found the best way to do so is

# January 28, 2008 3:44 AM

vishwaswamy said:

I am trying to develop an application using infopath 2003.

but i cant find the above Username() field in my insert function box, is the any other pre-settings to be done?

# June 20, 2008 7:23 AM

infopath said:

Hi vishwaswamy,

As per the information at the beginning of this post, the "userName()" function is new to InfoPath 2007 - as such, you will not find this in InfoPath 2003.

Now, if you stated you were using InfoPath "2003" in error, then this function is available in the UI. Here is a sample for finding the function:

- Add a button to your View

- Add a Rule to the Button

- Add an Action to the Rule as follows:

    - Action: Set a field'a value

    - Field: Select the field where you want to set the user

    - Value: Click the "fx" button

         - Click Insert Function

         - Select "All" in the Categories list

         - Scroll to the bottom of the Functions list and you will see "userName"

Scott

# June 23, 2008 8:49 AM

john@thibeaux.com said:

I have a question about using this function. It works find when the form loads. I however am trying to get the function to recalculate after the same form is opened again later. How does this work? Pease point me in the right direction. Thanks.

# July 9, 2008 11:13 AM

cgk said:

Hi everyone!

I am having what seems to be the same issue that by john@thibeaux.com posts about and I am wondering if anyone has a solution.

More specifically, I am trying to get the userName()

function to recalculate after the same form is opened by another person. The reason this is important in this effort is that we want to use the response to determine user role so that we can do dynamic view switching in InfoPath.

Even more specifically - This InfoPath form is expected to be opened, filled out, or later edited using a browser through SharePoint 2007 form Services.

Any responses would be appreciated. TIA

# November 5, 2008 2:00 PM

firouz said:

hi

I have a problem in this case. I have Used This method but I want that every time someone open the form, it get the new username dynamically. but the form always save the first username it has got. help me please

# April 4, 2009 8:44 AM

infopath said:

Hi firouz,

When using the "userName" function in the default value property as described here, this only changes (i.e. sets the property) when it is blank. If you need to get the user name each time the form is opened, you will need to do these steps:

1) Remove the "userName" function from the default value property of your text box control

2) From the Tools menu choose Form Options

    - Select the Open and Save category

    - Click the Rules button

    - Click Add

    - Click Add Action

    - From the "Action" dropdown box choose "Set a field's value"

    - Click the button next to the "Field" box and select the data source node where you previously had the "userName" function set as the default value

    - Click the "fx" button next to Value, click Insert Function and insert the "userName" function

What this will now do is fire the "userName" function and set your node each time the form is opened.

Scott

# April 6, 2009 8:43 AM

Nick H said:

Goodbye userName()!  Hello WhoAmI? *cry*

The link at the top of the blog to the WhoAmI web service (tutorial I assume?) is dead.

Instead of installing Infopath 2007 on everyone's computers just to allow me to use the userName() function, I wanted to look into using the WhoAmI codeless method for Infopath 2003 (which everyone has installed).  The problem is I cant find any good tutorials as to how to set it up.

Any links or help is much apprieciated.

# July 29, 2009 3:26 PM

infopath said:

Hi Nick,

Take a look at this MSDN article as it has the "WhoAmI" web service in there...it's pretty easy to setup:

HTTP Security and ASP.NET Web Services

http://msdn.microsoft.com/en-us/library/ms996415.aspx

Scott

# July 29, 2009 3:44 PM

Nick H said:

Thank you Scott I''ll give it a shot.

Nick

# July 30, 2009 11:42 AM
Anonymous comments are disabled
Page view tracker