Solved

Access SharePoint Lists REST API from Nintex for Office 365 forms.

  • 18 October 2016
  • 2 replies
  • 308 views

Badge +4

Ladies and Gentlemen,

 

I have quite a bit of custom Javascript in my on-premise Nintex forms that makes calls the SharePoint lists REST API.  As we migrate these sites and forms to Office 365 I suspect that these scripts will need to be rewritten to include an OAUTH token or converted to use JSOM as I can just use the client context.

 

Do any of you have sample code were you connect to SharePoint Online lists REST API from Javascript in a Nintex Office 365 form?

 

Thanks!

 

Chris

icon

Best answer by cclements 1 November 2016, 15:57

View original

2 replies

Badge +4

I was able to answer my own question.  

As Nintex Forms for Office 365 is a SharePoint provider hosted add in (application) you must refactor your REST calls to utilize the SharePoint cross domain libraries.  I created a code sample below that loads the appropriate SharePoint libraries in the proper order.  It then demonstrates two calls to the Nintex Forms for Office 365 Host Web.  The first call fetches the name of the site that hosts the Nintex Form.  The second calls on the lists and libraries API of the Host Web to fetch an item from the "Tanks" list where the title is "Sherman".

This code can be referenced in your Form Settings -> Advanced -> Custom Javascript Includes.   You may find that Nintex already loads the SharePoint Sp.Runtime.JS library. The snippet is intended to be extra cautious; ensuring everything is in place before execution begins.

Pay special attention to the URL of the REST calls.  You must specify the host target as well as include the app context switch call "SP.AppContextSite(@target)"  to gain access to the host web's API.

var hostweburl = decodeURIComponent(getQueryStringParameter("SPHostUrl"));
var appweburl = decodeURIComponent(getQueryStringParameter("SPAppWebUrl"));
var scriptbase = hostweburl + "/_layouts/15/";

NWF$.getScript(scriptbase + "MicrosoftAjax.js").then(function (data) {
return NWF$.getScript(scriptbase + "SP.Runtime.js");
}).then(function (data) {
return NWF$.getScript(scriptbase + "SP.js");
}).then(function (data) {
return NWF$.getScript(scriptbase + "SP.RequestExecutor.js");
}).then(function (data) {
var executor = new SP.RequestExecutor(appweburl);

/* This will get the title of the host web */
executor.executeAsync({
url: appweburl + "/_api/SP.AppContextSite(@target)/web/title/?@target='" + hostweburl + "'",
method: "GET",
headers: { "Accept": "application/json; odata=verbose" },
success: function (data) {
var siteName = JSON.parse(data.body).d.Title;
//alert(siteName);
},
error: function (e) {
//alert('Error getting site name.');
}
});

/* This will query a list on the host web and filter it by the title */
var listname = 'Tanks';
var fields = '&$select=Title,Country,Name,Designation';
var filters = '&$filter=Title eq 'Sherman'';
var url = appweburl + "/_api/SP.AppContextSite(@target)/web/lists/getbytitle('" + listname + "')/items?@target='" + hostweburl + "'" + fields + filters;

executor.executeAsync({
url: url,
method: "GET",
headers: { "Accept": "application/json; odata=verbose" },
success: function (data) {
var tank = JSON.parse(data.body).d.results[0];
//alert(tank.Country + " " + tank.Designation + " " + tank.Name);
},
error: function (e) {
//alert('Error getting tank.');
}
});

/* Utility function to get host and app URL from the query string */

function getQueryStringParameter(paramToRetrieve) {
var params =
document.URL.split("?")[1].split("&");
var strParams = "";
for (var i = 0; i < params.length; i = i + 1) {
var singleParam = params.split("=");
if (singleParam[0] == paramToRetrieve)
return singleParam[1];
}
}

Badge +4
Any idea how can we use SP.UserProfiles.PeopleManager/GetMyProperties to user profile, /web properties are working fine.

Reply