0

I have the following code

function execCrossDomainRequest() {

var executor = new SP.RequestExecutor(appweburl);


executor.executeAsync({
    url: appweburl + "/_api/Web/GetUserById(9)",
    method: "GET",
    headers: { "Accept": "application/json; odata=verbose" },
    success: successHandler,
    error: errorHandler
});


}

function successHandler(data) {
var jsonObject = JSON.parse(data.body);
var announcementsHTML = "";
var results = jsonObject.d.results;


 var jsonObject = JSON.parse(data.body);
var announcementsHTML = "";
var results = jsonObject.d.results;

for (var i = 0; i < results.length; i++) {
    announcementsHTML = announcementsHTML +
        "<p><h1>" + results.Title +
        "</h1>" + results.Email +
        "</p><hr>";

}



document.getElementById("renderAnnouncements").innerHTML = announcementsHTML;

}

Which should give me the user details back out of SP but instead i keep getting an error "Uncaught TypeError: Cannot read property 'length' of undefined "

1

You should use this way to create a RequestExecutor:

function execCrossDomainRequest() {
    var executor;
    executor = new SP.RequestExecutor(appweburl);
    executor.executeAsync(
        {
            url:
                appweburl +
                "/_api/SP.AppContextSite(@target)/web/GetUserById(1)?@target='" +
                hostweburl + "'",
            method: "GET",
            headers: { "Accept": "application/json; odata=verbose" },
            success: successHandler,
            error: errorHandler
        }
    );
}

function successHandler(data) {
    var jsonObject = JSON.parse(data.body);

    document.getElementById("userId").innerHTML =
        "<b>" + jsonObject.d.Title + "</b>";
}

I got my user name by this way.

0

Try this code

                function successHandler(data) {
                var jsonOBJ2 = $.parseJSON(data.d);
                $.each(jsonOBJ2, function (i, item) {

                });

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.