Ah, my love affair with the contact selector continues J. Ever wanted to pull a specific user's details into your InfoPath form? It's pretty easy to get the current user's details. But try to get another user's details and it becomes pretty difficult. All of the solutions I have read up until now have involved code (and I don't like to code).
Well, recently I was doing some reading and I stumbled across this great blog by Clayton Cobb. It shows how you can get any details (department, office, manager etc) for any user you like and pull the data into an InfoPath form. How awesome!! I thought I'd extend Clayton's idea and incorporate the contact selector into the mix.
To get the most out of this post, you need to understand a couple of key concepts:
I would suggest reading and doing the steps in these blogs first before you attempt to go any further.
Ok, here we go.
I'm assuming that you have read the above 2 blogs and therefore should have the following already set up in your InfoPath form:
For this post, I'm going to get the user's manager and the user's department. I have set up my form so it looks like this:
And the main data source looks like this:
GET THE EMPLOYEE'S USER NAME WITHOUT THE DOMAIN
We are going to be passing the employee's user name into the 'GetUserProfileByName' method to extract the relevant department and manager. For this to work we need to extract the employee's user name, without the domain, from the 'AccountId' field in the contact selector.
Double click on the 'EmployeeUserName' field to open the field's properties
Click on 'fx' next to the 'Value' field.
Select the 'substring-after' function
Set up your formula to look like this:
This is going to take everything after the "\" in the account ID field. So for example, if I enter my name in the contact selector the account ID will look something like this "MYDOMAIN\ahelbig". By using the substring-after value we are left with 'ahelbig'.
Click Ok and the Ok again.
Try previewing your form at this point and enter a name in the contact selector. You should see something similar to this:
GET THE DEPARTMENT
Now we can get into the fun stuff..... We will begin by extracting the department of the employee. All I'm doing here is using the steps in Clayton's blog to achieve this. Clayton has done such a great job of describing the why's so I'm not going to do that here. If you need more information, jump over into Clayton's blog.
Double click on the 'EmployeeUserName' field in the main data source to open the field's properties
Click on 'Rules and Merge' and then click 'Add'
Give the rule a name such as 'Get Employee Info'
No condition is required
Click 'Add Action'
Leave the action as 'Set a field's value'
Click on the button next to the 'field' field
In the data source drop down select the GetUserProfileByName Data source
Expand out the groups and select the 'AccountName' field
Click on Ok
Click the 'fx' button next to the 'Value' field
Click on 'Insert field or group' and then select the 'EmployeeUserName' field
Click on ok and then ok again
Your action should look like this:
In the Action drop down box, select 'Query using a data connection'
In the 'data connection' field select the 'GetUserProfileByName' data source
It should look like this:
Click on Ok to save the action
Click on the button next to the 'field' field and select the 'Department' field
Click on 'fx' next to the 'Value' field. Now this is where understanding Itay Shakury's blog comes in handy.
Insert a field or group
Click on the data source drop down and select the 'GetUserProfileByName' data source
Expand out the groups and select the 'Value' field.
Click on 'Filter Data'
Click on 'Add'
In the first drop down box select 'Select a field or group'
Select the 'Name' field from the 'GetUserProfileByName' data source
Leave the condition as 'Is equal to'
In the last drop down box enter 'Department'
Your filter condition should look like this:
Click on Ok until you are back at the 'action' window. Your action should look like this:
You should have 3 actions that look like this:
Note: It's very important that the actions appear in the order as shown
Click ok 3 times to save the rule.
Now is a good time to preview or publish your form and check that everything is working. Go ahead and enter a name into the Contact Selector and see the 'Department' field auto populate.
If you're anything like me, it was about this point in the process that I started to get really excited....
GET THE MANAGER'S NAME
If you have managed to get this far and everything is working ok, then these next few steps will be pretty straight forward. Basically, we are repeating what we have already done for department except this time we are going to get the manager.
Open the 'Get Employee Info' rule you created on the 'EmployeeUserName' field
Click on 'Add Action' again
Click on the button next to the 'field' field and select the 'Manager' field
In the last drop down box enter 'Manager'
Click on Ok to save the action.You should have 4 actions that look like this:
Note: Once again, it's very important that the first 2 actions appear in order as shown - the other actions can be in any order
Click ok 2 times to save the rule
Test the form to make sure that the manager's user name is populating.
Now at this point, you really are done. Most people just want to know the manager for the purposes of some kind of workflow. For example, you may want to route this form to the employee's manager for approval. Technically, you have the manager's user name, which is enough and can be used to assign tasks and send emails to the manager using SharePoint Designer workflow or some other workflow tool. Personally, if my client wants the manager's name to be displayed on the form, or perhaps in email notifications, I don't like just having the manager's user name. I think it looks ugly. I would much rather have the manager's full name displayed nicely on the form and just use the manager's user name for the purpose of sending email notifications with workflow. Clayton's post shows very nicely how to achieve this by using the 'PreferredName' so definitely worth a look if you haven't already.
I know this was a long post but hopefully you have the idea. I've only shown you how to extract 2 pieces of information about the employee. Of course, you can use this method to extract any other bits of information as you need. Refer to the long list of user information that Itay Shakury's provides in his blog.
Some final notes:
Stay tuned for my final post which talks you through how to validate the name in the contact selector.