Thank you for visiting my site!

Home / REST API/jQuery / Get Current User Information and Profile Properties Through REST API

Get Current User Information and Profile Properties Through REST API

Get the logged in user information


$(document).ready(function() {

//**********************************************************************
// get the user information from "UserProfiles.PeopleManager/GetMyProperties"
//**********************************************************************
    var UserName ="";
    var FirstName = "";
    var LastName = "";
    var Department = "";
    var workEmail = "";

    $.ajax({ 
        url: _spPageContextInfo.webAbsoluteUrl + "/_api/SP.UserProfiles.PeopleManager/GetMyProperties", 

        headers: { Accept: "application/json;odata=verbose" }, 
        success: function (data) { 
            try { 
                //Get properties from user profile Json response 
                userDisplayName = data.d.DisplayName; 
                AccountName = data.d.AccountName;
                console.log(" AccountName: " + AccountName );

                var indexOfSlash =  AccountName.indexOf('\\') + 1;
                Domain = AccountName.slice(0, indexOfSlash - 1);
                console.log(" Domain: " + Domain );
                var properties = data.d.UserProfileProperties.results; 

                for (var i = 0; i < properties.length; i++) { 
                var property = properties[i]; 

                if (property.Key == "WorkEmail") { 
                   workEmail = property.Value; } 
                if (property.Key == "EmployeeID") { 
                  EmployeeID = property.Value; } 
                if (property.Key == "UserName") { 
                  UserName = property.Value; } 
                if (property.Key == "Department") { 
                  Department = property.Value; } 
                if (property.Key == "FirstName") { 
                  FirstName = property.Value; } 
                if (property.Key == "LastName") { 
                  LastName = property.Value; } 
                } 

                console.log(" AccountName: " + AccountName + " Domain: " + Domain + " Department: " + Department + " workEmail: "+ workEmail); 

$("input[title='First Name Required Field']").val(FirstName); 
$("input[title='First Name Required Field']").attr("readonly", "true"); 
$("input[title='Last Name Required Field']").val(LastName); 
$("input[title='Last Name Required Field']").attr("readonly", "true"); 
$("input[title='Organization']").val(Department); 
$("input[title='Organization']").attr("readonly", "true"); 
$("input[title='Domain Required Field']").val(Domain); 
$("input[title='Domain Required Field']").attr("readonly", "true"); 
$("input[title='VA ID Required Field']").val(UserName); 
$("input[title='VA ID Required Field']").attr("readonly", "true"); 
$("input[title='Email Address Required Field']").val(workEmail); 
$("input[title='Email Address Required Field']").attr("readonly", "true"); 
$("input[title='AccountID']").val(UserName); } 

               catch (err2) { 
                 console.log(JSON.stringify(err2)); 
               } }, 
               error: function (jQxhr, errorCode, errorThrown) { 
                 console.log(errorThrown);
               } 
}); 
//********************************************************************** 
// Set the value of dropdown with the querystring value 
//********************************************************************** 
//Set the values for HiddenCapability out of object qObj.HiddenCapability=6 
//Set the value of Source var setAccessLevel = getQueryParameters().HiddenCapability; 

var setSource = getQueryParameters().Source; 
console.log('setAccessLevel ' + setAccessLevel); 
console.log('setSource ' + setSource); 

$("select[title='Access to capability']").val(setAccessLevel); 
$("select[title='Request Status']").val(4); 

function getQueryParameters(){ qObj = {}; 
  var urlSearch = window.location.search; 
  //console.log(urlSearch); 

  if(urlSearch.length>0){
    var qpart = urlSearch.substring(1).split('&');
    console.log('qpart ' + qpart);

    $.each(qpart,function(i,item){
    var splitAgain = item.split('=');
    console.log('splitAgain ' + splitAgain);
    qObj[splitAgain[0]] = splitAgain[1];
    });
  }
  //console.log(qObj.HiddenCapability);
  console.log('qObj ' + qObj);
  return qObj;
  //return qObj.HiddenCapability;
  }  

//**********************************************************************
//  Hide Submit Button After Six PM
//**********************************************************************

    $("#showSaveRow").show();
    $("#hideSaveRow").hide();

    hideSubmitButtonAfterSix();

    function hideSubmitButtonAfterSix() {
        var currentTime = new Date();
        var hours = currentTime.getHours();
        var minutes = currentTime.getMinutes();
        var t =currentTime.getHours()  + ":" + currentTime.getMinutes();
         //alert(t);
        var newButton = document.getElementById("submit");
        //Disabling submit button evening 6 PM to 6 AM.
        if(t >= '18:0' && t <= '6:0') { 
          console.log("hide " + t); 
          $("#showSaveRow").hide();
          $("#hideSaveRow").show();
        } else { 
        console.log("Show " + t); 
        $("#showSaveRow").show(); 
        $("#hideSaveRow").hide();
        } 
    } 

//**********************************************************************
//  Custom cancel button - cancel takes you from list to another location
//**********************************************************************
    // Get the custom cancel button
    //<input name="cancelButton" type="button" value="Cancel" id="cancelButton" />

    $("#cancelButton").click(function(e){

        //history.go(-1);
        window.location = "https://site"

    });
});


ADD YOUR COMMENT