none
Get Current User DirectReport by UserID Sharepoint 2013 RRS feed

  • Question

  • Greetings All,

    I have NewForm in Sharepoint 2013 issue list, and i have already populate the employee name and job title using current userid. However there is another fields named " supervisor , i want to fetch it by Direct Report to this employee

    Employee Name already populated 

    Supervisor : still!

    How it can be achieved by Java Script Web part. 

    here is the code that i used to populate different fields such as " Name , job title ...etc"

     
    script src="http://code.jquery.com/jquery-latest.js" type="text/javascript"></script>
    <script type="text/javascript" src="/_layouts/15/clientpeoplepicker.js"></script>
    <script type="text/javascript">  
    
    $(document).ready(function () 
    {  var userOneId=_spPageContextInfo.userId;
    var userTwoId=_spPageContextInfo.userId;
    
                    GetUserLogin(userOneId,"Name");   
        GetUserLogin(userTwoId,"Job Title");  
    });
    
    
    function disablePP()
    {
    
    $(".sp-peoplepicker-editorInput").attr('disabled' , 'disabled');
    $(".sp-peoplepicker-delImage").css({ 'display' : 'none'});
    $("div.sp-peoplepicker-topLevel[title='peoplepicker']").addClass("sp-peoplepicker-topLevelDisabled");
    }
      
    var loginName;
    function GetUserLogin(userid,ColumnName) {  
    var requestUri = _spPageContextInfo.webAbsoluteUrl + "/_api/web/getuserbyid(" + userid + ")";  
      
    var requestHeaders = { "accept" : "application/json;odata=verbose" };  
      
    $.ajax({  
      url : requestUri,  
      contentType : "application/json;odata=verbose",  
      headers : requestHeaders,  
      success : function(data,request){
      console.log(data.d);
       var loginName =  data.d.LoginName;
        window.setTimeout(function() {
          SetAndResolvePeoplePicker(ColumnName, loginName);
          }, 500);    
        
        setTimeout(disablePP, 600);
      },  
      error : QueryError  
    });  
    }
    
      
    function QueryError(error)
    {  
      alert(error);  
    }  
    function SetAndResolvePeoplePicker(fieldName,Name) 
    {
    
                                    var controlName = fieldName;
                                    var peoplePickerDiv = $("[id$='ClientPeoplePicker'][title='" + controlName + "']");
                        var peoplePickerEditor = peoplePickerDiv.find(".sp-peoplepicker-editorInput");
                                    var spPeoplePicker = SPClientPeoplePicker.SPClientPeoplePickerDict[peoplePickerDiv[0].id];
                                    peoplePickerEditor.val(Name);
                                    spPeoplePicker.AddUnresolvedUserFromEditor(true);  
    }
    </script>

    Appreciated.

    Saturday, April 18, 2020 5:24 PM

All replies

  • Hi Majedix,

    You could use this endpoint for current user’ /_api/SP.UserProfiles.PeopleManager/GetMyProperties?$select=DirectReports’.

    Full test code :

    <script src="http://code.jquery.com/jquery-latest.js" type="text/javascript"></script>
    <script>
    $(function(){
    getCurrentDirectReports('supervisor')
    })
    function getCurrentDirectReports(ColumnName){
    var requestHeaders = { "accept" : "application/json;odata=verbose" };  
                    $.ajax({  
      url : _spPageContextInfo.webAbsoluteUrl+"/_api/SP.UserProfiles.PeopleManager/GetMyProperties?$select=DirectReports",  
      contentType : "application/json;odata=verbose",  
      headers : requestHeaders, 
      success : function(data,request){
                                    for(var i=0;i<data.d.DirectReports.results.length;i++){
                                    
                                    loginName=data.d.DirectReports.results[i];
          SetAndResolvePeoplePicker(ColumnName, loginName);
         
                     
                                    }
                                    disablePP()
                                    
                                    console.log(data.d.DirectReports.results)
       
      },  
      error : QueryError  
    });
    
    function QueryError(error)
    {  
      alert(error);  
    }  
    function disablePP()
    {
    
    $(".sp-peoplepicker-editorInput").attr('disabled' , 'disabled');
    $(".sp-peoplepicker-delImage").css({ 'display' : 'none'});
    $("div.sp-peoplepicker-topLevel[title='peoplepicker']").addClass("sp-peoplepicker-topLevelDisabled");
    }
    function SetAndResolvePeoplePicker(fieldName,Name) 
    {
    
                                    var controlName = fieldName;
                                    var peoplePickerDiv = $("[id$='ClientPeoplePicker'][title='" + controlName + "']");
                        var peoplePickerEditor = peoplePickerDiv.find(".sp-peoplepicker-editorInput");
                                    var spPeoplePicker = SPClientPeoplePicker.SPClientPeoplePickerDict[peoplePickerDiv[0].id];
                                    peoplePickerEditor.val(Name);
                                    spPeoplePicker.AddUnresolvedUserFromEditor(true);  
    }
    }
    </script>
    

    If you want to get other people’s direct reports,you could use ‘/_api/SP.UserProfiles.PeopleManager/GetPropertiesFor(accountName=@v)?@v='contoso\administrator'&$select=DirectReports’;

    Best regards,

    Amos



    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    Monday, April 20, 2020 6:47 AM
  • Hi Majedix,

    You could use this endpoint for current user’ /_api/SP.UserProfiles.PeopleManager/GetMyProperties?$select=DirectReports’.

    Full test code :

    <script src="http://code.jquery.com/jquery-latest.js" type="text/javascript"></script>
    <script>
    $(function(){
    getCurrentDirectReports('supervisor')
    })
    function getCurrentDirectReports(ColumnName){
    var requestHeaders = { "accept" : "application/json;odata=verbose" };  
                    $.ajax({  
      url : _spPageContextInfo.webAbsoluteUrl+"/_api/SP.UserProfiles.PeopleManager/GetMyProperties?$select=DirectReports",  
      contentType : "application/json;odata=verbose",  
      headers : requestHeaders, 
      success : function(data,request){
                                    for(var i=0;i<data.d.DirectReports.results.length;i++){
                                    
                                    loginName=data.d.DirectReports.results[i];
          SetAndResolvePeoplePicker(ColumnName, loginName);
         
                     
                                    }
                                    disablePP()
                                    
                                    console.log(data.d.DirectReports.results)
       
      },  
      error : QueryError  
    });
    
    function QueryError(error)
    {  
      alert(error);  
    }  
    function disablePP()
    {
    
    $(".sp-peoplepicker-editorInput").attr('disabled' , 'disabled');
    $(".sp-peoplepicker-delImage").css({ 'display' : 'none'});
    $("div.sp-peoplepicker-topLevel[title='peoplepicker']").addClass("sp-peoplepicker-topLevelDisabled");
    }
    function SetAndResolvePeoplePicker(fieldName,Name) 
    {
    
                                    var controlName = fieldName;
                                    var peoplePickerDiv = $("[id$='ClientPeoplePicker'][title='" + controlName + "']");
                        var peoplePickerEditor = peoplePickerDiv.find(".sp-peoplepicker-editorInput");
                                    var spPeoplePicker = SPClientPeoplePicker.SPClientPeoplePickerDict[peoplePickerDiv[0].id];
                                    peoplePickerEditor.val(Name);
                                    spPeoplePicker.AddUnresolvedUserFromEditor(true);  
    }
    }
    </script>

    If you want to get other people’s direct reports,you could use ‘/_api/SP.UserProfiles.PeopleManager/GetPropertiesFor(accountName=@v)?@v='contoso\administrator'&$select=DirectReports’;

    Best regards,

    Amos



    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

        Greetings Amos , Thank you for your reply. I've added the script but not worked. The supervisor field not fetched by the name of direct report. 

    Thanks for usual support. 

    Monday, April 20, 2020 9:17 AM
  • Hi,

    Are there any errors on the page?

    Have you maintained users’ Direct Reports?

    Best regards,

    Amos


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    Monday, April 20, 2020 9:41 AM
  • Hi,

    Are there any errors on the page?

    Have you maintained users’ Direct Reports?

    Best regards,

    Amos


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

          Thank you Amos,  see the below i want to get (Report To):


    • Edited by Majedix Monday, April 20, 2020 10:19 AM
    Monday, April 20, 2020 10:08 AM
  • Hi,

    Because our windows server versions are different, our ‘reqorts to’ display different, in fact, ‘reports to’ and ‘manager’ are the same meaning. You can check whether the value of manager and reports to are equal in the user profile

    These attributes can all get by this endpoint ‘/_api/SP.UserProfiles.PeopleManager/GetMyProperties’.

    Updated code(update rest api and success function):

    <script src="http://code.jquery.com/jquery-latest.js" type="text/javascript"></script>
    <script>
    $(function(){
    getCurrentDirectReports('supervisor');
    })
    function getCurrentDirectReports(ColumnName){
    var requestHeaders = { "accept" : "application/json;odata=verbose" };  
                    $.ajax({  
      url : _spPageContextInfo.webAbsoluteUrl+"/_api/SP.UserProfiles.PeopleManager/GetMyProperties",  
      contentType : "application/json;odata=verbose",  
      headers : requestHeaders, 
      success : function(data,request){
          SetAndResolvePeoplePicker(ColumnName, data.d.UserProfileProperties.results[15].Value);
                                    disablePP();
      },  
      error : QueryError  
    });
    function QueryError(error)
    {  
      alert(error);  
    }  
    function disablePP()
    {
    $(".sp-peoplepicker-editorInput").attr('disabled' , 'disabled');
    $(".sp-peoplepicker-delImage").css({ 'display' : 'none'});
    $("div.sp-peoplepicker-topLevel[title='peoplepicker']").addClass("sp-peoplepicker-topLevelDisabled");
    }
    function SetAndResolvePeoplePicker(fieldName,Name) 
    {
                                    var controlName = fieldName;
                                    var peoplePickerDiv = $("[id$='ClientPeoplePicker'][title='" + controlName + "']");
                        var peoplePickerEditor = peoplePickerDiv.find(".sp-peoplepicker-editorInput");
                                    var spPeoplePicker = SPClientPeoplePicker.SPClientPeoplePickerDict[peoplePickerDiv[0].id];
                                    peoplePickerEditor.val(Name);
                                    spPeoplePicker.AddUnresolvedUserFromEditor(true);  
    }
    }
    </script>
    

    Best regards,

    Amos


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    Wednesday, April 22, 2020 2:40 AM
  • Thank you Amos for the reply,  I tried to check from my browser IE11 

     https://mysite/_api/SP.UserProfiles.PeopleManager/GetMyProperties/

    but actually not worked! 

    Can we change the script code (DirectReport) to ( ReportTo) or (Manager) ?

    Appreiated yoru contiunes help.

    Wednesday, April 22, 2020 7:45 AM
  • Hi,

    Have you configured Mysite in your farm?

    https://docs.microsoft.com/en-us/sharepoint/install/configure-my-sites

    Steps to solve the problem:1.open the developer tool to find out if the code reports errors 2.If there is error in code,you could add a breakpoint at the errored code ,when the debug hits breakpoint, you could check the code where goes wrong. If there is no error in code ,you could debug the code and check if the value which we want to use is right or exist.

    Whether it is DirectReport, ReportTo, or Manager, which value we need, we use the corresponding value in the code.

    If you need more help, please provided more information.

    Best regards,

    Amos


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    Wednesday, April 22, 2020 9:03 AM
  • Hi,

    Thanks Amos, i've checked the code with debugger and no error found. It seems that no value exist for Manager or ReportTo in Sharepoint (might be restricted). i'm end user i don't have permission to see some of functions. However, when click on Name form Sharepoint list data it shows user details such as Job Title, Department and its connected with outlook profiles also which contains ReportTo as i snap it for you above, is that mean we can Get Report To or Manager on some how?

    Thanks for continues help. 

    Wednesday, April 22, 2020 11:46 PM
  • Hi Majedix,

    You could check the return value of the ajax request, especially  ‘data.d.UserProfileProperties.results[15].Value’.You need to find the ‘manager’ or ‘reports to’ key in this array.

    Here is a part values of my request.

    You could share your full return value for further research.

    Best regards,

    Amos


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    Thursday, April 23, 2020 8:26 AM
  • Hi Amos. 

    i couldn't find the data.d.UserProfileProperties from the debugger from (F12).

    thanks. 

    Monday, April 27, 2020 11:15 PM
  • Hi,

    data.d.UserProfileProperties is the variable name I put in watch watch window when I debug.

    You’d better check whether you have configured mysite in your farm.The rest api need a mysite configured farm.

    Best regards,

    Amos


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    Wednesday, April 29, 2020 9:13 AM
  • Hi Amos,

    see the snapshot, when i debug the new form page , 

    Thursday, April 30, 2020 2:07 AM
  • Hi,

    You can enter the rest api into the browser(Chrome) address bar directly to check whether there is a return value.

    Tip: use your site webAbsoluteUrl.

    Best regards,

    Amos


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    Tuesday, May 5, 2020 8:44 AM
  • Thanks Amos for always help.

    in fact chrome not installed. and only EI can work to my station.

    Monday, June 29, 2020 10:35 PM