Using the SharePoint 2013 REST user resources API in InfoPath 2013


I recently did a DB attach migration from SharePoint 2007 to 2010 to 2013 for a customer, which requires many steps to account for all the changes in SP along the way. One of the changes that I chose to make was to replace some of the older SOAP web services calls, like GetUserProfileByName, in InfoPath forms converted from 2007 to 2013. I started with the approach outlined in John Liu’s post here. John’s post takes care of the on-the-fly changes often required to REST URLs to use them for a specific query without resorting to a whole set of Data Connections for each required query. The other necessary information is in the SharePoint REST API spec, specifically here for the User resource specifications.

Essentially the URL for this query takes the form of

http://<web app URL>/_api/siteusers(@v)?@v='i%3A0%23.w%7Cdomain\userid

For example, for an on-premise 2013 installation, the URL might look something like this:

http://portal.microsoft.com/_api/siteusers(@v)?@v='i%3A0%23.w%7Cmicrosoft\rramirez'

Invoking this query will result in a ATOM-XML feed returning that can be used in InfoPath to select particular fields of interest. Below is an example of such an XML document.

 <?xml version="1.0" encoding="utf-8" ?> 
 <entry xml:base="http://portal.microsoft.com/_api/" xmlns="http://www.w3.org/2005/Atom" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns:georss="http://www.georss.org/georss" xmlns:gml="http://www.opengis.net/gml">
 <id>http://portal.tcfc.pvt/_api/Web/GetUserById(1209)</id> 
 <category term="SP.User" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" /> 
 <link rel="edit" href="Web/GetUserById(1209)" /> 
 <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Groups" type="application/atom+xml;type=feed" title="Groups" href="Web/GetUserById(1209)/Groups" /> 
 <title /> 
 <updated>2014-07-03T20:12:56Z</updated> 
 <author>
 <name /> 
 </author>
 <content type="application/xml">
 <m:properties>
 <d:Id m:type="Edm.Int32">1209</d:Id> 
 <d:IsHiddenInUI m:type="Edm.Boolean">false</d:IsHiddenInUI> 
 <d:LoginName>i:0#.w|tcfc\rramirez</d:LoginName> 
 <d:Title>Russ Ramirez</d:Title> 
 <d:PrincipalType m:type="Edm.Int32">1</d:PrincipalType> 
 <d:Email /> 
 <d:IsSiteAdmin m:type="Edm.Boolean">true</d:IsSiteAdmin> 
 <d:UserId m:type="SP.UserIdInfo">
 <d:NameId>s-1-5-21-341662082-687677503-1835691634-17717</d:NameId> 
 <d:NameIdIssuer>urn:office:idp:activedirectory</d:NameIdIssuer> 
 </d:UserId>
 </m:properties>
 </content>
 </entry>

The data of interest in one particular case was the value within the <Title> element; this is equivalent to the PreferredName field returned using the GetUserProfileByName() web method. Within InfoPath then, you just need to do the following:

  • Define a Data Connection to a simple REST URL like http://portal.microsoft.com/_api/Web/CurrentUser
  • Create a Form Load rule that will
    1. do a Change REST URL Action
    2. do a Query of the REST data connection (it will use the new URL just modified)
    3. set the value of the field of interest; e.g. a Prepared By field

The XPath for the Change REST URL action of step 1. will look like this for our example:

concat("http://portal.microsoft.com/_api/web/siteusers(@v)?@v='i%3A0%23.w%7Cmicrosoft\", my:UserId, "'")

The escaped characters in the variable @v are i:0#.w| which you should recognize, and the my:UserId is a field in InfoPath initialized to substring-after(xdUser:get-UserName(), “\”) so the user id part (rramirez in this example) is stored for use by the modified REST URL. If you try this URL from a browser (turn off Feed view in IE), you will get the XML document above with your pertinent information. Step 3. above, setting the Prepared By field, uses the value of the Title element as I mentioned. Here’s what the REST service schema looks like in InfoPath 2013: RESTschema Once you have done all of this, you should see your user’s preferred name/title looking like this: prepared by field

Advertisements

About generation12

I am a SharePoint/.NET consultant in the twin cities.
This entry was posted in GetUserProfileByName, InfoPath 2013, REST, SharePoint 2013. Bookmark the permalink.

Leave a Reply

Please log in using one of these methods to post your comment:

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