Friday, July 13, 2012

InfoPath 2010: Pre-populate the People Picker

This solution can be done with any version of SharePoint 2010 or SharePoint Online with Office 365.
Here’s a little bit of background on how the people picker is structured:image
It is structured as a repeating table, and the “group” can be renamed. No other fields can be renamed
DisplayName = Nizamuddin
This is the first name and last name of the user, which is going to be equivalent to the value of the PreferredName field in the user profile service.
AccountId = contoso\Nizamuddin
This is domain\username
AccountType = User
This is User or Group.
image
Here are the steps to prepopulate the people picker:
  1. On the Data tab, click Data Connections. Click Add..
  2. Choose Receive data and click Next. Choose SOAP web service, and click Next.
  3. Type the URL of your site, and after the site, type /_vti_bin/userprofileservice.asmx
    Click Next.
  4. Choose GetUserProfileByName. Click Next. Click Next. Click Next. Click Finish. image
  5. All we’ve done so far is create a connection to the user profile service web service, so that we can get some additional information about the logged in user. Since we only want the people picker to get pre-populated when it’s a new form, we’re going to create a form load rule that does this.
    If you haven’t already placed a people picker control on your InfoPath 2010 form, go ahead and do that.
  6. On the Data tab, click Form Load.
  7. In the form load rule pane on the right, click New and choose Action.
  8. We only want this to happen if there’s no name in the people picker yet, so we’ll create a condition first. Under the word “Condition”, click the blue word None – …
  9. Creating the condition: In the first drop-down box (myFields), click Select a Field or Group. Navigate down to your people picker control group and expand it. Select the AccountId and click OK.
    CropperCapture[38]
  10. On the condition’s second drop-down, choose Is Blank. Click OK.
    CropperCapture[40]
  11. Back in the form load rule pane, click Add and choose Set a Field’s Value.
  12. For the Field box, select the DisplayName field inside your people picker form (that DisplayName field you see in the screenshot at step 9). Click OK.
  13. Click the fx (function) button next to the Value box. Click Insert Field or Group… In the Fields drop-down box, choose GetUserProfileByName.
    CropperCapture[41]
  14. Drill all the way in the DataFields until you get to Value. CropperCapture[42]
  15. Click the Filter Data button. Click the Add button.
  16. In the first drop-down box that says Value, choose Select a field or group. Select the Name field. Click OK.
  17. Leave the middle drop-down to say Is equal to. In the third drop-down box, choose Type text. Type the word PreferredName It will automatically put the quotes around it for you, you don’t need to type those. (this is case sensitive, so you have to type it exactly right) Click OK.
    CropperCapture[43]
  18. On the Filter Data screen, click OK. On the select a field or group screen, click OK. On the insert formula screen, click OK.
  19. This is what your Rule Detail screen will look like. Click OK.
    CropperCapture[44]
  20. Back over in the form load rule pane on the right, click the Add button again, and choose Set a field’s value.
  21. For the Field, drill down to your form’s people picker, and select the AccountId field, as seen in step 9. Click OK.
  22. Repeat steps 13-16.
  23. Leave the middle drop-down to say Is equal to. In the third drop-down box, choose Type text. Type the word AccountName Click OK.
  24. This is what your Rule Detail screen will look like. Click OK.
    CropperCapture[45]
  25. In the Rule pane on the right, click the Add button yet again. Choose set a field’s value.
  26. For the Field, pick the AccountType field within your people picker. You can see what this field looks like back in step 9. Click OK.
  27. In the Value box, just type the word User
  28. This is what your Rule Details screen will look like. Click OK.
    CropperCapture[46]
  29. Done. Publish your form and try it out. When you fill out a new form, your own name will be populated in the people picker.