Microsoft

Blog Categories

Subscribe to RSS feed

Archives

SharePoint 2010: Getting User Information in InfoPath Form Services

Situation

So you’ve got this nice InfoPath form, and you want to pre-populate it with user information. This means you’ll need to somehow get the user’s login information into the InfoPath form. Unfortunately, there’s no simple or straightforward way to do this.

Several blogs reference just calling the form’s User property and everything magically works in SharePoint. I can tell you from personal experience that this isn’t the case. There are other blogs saying to use a new field in the form that has its Default Value set to InfoPath’s userName() function. This also doesn’t work, at least not completely.

Solution

So what do you do to get user information for the current user within an InfoPath form? You use a combination of the above techniques. That might seem easy, but it has its tricks, which I’ll cover when we get to that point.

The first point I want to make is that you must use code-behind to access and manipulate user information in InfoPath Form Services. It cannot be done from the InfoPath designer because the binding of the User property doesn’t happen in time.

Create a Field Called UserName

The first thing you need to do is create a Text field on your form that has its Default Value set to userName(). I named my field UserName to avoid confusion. userName is an InfoPath function that binds user data to the InfoPath form. It’s technically supposed to populate this field with the user name, but it doesn’t. It does populate the User property of the form. Interestingly enough, the User property is populated on Form_Load only if the call to userName() is made. Otherwise, User is null.

image

 

Use the Form’s User Property

This User property isn’t very useful. It only contains a UserName and LoginName property, which are both strings.

However, LoginName can be used in conjunction with your login provider to convert the LoginName to something useful. You can also use it to pull back more data about the user from a membership provider or SharePoint itself. That information will allow you to continue populating your form with relevant data.

So there you have a way to get the current user’s login information in InfoPath that will work in InfoPath Forms Services on SharePoint. Not particularly difficult, but definitely convoluted. Moreover, you have to be ok with deploying code behind for your forms.

Tags: ,

Leave a Reply