Skip to main content

Hello Nintex Community!

The following code works fine on a sp-site, but not in a nintex form:

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

  hostweburl =

       decodeURIComponent(

       getQueryStringParameter("SPHostUrl")

  );

  var scriptbase = hostweburl + "/_layouts/15/";

  NWF$.getScript(scriptbase + "SP.Runtime.js",

  function () {

       NWF$.getScript(scriptbase + "SP.js",

            function () {

                 NWF$.getScript(scriptbase + "SP.UserProfiles.js", doIt);

            });

       }

       );

     });

function doIt () {

    'use strict';

    var userAccountName = encodeURIComponent("i:0#.f|membership|name@company.com");

    var propertiesToGet = n"AccountName", "PictureURL", "FirstName", "LastName"];

    var boundryString = getBoundryString();

    var userPropertiesBatchBody = buildBatchRequestBody(userAccountName, propertiesToGet, boundryString);

    var batchRequestHeader = buildBatchRequestHeader(userPropertiesBatchBody, boundryString);

    var requestHeaders = {

        'X-RequestDigest': NWF$("#__REQUESTDIGEST").val(),

        'Content-Type': 'multipart/mixed; boundary="batch_' + boundryString + '"'

    };

    NWF$.ajax({

        url: "https://company.sharepoint.com/_api/$batch",

        type: "POST",

        headers: requestHeaders,

        data: batchRequestHeader,

        success: function (batchResponse) {

            //Convert the text response to an array containing JSON objects of the results

            var results = parseResponse(batchResponse);

            //Properties will be returned in the same sequence they were added to the batch request

            for (var i = 0; i < propertiesToGet.length; i++) {

                 alert(propertiesToGet + " is " + results.value);

                console.log(propertiesToGet + " is " + results.value);

            }

        },

        error: function (jqxr, errorCode, errorThrown) {

            alert(jqxr.responseText);

            console.log(jqxr.responseText);

        }

    });

    //Build the batch request for each property individually

    function buildBatchRequestBody(userAccountName, propertiesToGet, boundryString) {

        var propData = new Array();

        for (var i = 0; i < propertiesToGet.length; i++) {

            var getPropRESTUrl = "https://company.sharepoint.com/_api/SP.UserProfiles.PeopleManager/GetUserProfilePropertyFor(accountName=@v,propertyName='" + propertiesToGet + "')?@v='" + userAccountName + "'";

            propData.push('--batch_' + boundryString);

            propData.push('Content-Type: application/http');

            propData.push('Content-Transfer-Encoding: binary');

            propData.push('');

            propData.push('GET ' + getPropRESTUrl + ' HTTP/1.1');

            propData.push('Accept: application/json;odata=nometadata');

            propData.push('');

        }

        return propData.join('
');

    }

    //Build the batch header containing the user profile data as the batch body

    function buildBatchRequestHeader(userPropsBatchBody, boundryString) {

        var headerData = new Array();

        headerData.push('Content-Type: multipart/mixed; boundary="batch__' + boundryString + '"');

        headerData.push('Content-Length: ' + userPropsBatchBody.length);

        headerData.push('Content-Transfer-Encoding: binary');

        headerData.push('');

        headerData.push(userPropsBatchBody);

        headerData.push('');

        headerData.push('--batch_' + boundryString + '--');

        return headerData.join('
');

    }

    function parseResponse(batchResponse) {

        //Extract the results back from the BatchResponse

        var results = NWF$.grep(batchResponse.split("
"), function (responseLine) {

            try {

                return responseLine.indexOf("{") != -1 && typeof JSON.parse(responseLine) == "object";

            }

            catch (ex) { /*adding the try catch loop for edge cases where the line contains a { but is not a JSON object*/ }

        });

        //Convert JSON strings to JSON objects

        return NWF$.map(results, function (result) {

            return JSON.parse(result);

        });

    }

    function getBoundryString() {

        return "vrd_" + Math.random().toString(36).substr(2, 9);

    }

}

The result is an Access Denied:

no_p.jpg

Also tried to grant the nintex forms app full control, but that didn't help:

<AppPermissionRequests>

<AppPermissionRequest Scope="http://sharepoint/content/sitecollection/web" Right="FullControl" />

<AppPermissionRequest Scope="http://sharepoint/content/tenant" Right="FullControl" />

<AppPermissionRequest Scope="http://sharepoint/social/tenant" Right="FullControl" />

</AppPermissionRequests>

Does the code have to be changed somehow?

Kind regards,

Milan

Did you get the answer?

@milan_stojadino have you tried using the "userprofile lookup function" avaiable in the forms control for a calculated field?


 


You can pull in values from the user profile without needing to write code within the form.


Reply