Quantcast
Channel: SharePoint 2013 - Development and Programming forum
Viewing all articles
Browse latest Browse all 7589

REST API no results for getUserById() method

$
0
0

Hi Below code always go to error handler.

Could any one help with alternative for JSOM code.

// Variables that we will use in callbacks
var context;
var listItem;

$(document).ready(function () {
    emptyContact("Loading Owner Details...");
    getContactFromList();
});

// Get the value from the Contact column in the Pages list
function getContactFromList() {
    context = SP.ClientContext.get_current();
    listItem = context.get_web().get_lists().getByTitle('Pages').getItemById(_spPageContextInfo.pageItemId);
    context.load(listItem, 'PublishingContact');
    context.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceededc, Function.createDelegate(this, this.onQueryFailedc)));
}

// Delegate method to handle successful async call to load Contact column info.
function onQuerySucceededc(contactColumn) {
    var contact = listItem.get_item('PublishingContact');
    if (contact) {
        getAccountName(contact);
    }
    else {
        emptyContact("Could not retrieve Contact for this page.");
    }
}
// Delegate method to handle failure of async call to load Contact column info.
function onQueryFailedc(sender, args) {
    emptyContact("Could not retrieve Contact for this page.");
}

// Load User Profile information based on the account name of the supplied page Contact
function getAccountName(contact) {
var username = contact.get_lookupValue();
    jQuery.ajax({
        url: _spPageContextInfo.webAbsoluteUrl + "/_api/Web/GetUserById(" + contact.$2Z_1 + ")",
        type: "GET",
        headers: {
            Accept: "application/json; odata=verbose"
        },
        success: displayProfileInformation,
        error: handleAccountNameError
    });
}

function handleAccountNameError(request, textStatus, error) {
    emptyContact("Could not request User Profile for Contact.");
}

// Read and display the detailed User Profile information for supplied Account
function displayProfileInformation(ajaxResponse) {

    var accountName = ajaxResponse.d.LoginName;

    // prepare peoplemanager and profile properties request
    var peopleMgr = new SP.UserProfiles.PeopleManager(context);
    context.load(peopleMgr);
    var profileProperties = peopleMgr.getPropertiesFor(accountName);
    context.load(profileProperties);

    // execute the command against SharePoint
    context.executeQueryAsync(
        // success callback
        function () {

            // contact picture
            $('.q_OwnerImage').children().remove();
            var myPicture = profileProperties.get_pictureUrl();
            if (myPicture == null) {
                myPicture = "";
            }
            var myPictureHTML = '<img src="/_layouts/15/userphoto.aspx?url=' + myPicture + '" alt="Contact"/>';
            var personProperties = profileProperties.get_userProfileProperties();
            $('.q_OwnerImage').append(myPictureHTML);

            alert(personProperties.FirstName);

        },
        // Error handler callback
        handleProfileLoadError);
}

function handleProfileLoadError(request, textStatus, error) {
    emptyContact("Could not load detailed Profile for Contact.");
}

It does not return result when function getAccountName(contact) called. The Ajax didn't respond.

I tried in URL like this

https://mysite/_api/Web/GetUserById(5)

and got below error

<?xml version="1.0" encoding="UTF-8"?>
-<m:error xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata"><m:code>-2146232832, Microsoft.SharePoint.SPException</m:code><m:message xml:lang="en-US">User cannot be found.</m:message></m:error>

I referred below two links

https://www.nothingbutsharepoint.com/sites/devwiki/articles/Pages/How-to-Get-Login-Name-and-Display-Name-using-SharePoint-2013-REST-API.aspx

http://vrdmn.blogspot.com.au/2013/02/sharepoint-2013-working-with-user.html


Murali Krishna Dittakavi *********************** If my answer/post resolved your query could you mark the post as answered. If it is helpful then vote as Helpful. Thanks


Viewing all articles
Browse latest Browse all 7589

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>