Whilst working on client project recently that involved InfoPath within Office365 I immediately wanted to use a commonly employed technique with previous projects – using the GetUserProfileByName web-service to obtain the users first and last name.
There are a number benefits to doing this, especially when the project inevitably involves workflow, custom library views, webparts etc to have a personalized experience for the user. For this specific project we needed to get the user of the form within the document as they were to form official standalone documents (as in out of the SharePoint document library context).
Unfortunately there is a BIG limitation…
“This issue occurs because loopback protection is enabled in the SharePoint Online environment. Loopback protection must be disabled for InfoPath forms to be able to connect to a SharePoint Online web service.
Any time that you make a call to the same server from an InfoPath form, the requests to loop back. This works only when loopback protection is disabled. For security reasons, loopback protection is always enabled in SharePoint Online. This is a known limitation of InfoPath forms in the Office 365 SharePoint Online environment, and there is no workaround for this issue.
Here is the article for your reference: http://support.microsoft.com/kb/2674193“
So we need another way to get the user data from within Office 365 and into the InfoPath form and I devised the following solution:
The overview is as follows
- an InfoPath webpart
- a query string filter connected to the InfoPath webpart
- a javascript file to add the query string parameter
Now I had to provision a page with the Infopath webpart and a few other connected webparts
I then needed to get the InfoPath form to react to a query string filter webpart
Passing Parameters to Infopath –
Next I added some JavaScript via a script editor webpart to read the current user name from the user profile service view CSOM
I used some of the script from Kit Menke’s blog here http://kitmenke.com/blog/2013/10/31/pass-user-information-from-sharepoint-2013-to-infopath-2013/
I simplified his script to just read the current user via “get_title()” and then pass this to the “window. location”
This redirects to a url with a query string parameter “CurrentUser” with this value
…aspx?CurrentUser=Username
making sure to redirect only if the string “CurrentUser” is missing from the url. See the “check” statement…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
loc = window.location.href; | |
check = loc.indexOf('CurrentUser'); | |
if(check <0) | |
{ | |
// if not the redirect with appended query string | |
window.location= loc + "?CurrentUser=" + user.get_title(); | |
} | |
}; | |
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'); |
So I now had the current user in the url and the query string filter webpart reading the value and passing this to the InfoPath form field – I then had a people picker field read this value and bingo I could interact as if the original query was there.
Hope this helps someone else.
Thanks
Ryan
Brilliant solution Ryan….thanks for sharing
LikeLike
Hi Ryan
you mention that you interact as if the original query was there, does that mean being able look up Office and Department etc from their name?
Thanks
Jamie
LikeLike
Yes you can look up that information, and more.
Check out the following link for a JS object model example
Get a property from the userProfileProperties property of the PersonProperties object.
This link is the dictionary of user profile properties available…
Gets user profile properties for the user.
LikeLike