cancel
Showing results for 
Search instead for 
Did you mean: 
milan_stojadino
Nintex Newbie

SharePoint Online (Forms): Get UserProfile Properties with REST API Batching

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 = ["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(accountN...'" + 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('\r\n');

    }

    //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('\r\n');

    }

    function parseResponse(batchResponse) {

        //Extract the results back from the BatchResponse

        var results = NWF$.grep(batchResponse.split("\r\n"), 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

0 Kudos
Reply
2 Replies
Crack_Jack
Nintex Newbie

Re: SharePoint Online (Forms): Get UserProfile Properties with REST API Batching

Did you get the answer?
0 Kudos
Reply
Community Manager Community Manager
Community Manager

Re: SharePoint Online (Forms): Get UserProfile Properties with REST API Batching

@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.

0 Kudos
Reply