kkgan

JavaScript to get List Items and User Profiles from Nintex Forms for Office 365

Blog Post created by kkgan Employee on Dec 28, 2016

Sharing two pieces of JavaScript getting List Item(s) and User Profiles from Nintex Forms for Office 365. The two JavaScript was tested based on Nintex Forms for Office 365, App Version: NFO 1.2.3.0. Please take note that this code is tested only based on the above mentioned version of Nintex Forms, it might not be optimized in term of performance or simplicity.

 

 

Calling Sharepoint Online REST API to query host web's custom list from within Nintex Forms for Office 365 (i.e. App web).

var pollSP;  
NWF.FormFiller.Events.RegisterAfterReady(function (){ 
    pollSP = setInterval(checkSPLoad, 500); 
}); 
     
function checkSPLoad(){ 
    if (clientContext){ 
        window.clearInterval(pollSP); 
          var hostweburl = decodeURIComponent(getQueryStringParameter("SPHostUrl"));
          var appweburl = decodeURIComponent(getQueryStringParameter("SPAppWebUrl")); 
          var layoutsPath = "/_layouts/15/"; 
          var scriptbase = appweburl + layoutsPath; 
 
          NWF$.getScript(scriptbase + "SP.js",
               function () {
                              NWF$.getScript(scriptbase+ "SP.RequestExecutor.js", execCrossDomainRequest);
               }
          );
    } 
 
     function execCrossDomainRequest(){
          var deferred = NWF$.Deferred();
          var url = appweburl + "/_api/SP.AppContextSite(@target)/web/lists/GetByTitle('MyExpense')/Items?$select=ID,Title&@target='" + hostweburl + "'";
          var executor = new SP.RequestExecutor(appweburl);
          executor.executeAsync(
               {
                    url: url,
                    method: "GET",
                    headers: {"accept": "application/json;odata=verbose"},
                    success: function (data) {
                         var jsonobj = JSON.parse(data.body);
                         var results = jsonobj.d.results;
                         deferred.resolve(data);
                         var msg = "";
                         for (var i = 0; i < results.length; i++){
                              msg = msg + results[i].Title + ";";
                         }
                         alert(msg);
                    },
                    error: function (data) {
                         deferred.reject(data);
                         alert("data");
                    }
               }
          );
     }
}

 

Querying current user's profile from Nintex Forms for Office 365. 

NWF.FormFiller.Events.RegisterAfterReady(function (){  
     var clientContext;
     var hostweburl = decodeURIComponent(getQueryStringParameter("SPHostUrl"));
     var appweburl = decodeURIComponent(getQueryStringParameter("SPAppWebUrl")); 
     var layoutsPath = "/_layouts/15/"; 
     var scriptbase = appweburl + layoutsPath; 
 
     NWF$.getScript(scriptbase + "SP.js").then(function(){
          return NWF$.getScript(scriptbase + "SP.RequestExecutor.js");
     }).then(function(){
          return NWF$.getScript(scriptbase + "init.js");
     }).then(function(){
          return NWF$.getScript(scriptbase + "SP.UserProfiles.js");
     }).then(execCrossDomainRequest);
 
     function execCrossDomainRequest(){
          var deferred = NWF$.Deferred();
          
          var con = SP.ClientContext.get_current();
          clientContext = new SP.ClientContext(appweburl);
          var factory = new SP.ProxyWebRequestExecutorFactory(appweburl);
          clientContext.set_webRequestExecutorFactory(factory);
          
          var hostWeb = new SP.AppContextSite(clientContext, hostweburl).get_web();
          var appWeb = new SP.AppContextSite(clientContext, appweburl).get_web();
          
          var url = appweburl + "/_api/SP.UserProfiles.PeopleManager/GetMyProperties";
     
          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) {
                         var msg = "";
                         var parsedData = JSON.parse(data.body);
                         var properties = parsedData.d.UserProfileProperties.results;
                         for (var i = 0; i < properties.length; i++){
                              var property = properties[i];
                              msg = msg + property.Key + ":" + property.Value + ",";
                         }
                         deferred.resolve(parsedData);
                         alert(msg);
                    },
                    error: function (data) {
                         deferred.reject(data);
                         alert("failed");
                    }
               }
          );
     }

});

Outcomes