Home > InfoPath2010 > Populate people picker with current user in InfoPath2010

Populate people picker with current user in InfoPath2010

In recent project, we have a requirement to populate people picker with current user in InfoPath2010 form. It is quite easy to do it in InfoPath; however it does not quite work as we have expected. In the end, we have to use a combination of InfoPath and list event receiver to complete this task. Here are the steps to set the current user to the additional stakeholder display name in InfoPath.

1. Add a set a field’s value rule to InfoPath Form load event.

2. Set the condition as below

3. Configure the set field value action as below, userName() is a built-in function in InfoPath2010

4. Publish the form, you will see the current user is populated with the current user name as below, however when you click the save, the user does not get saved. I guess this is due to the fact the people-picker control needs to perform a post-back to do some tricks.But we could not force the control to do it in InfoPath2010 for the automatically populated user , therefore we have to write up an list event receiver to set the current user.

5. In you SharePoint solution, add event receiver.


6. Copy the code to as specified to the screenshot

       public override void ItemAdding(SPItemEventProperties properties)
        {
            //base.ItemAdding(properties);

            string keyStakeholders = "Key_x0020_Stakeholders";
                // this has to be the static or internal name , not the display name

            string seperator = ";#";

            SPUser user = properties.Web.CurrentUser;

            if (string.IsNullOrEmpty(SPHelper.SafeToString(properties.AfterProperties[keyStakeholders])))
            {
                properties.AfterProperties[keyStakeholders] = seperator + user.ID + seperator + user.Name;
            }

            else
            {
                SPFieldUserValue fieldValue = SPHelper.GetSPFieldUserValue(SPContext.Current.Web,
                                                                           SPHelper.SafeToString(
                                                                               properties.AfterProperties[
                                                                                   keyStakeholders]));

                properties.AfterProperties[keyStakeholders] = seperator + fieldValue.LookupId + seperator +
                                                              fieldValue.LookupValue + seperator + user.ID + seperator +
                                                              user.Name;
            }
        }

7. Go to the elements.xml under the new event receiver


8. Change the xml highlighted in yellow

From:


TO


 9. Deploy your solution; it will work like a charm. 🙂

References:

How to: Create an Event Receiver for a Specific List Instance


Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: