Sunday, December 27, 2015

Get logged in user info using SharePoint REST call


Get Current user info from SharePoint Userprofile using Ajax REST call

(function ($) {
    $(document).ready(function () {
        // Ensure that the SP.js file is loaded before the custom code runs.
        SP.SOD.executeOrDelayUntilScriptLoaded(loadUserData, 'SP.js');
    });

    function loadUserData() {    

        jQuery.ajax({
            url: _spPageContextInfo.webAbsoluteUrl + "/_api/SP.UserProfiles.PeopleManager/GetMyProperties",
            type: "GET",
            headers: { Accept: "application/json;odata=verbose" },
            success: function (data) {
                if (data.d.DisplayName != null)
                    var curDisplayName = data.d.DisplayName;
                var curEmail = data.d.Email;
                empEmail = curEmail;
                if (data.d.UserProfileProperties != null) {
                    $.each(data.d.UserProfileProperties.results, function (index, value) {
                        if (value.Key == "Office") {
                            empLocation = data.d.UserProfileProperties.results[index].Value;                          
                        }
                    });
                }              

            },
            error: function (jqxr, errorCode, errorThrown) {
                alert("Error: " + args.get_message());
            }
        });
    }


})(jQuery);

No comments:

Post a Comment