0

Is there any web part that display info about current logged user, like name, position, phone?

I tried to export Profile details webpart from MySite (site for user profiles) and imported to homepage of main site, but it doesn't work (show nothing).

2

You can put below code to script editor webpart to show current logged in user's details.

<script type="text/javascript">
ExecuteOrDelayUntilScriptLoaded(init,'sp.js');
var currentUser;
function init(){
    this.clientContext = new SP.ClientContext.get_current();
    this.oWeb = clientContext.get_web();
    currentUser = this.oWeb.get_currentUser();
    this.clientContext.load(currentUser);
    this.clientContext.executeQueryAsync(Function.createDelegate(this,this.onQuerySucceeded), Function.createDelegate(this,this.onQueryFailed));
}

function onQuerySucceeded() {
    document.getElementById('userLoginName').innerHTML = currentUser.get_loginName(); 
    document.getElementById('userId').innerHTML = currentUser.get_id();
    document.getElementById('userTitle').innerHTML = currentUser.get_title();
    document.getElementById('userEmail').innerHTML = currentUser.get_email();
}

function onQueryFailed(sender, args) {
    alert('Request failed. \nError: ' + args.get_message() + '\nStackTrace: ' + args.get_stackTrace());
}
</script>
<div>Current Logged User:
    <span id="userLoginName"></span>
    <span id="userId"></span>
    <span id="userTitle"></span>
    <span id="userEmail"></span>
</div>

OR you can use below code which uses SPService

<script src="/Scripts/jquery-1.10.2.min.js" type="text/javascript"></script>  
<script src="/Scripts/jquery.SPServices-2014.02.min.js" type="text/javascript"></script>  
<script language="javascript" type="text/javascript" src="/Scripts/jquery.SPServices-0.7.2.min.js"></script>

<script language="javascript" type="text/javascript" src="/Scripts/sputility.min.js"></script>


<script type="text/javascript">  

$(document).ready(function() {  
    // get the title of current user  
    var UserName= $().SPServices.SPGetCurrentUser({  
    fieldName: "Title",  
   debug: false  
});  
// get department of current user
var userDepartment = $().SPServices.SPGetCurrentUser({
   fieldName: "Department"
   });
     // alert(userDepartment);

// get Phone of current user
var userPhone = $().SPServices.SPGetCurrentUser({
   fieldName: "WorkPhone"
   });
      alert(userPhone);


//alert("Hi");
//populate the field ‘Requestor’ with the current account  
$('textarea[title="Requestor"]').val(UserName);  
$('input[title="Requestor"]').val(UserName);  

$('textarea[title="Department"]').val(userDepartment); 
$('input[title="Department"]').val(userDepartment); 


$('textarea[title="Phone Number"]').val(userPhone); 
$('input[title="Phone Number"]').val(userPhone);

});  

</script>

To check the internal names of fields, kindly check below URL.

$().SPServices.SPGetCurrentUser

Note :-

This script gets the few details of current user you can get the more details by using appropriate variables.

  • @Roman, Is that helps you? – Hardik Jun 7 '16 at 14:07
  • 1st part code works awesome , 1 req, i have a list with a status column so according to open/close i need to display text , how will i write that in the above code ? @Hardik – OM-ॐ Mar 15 '18 at 11:51
0

You could use InfoPath forms where you can use SharePoint web services to retrieve info from the loggedin user.

Check this link for the SharePoint Web Services

Publish InfoPath form to SharePoint and then add to your page the custom infopath form control (custom web part) you have just published.

0

Use Rest to get current logged in user details as below

$(document).ready(function () {

    var requestUri = _spPageContextInfo.webAbsoluteUrl + "/_api/sp.userprofiles.peoplemanager/getmyproperties";

    $.ajax({
    url : requestUri,
    contentType : "application/json;odata=verbose",
    headers : { "accept" : "application/json;odata=verbose" },
    success : onsuccess,
    error : onerror
    });
    });

    function onsuccess(data){
    document.getElementById('CurrentUser').innerHTML=data.d.DisplayName; 
    console.log(data);     
    }
    function onerror(errorMessage){
    //alert('Error');
    }
0

Using below code you will be able to find User Name, Login ID, User ID, Email ID of the current logged in user

$(document).ready(function () {
SP.SOD.executeFunc('sp.js', 'SP.ClientContext', LoadCurrentUser);
});


function LoadCurrentUser() {
    var context = SP.ClientContext.get_current();
    var siteColl = context.get_site();
    var web = siteColl.get_rootWeb();
    this._currentUser = web.get_currentUser(); 
    context.load(this._currentUser);
    context.executeQueryAsync(Function.createDelegate(this, GetCurrentUserProperties), Function.createDelegate(this, LoadUserfailed));
}

function LoadUserfailed() {
    alert('failed');
}

function GetCurrentUserProperties()
{
    var loginName = this._currentUser.get_loginName();
    var email = this._currentUser.get_email();
    var userid = this._currentUser.get_id();
    var username = this._currentUser.get_title();
}

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.