Pass user information from SharePoint 2013 to InfoPath 2013

InfoPath is a common tool used to create forms hosted on SharePoint. Due to the limited development options available for Office 365, I think this places an increased focus on InfoPath when it comes to creating a solution. Unfortunately, I found that it was relatively difficult to get the current user’s information to be displayed or saved within an Infopath form.

Typically, within the form you would just make a web service call to the UserProfileService.asmx in order to retrieve the current user. Unfortunately, I was unable to get this working when the form was deployed to an Office 365 site. [1] This KB article suggests this is because the Office 365 servers have loopback protection enabled.

My solution involves the following:

  • Office 365 SharePoint 2013 site
  • InfoPath 2013 Form with one  parameter made available to use in a web part connection
  • InfoPath Form Web Part
  • Script Editor Web Part
  • Query String (URL) Filter Web Part

Step 1: Create your InfoPath form (or cheat and download the one I’ve created User Information Infopath form). I created textboxes for each of the User fields as well as one additional one that will be used to pass information into the form.infopath-formI created rules on the txtUrlParameter field to populate my other fields. This is because the InfoPath form web part can only accept one web part connection at a time so we have to cram all our parameters into one URL parameter. I decided to use a format like:

0[i:0#.f|membership|[email protected]]1[11]2[Kit Menke]3[[email protected]]

This allows me to keep my rules relatively simple inside the InfoPath form:

infopath-rule-details

Step 2: Publish the form to your Office 365 site. I created a new Forms library for testing. Make sure the Url Parameter field available to use in a web part connection.

infopath-publishingwizard-parameters

Step 3: I edited my web part page and added on my three web parts:

  1. Forms -> InfoPath Form Web Part
  2. Media and Content -> Query String (URL) Filter Web Part
  3. Filters -> Script Editor Web Part

Note: The “Current User Filter” web part might catch your eye. Unfortunately, this web part passes a user in the format: i:0#.f|membership|[email protected] Not what we need inside an InfoPath form.

Step 4: Configure the InfoPath Form Web Part to point to the form you published in step 2.

Step 5: Configure the Query String (URL) Filter Web Part to use a URL Parameter called “userinfo”.

Step 6: Connect it to the InfoPath Form Web Part so that the userinfo parameter will populate the Url Parameter field in your InfoPath form.

Step 7: Add the following script inside your Script Editor Web Part (modified the code I found http://sharepoint.stackexchange.com/a/73037/2070). The script looks up the current user using SharePoint’s Client Object Model and creates a dynamic link.  You would put the Script editor web part on another page (but for demo purposes, it works fine on the same page too). You will want to modify the HREF of the link to point the page you want.

<script type="text/javascript">
function CustomExecuteFunction() {
    var self = this;
    self.context = new SP.ClientContext.get_current();
    self.web = context.get_web();
    self.currentUser = web.get_currentUser();
    context.load(self.currentUser);
    
    self.asyncSuccess = function(sender, args) {
        var user = this.currentUser;
        console.log('asyncSuccess', user);
        document.getElementById('userLoginName').innerHTML = user.get_loginName(); 
        document.getElementById('userId').innerHTML = user.get_id();
        document.getElementById('userTitle').innerHTML = user.get_title();
        document.getElementById('userEmail').innerHTML = user.get_email();
        
        var url = '?userinfo=';
        url += '0[' + encodeURIComponent(user.get_loginName()) + ']';
        url += '1[' + encodeURIComponent(user.get_id()) + ']';
        url += '2[' + encodeURIComponent(user.get_title()) + ']';
        url += '3[' + encodeURIComponent(user.get_email()) + ']';
        document.getElementById('customButton').href += url;
    };
    
    self.asyncFailure = function(sender, args) {
        alert('Request failed. \nError: ' + args.get_message() + '\nStackTrace: ' + args.get_stackTrace());
    };
    
    // actually fires off the AJAX request
    // more info: http://msdn.microsoft.com/en-us/library/dn168907.aspx
    context.executeQueryAsync(
        Function.createDelegate(self,self.asyncSuccess), 
        Function.createDelegate(self,self.asyncSuccess)
    );
}

ExecuteOrDelayUntilScriptLoaded(CustomExecuteFunction,'sp.js');
</script>
<div>Current Logged User:
    <span id="userLoginName"></span>
    <span id="userId"></span>
    <span id="userTitle"></span>
    <span id="userEmail"></span>
    <a id="customButton" href="https://example.sharepoint.com/SitePages/Test.aspx">Click me!</a>
</div>

Here is the completed InfoPath web part with pre-populated data from the current user!

infopath-form-webpart

Download the InfoPath form I’ve created: UserInformation.zip.