Summit 7 Team Blogs

InfoPath and SharePoint’s GetUserProfileByName Web Service

I used to do a lot of work with InfoPath forms, and still do occasionally. I have always used SharePoint’s GetUserProfileByName web service to automatically retrieve the current user’s information from Active Directory. This web service provides us with all of the information that was imported into the user’s SharePoint profile so it is important that the information is kept up to date in AD.  This method has always worked perfectly for me except for one issue:

What if another user opens the Form after initial submission?

I’d like to propose an example scenario to demonstrate this issue I typically encounter. I created a form for Employees to enter 'Leave Requests'. The form automatically populates the Employee’s Name and their Manager’s Name using the GetUserProfileByName web service. I am also using a SharePoint Designer workflow to send an email upon creation to the Employee’s Manager for Approval…or Rejection, but hopefully that won’t happen!

Here is a simple example of a Leave Request form I created for demonstration purposes:

View 1: The default view of the form. This is the initial form that the users will fill out (Employee Name and Manager Name will be populated with the GetUserProfileByName web service):

InfoPath and SharePoint’s GetUserProfileByName Web Service

Note: There are numerous blogs about how to set up your data connection in your form to retrieve this information so I don’t want to go into detail on that. I have always relied on Itay Shakury’s blog called ‘InfoPath – Get the current user without writing code’. It was the only one I could find when I first started working with InfoPath forms. Since his blog, there have been several other great ones, some going into more detail, but I’ve always relied on this one. It is an excellent, easy to follow step by step tutorial.

View 2: Approval View of the form. I always make this view 'Read Only' and add a link to this view in the workflow email that the Manager will receive to approve the Request. In addition, I like to turn all fields into text so it gives the appearance of a nicely filled out form rather than the user seeing drop downs or date fields. In addition, I will remove borders and shading so you can’t see the text box (only the actual text):

InfoPath and SharePoint’s GetUserProfileByName Web Service

Now, thinking about the issue I mentioned:

With the above scenario, the Manager will receive an email to review the Employee’s Leave Request and either approve or reject it. Well, what happens when the Manager goes to view the form? It will call the GetUserProfileByName web service again and instead of displaying the correct values from the user who initially completed the form, it will pull THAT current users information…the Manager that is now reviewing the form. So the result is: it will display the Manager’s name in the 'Employee Name' field and that Manager’s Manager in the 'Manager Name' field (if they have one).

I’ve used 2 workarounds in the past to get around this issue. One I like better than the other, but they both work:

Method 1:

This requires two simple steps. I’ve used these steps with InfoPath 2007 and 2010 forms.

Step 1: Create two new fields called 'EmployeeName2' and 'ManagerName2' as Single line of text for Data type. Think of these as your 'constant' fields. In other words, these fields are always going to retain the information of the user that filled out the form. ('Employee Name' and 'Manager Name' are your fields that will always populate from AD, so make sure the value of these fields is using the formula needed to pull the information from Active Directory.) Please see the blog post I mentioned above for the tutorial on how to do this.

InfoPath and SharePoint’s GetUserProfileByName Web Service

Step 2: Create two new rules on submission of the form (on your Submit button) that set your constant fields ('EmployeeName2' and 'ManagerName2' to the value of 'Employee Name' and 'Manager Name'). Type in a descriptive name for your rule such as 'Set Employee Name', do not change the Condition, and add an action of type 'Set a field’s value':

InfoPath and SharePoint’s GetUserProfileByName Web Service
InfoPath and SharePoint’s GetUserProfileByName Web Service

 Note: Be sure to set this rule to run before your rule to submit the form.

That’s really all there is to it. The last thing to remember is on your Approval view of the form (or your read only view) that the Manager will open, be sure to use your text fields that will remain constant (I.E. 'EmployeeName2' and 'ManagerName2'). Also be sure to add these same rules to your Approve and Reject buttons to keep the Manager Name and Employee Name from updating in your list after the Manager approves or rejects the form.

Method 2:

Although Method 1 works, this one is probably a much better and cleaner method, especially if you have several fields you are populating from Active Directory.

Step 1: When you set up your data connection, make sure you uncheck the following box:

‘Automatically retrieve data when the form is opened’.

InfoPath and SharePoint’s GetUserProfileByName Web Service

Step 2: This method requires that you create a field titled 'Leave Status' and set the default value to 'New':

InfoPath and SharePoint’s GetUserProfileByName Web Service

Side Note: It’s always a good idea to have a status field for your forms anyway so users can quickly tell what the status of their submitted form is or so you can create views based off of status. In addition, you can use this status field in your workflow emails that you send out once the form has been Approved or Rejected.

In the Form Load rules, add a rule titled 'Query for Profile Data' that checks if Leave Status is New. If it is, then select Query for Data Connection and select your GetUserProfileByName data connection.

InfoPath and SharePoint’s GetUserProfileByName Web Service jip9InfoPath and SharePoint’s GetUserProfileByName Web Service


On Form Load:

InfoPath and SharePoint’s GetUserProfileByName Web Service

So now when a Manager opens the form to approve it, the GetUserProfileByName data connection will not run again since it is only set to run on Form load when the Leave Status is equal to New.

Tip: For this to work correctly, you need to be sure to also create rules to keep your Leave Status up to date. In other words, on the submit button, change the status to Submitted, then on the Approval button, set the status to Approved, etc.

About Jessica Criner