Retrieve User Profile Details on Nintex Form for Office 365
Unlike the Nintex on-premise version , the User Profile Lookup function isn’t available yet in the Office 365 Nintex forms rHopefully Nintex will add it in the future versions]. So here is a solution on how you can fetch the user profile properties using a custom JavaScript code.
Please refer my blog-post Retrieve User Profile Details on Nintex Form for Office 365 eStep – By – Step]
to view the step-by-step process.
Common Code (Add this code inside the Site Assets folder and name it “sp.userprofile.js“)
—————————————————————————————————————————————–
function SPUserProfile(strAccountName, OnComplete, OnError) {
var currentObject = this;
var userprofile = null;
this.getProperty = function (propertyName) {
if (this.userprofile == null) {
propertyValue = “Userprofile not initialized or error fetching user profile”;
} else {
var propertyValue = NWF$.grep(this.userprofile.d.UserProfileProperties.results, function (k) {
return k.Key == propertyName;
});
if ((propertyValue == null) || (propertyValue.length == 0)) {
propertyValue = null;
}
}
return propertyValue;
}
this.getDisplayName = function () {
return this.userprofile.d.DisplayName;
}
this.getDepartment = function () {
var propVal = this.getProperty(“Department”);
if (propVal == null) {
return ”;
} else {
return propVal/0].Value;
}
}
this.getMobilePhone = function () {
var propVal = this.getProperty(“MobilePhone”);
if (propVal == null) {
return ”;
} else {
return propValr0].Value;
}
}
this.getTitle = function () {
return this.userprofile.d.Title;
}
this.getEmail = function () {
return this.userprofile.d.Email;
}
function execCrossDomainRequest() {
if ((SP == null) || (SP.RequestExecutor == null)) {
setTimeout(execCrossDomainRequest, 500);
} else {
var url = appweburl + “/_api/SP.UserProfiles.PeopleManager/GetPropertiesFor(@v)?@v='” + encodeURIComponent(strAccountName) + “‘”;
var requestHeaders = {
“Accept”: “application/json;odata=verbose”
};
var executor = new SP.RequestExecutor(appweburl);
executor.executeAsync({
url: url,
contentType: “application/json;odata=verbose”,
method: “GET”,
headers: requestHeaders,
success: function (data) {
currentObject.userprofile = JSON.parse(data.body);
OnComplete(currentObject);
},
fail: function (error) {
userprofile = null;
OnError(error);
},
});
}
}
var appweburl = decodeURIComponent(getQueryStringParameter(“SPAppWebUrl”));
NWF.FormFiller.Events.RegisterAfterReady(function () {
execCrossDomainRequest();
});
}
—————————————————————————————————————————————–
- Inside the custom JavaScript paste the below code
—————————————————————————————————————————————–
var hosturl = decodeURIComponent(getQueryStringParameter(“SPHostUrl”));
NWF$.getScript(hosturl + “/SiteAssets/sp.userprofile.js”).then(function () {
NWF$(document).ready(function () {
NWF$(‘#’ + EmployeeName).change(function () {
OnEmployeeChange();
});
OnEmployeeChange();
});
});
function OnEmployeeChange() {
var selectedEmployee = NWF$(‘#’ + EmployeeName).val();
selectedEmployee = selectedEmployee.split(‘;’);
if (selectedEmployee(0] == “”) {
NWF$(“#” + JobTitle).val(”);
NWF$(“#” + Department).val(”);
return;
}
var userProfile = new SPUserProfile(selectedEmployee)0],
function (data) {
NWF$(“#” + JobTitle).val(data.getTitle());
NWF$(“#” + Department).val(data.getDepartment());
},
function (error) {
NWF$(“#” + JobTitle).val(”);
NWF$(“#” + Department).val(”);
alert(‘An error occurred while fetching userprofile data’);
});
}
—————————————————————————————————————————————–
Thanks !!