User Information within InfoPath Forms
There is a quick and easy way that information about the logged in user can be obtained from within an InfoPath form. You may have read my blog and other blogs before, with the user profile service web service instructions. In this post, I'm not going to be using the user profile service. This means that what I'm about to show you can be utilized with any version of SharePoint, whether you have a user profile service (enterprise) or not. This can be done with SharePoint 2010 Foundation, SharePoint 2010 enterprise and standard, and also WSS 3.0 and MOSS.
The User Information List is a list that exists at the site collection level, and contains a list of every user who has actually visited the SharePoint site. If they are a new user, they will not show in this list until they've actually visited a site in that site collection.
With that said, here are some instructions for getting some information about users, more specifically, the currently logged in user.
There is a quick and easy way that information about the logged in user can be obtained from within an InfoPath form. You may have read my blog and other blogs before, with the user profile service web service instructions. In this post, I'm not going to be using the user profile service. This means that what I'm about to show you can be utilized with any version of SharePoint, whether you have a user profile service (enterprise) or not. This can be done with SharePoint 2010 Foundation, SharePoint 2010 enterprise and standard, and also WSS 3.0 and MOSS.
The User Information List is a list that exists at the site collection level, and contains a list of every user who has actually visited the SharePoint site. If they are a new user, they will not show in this list until they've actually visited a site in that site collection.
With that said, here are some instructions for getting some information about users, more specifically, the currently logged in user.
- Open up your InfoPath form, click on the Data tab, and click the Data Connections button.
- Click the Add… button.
- Choose Receive Data, and click Next.
- Choose SharePoint List or Library, and click Next.
- Paste the URL of your SharePoint site, click Next. (No need for any "default.aspx" at the end, or any other .aspx)
- Scroll down towards the bottom of the list of lists and libraries, and select User Information List. Click Next.
(note that even though the user information list physically exists at the site collection level, you will still see this list on every sub-site also) - Put check boxes next to the following fields, especially User_name. Click Next.
- Do not check the box to store a copy of the data. Click Next.
- This is very important. UNCHECK the box to automatically retrieve the data. Click Finish.
- On the Data Connections screen, click Close.
- Still on the Data tab in InfoPath, click the Form Load button, which will bring up the rules pane on the right side of the screen.
- Click the New button to create a new Action rule.
- Click the Add button and choose Set a Field's Value. On the Rule Details screen, click the button next to Field.
- In the Fields drop-down box, choose User Information List, expand the queryFields, and select User name.
(be sure that you're selecting from the query fields, not the data fields.) Click OK. - Back on the Rule Details screen, click the function (fx) button next to the Value box.
- Click the Insert Function button, and choose the userName function. Click OK two times.
- This is what your rule looks like. Click OK.
- Back over in the rules pane on the right, click the Add button, and choose Query for Data.
- Choose the User Information List and click OK.
- Now, each time the form is opened, the current user's information will automatically be queried, and that one record will be retrieved. There are many ways that this information can be used within your form. The data exists in the data fields of the user information list. In the Fields pane on the right side of the screen, choose the User Information List from the drop-down box.
- Select the folder directly under dataFields, the one with the blue icon called d:SharePointListItem_RW. Drag it onto an empty area of your form, and when you let go, choose Repeating Section with Controls.
I'm really just having you put all this on the form so that you can see exactly what's being returned. - Go ahead and publish the form, and open a new form. You'll see all of the data about you will be displayed, something like this:
- One more little trick. Go back to InfoPath Designer. Delete the "Picture" field. Add a Picture control to the form (not a picture button, just a picture), and choose As a link. Click OK.
- You will be prompted to bind to a field. Select the Picture field under the User Information List data fields. Click OK.
- Filling out the form again, now there's me:
You can see how this data could potentially be used in many ways in your form. You can set some field defaults the first time the form is filled out, you could display the currently logged in user's picture… You can even use formatting rules to show, hide, or disable certain fields depending on who the current user is or what department they're in. I don't usually drop the fields on the form like I showed you, that was for demonstration purposes. I usually just refer to that data via rules, etc. Keep in mind that the user information list does not have the 60 or so fields that the user profile service contains, but it is very useful nonetheless. There are many other blog posts out there that show how to tap into the user profile service using the web service in InfoPath, it's just that if all you need is one of these basic fields, you might not need to deal with all of the complexity that it entails.