AnsweredAssumed Answered

REST updates no longer working – Nintex Forms for Office 365 (v. 1.2.3.0)

Question asked by patrickabel Champion on Sep 16, 2016
Latest reply on Mar 2, 2017 by patrickabel

I have recently upgraded to the latest version of Nintex Forms for Office 365 (1.2.3.0). After doing so, I am encountering a 403 access denied error when performing REST calls to create or update SharePoint list items from within my form.

 

I was able to bypass the 403 error by forcing SP.RequestExecutor.js to be loaded (contradictory to best practices : Nintex Forms O365 - Getting SharePoint ClientContext Do's and Dont's), but with this success comes some other issues within the form itself like form controls (lookups) not loading properly.

 

I believe I can state with certainty that this error I'm seeing spawns from recent updates on the product. Before fully upgrading, I had the same Nintex Form and embedded code running on two different tenants. The tenant with the latest update (v. 1.2.3.0) was failing whereas the previous version was working fine.

 

Perhaps the development pattern for interacting with Client Context has changed with the latest code base? I've attached a sample form and embedded JavaScript sample. This sample:

  1. Demonstrates the retrieval of SharePoint list data through REST (written to console) – working
  2. Demonstrates my previously working approaching for creating SharePoint list items using REST – not working
    1. initiates from a JavaScript button on the form itself for testing purposes.
  3. Dependency : the above are wired up to GET/POST data to a list named TEST 

 

Example of error:

 

Embedded JavaScript Code:

var web;
var hostweburl;
var appweburl;
var pollSP;


NWF.FormFiller.Events.RegisterAfterReady(function () {
    pollSP = setInterval(checkSPLoad, 500);
});


function checkSPLoad() {
    if (clientContext) {
        window.clearInterval(pollSP);
        sharePointReady();
    }
}


function sharePointReady() {
    NWF$().ready(function () {

        hostweburl = decodeURIComponent(Utils.getQueryStringParameter('SPHostUrl'));
        appweburl = decodeURIComponent(Utils.getQueryStringParameter('SPAppWebUrl'));

        Data.getSomethingWithREST();

    });
}


var Data = {
    getSomethingWithREST: function () {
        var context;
        var factory;
        var appContextSite;
        var listName = "TEST"; // SharePoint list dependancy


        context = new SP.ClientContext(appweburl);
        factory = new SP.ProxyWebRequestExecutorFactory(appweburl);
        context.set_webRequestExecutorFactory(factory);
        appContextSite = new SP.AppContextSite(context, hostweburl);
        var executor = new SP.RequestExecutor(appweburl);


        executor.executeAsync({
            url: appweburl + "/_api/SP.AppContextSite(@target)/web/lists/getbytitle('" + listName + "')/items?&$orderby=Title&@target='" + encodeURIComponent(hostweburl) + "'",
            method: "GET",
            headers: {
                "Accept": "application/json; odata=verbose"
            },
            success: function (data) {
                var obj = JSON.parse(data.body);
                // log to console for demonstration purposes
                console.log('REST query results:');
                console.log(obj.d.results);
            },
            error: function (err) {
                alert(JSON.stringify(err));
            },
        });
    },
    createListItemWithREST: function () {
        var context;
        var factory;
        var appContextSite;
        var listName = "TEST"; // SharePoint list dependancy
        var listTitle = Utils.getRandomString();

        context = new SP.ClientContext(appweburl);
        factory = new SP.ProxyWebRequestExecutorFactory(appweburl);
        context.set_webRequestExecutorFactory(factory);
        appContextSite = new SP.AppContextSite(context, hostweburl);
        var executor = new SP.RequestExecutor(appweburl);

        // metadata to update.
        var item = {
            "__metadata": { "type": Utils.getItemTypeForListName(listName) },
            "Title": listTitle,
        };

        var requestBody = JSON.stringify(item);
        var requestHeaders = {
            "accept": "application/json;odata=verbose",
            "X-RequestDigest": NWF$("#__REQUESTDIGEST").val(),
            "X-HTTP-Method": "PATCH",
            "content-type": "application/json;odata=verbose",
            "If-Match": "*"
        }

        executor.executeAsync({
            url: appweburl + "/_api/SP.AppContextSite(@target)/web/lists/getbytitle('" + listName + "')/items?@target='" + encodeURIComponent(hostweburl) + "'",
            method: "POST",
            contentType: "application/json;odata=verbose",
            headers: requestHeaders,
            body: requestBody,
            success: function () {
                console.log("update success.");
            },
            error: function (err) {
                alert(JSON.stringify(err));
            },
        });
    },
};


var Utils = {
    getQueryStringParameter: function (param) {
        var params = document.URL.split('?')[1].split('&');
        var strParams = '';
        for (var i = 0; i < params.length; i = i + 1) {
            var singleParam = params[i].split('=');
            if (singleParam[0] == param) {
                return singleParam[1];
            }
        }
    },
    getItemTypeForListName: function (str) {
        return "SP.Data." + str.charAt(0).toUpperCase() + str.slice(1) + "ListItem";
    },
    getRandomString: function () {
        var text = "";
        var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

        for (var i = 0; i < 5; i++)
            text += possible.charAt(Math.floor(Math.random() * possible.length));

        return text;
    }
};

 

Any help would be greatly appreciated!

 

Thanks,

Patrick

Outcomes