Microsoft InfoPath 2010
The official blog of the Microsoft InfoPath team

Goodbye WhoAmI! Hello userName()

Goodbye WhoAmI! Hello userName()

  • Comments 18
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
Leave a Comment
  • Please add 1 and 2 and type the answer here:
  • Post
  • 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

  • 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?

  • 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?

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

  • PingBack from http://copyrightrenewalsblog.info/infopath-team-blog-goodbye-whoami-hello-username/

  • 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?

  • 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

  • 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.

  • 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

  • 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

  • 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

  • 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.

  • 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

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

    Nick

  • How can i change the view based on user group, I have three groups User,Team member and manager and three forms based on their requirements.

Page 1 of 2 (18 items) 12