none
Setting manager people picker of current user RRS feed

  • Question

  • I've found some great code that will automatically set the current user for a people picker from here: http://www.sharepointcolumn.com/sp2013-setting-people-picker-value-in-newform-aspx/

    <script src="http://site/SiteAssets/jquery.min.js"></script> 
    <script type="text/javascript">
    $(document).ready(function () {
    var userid = _spPageContextInfo.userId;
    //alert(userid)
    function GetCurrentUser() {
    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 : onSuccess,
      error : onError
    });
    }
    function onSuccess(data, request){
     var userName = data.d.Title;
     //set following "field" as column to set in people picker
     SetUserFieldValue("Name",userName);
    }
    
    function onError(error) {
      //alert(error);
    }
    function SetUserFieldValue(fieldName, userName) {
     
    var controlName = fieldName;
     
    var peoplePickerDiv = $("[id$='ClientPeoplePicker'][title='" + controlName + "']");
     
    var peoplePickerEditor = peoplePickerDiv.find("[title='" + controlName + "']");
     
    var spPeoplePicker = SPClientPeoplePicker.SPClientPeoplePickerDict[peoplePickerDiv[0].id];
     
    //comment out this field if edit form needs to be read only 
    peoplePickerEditor.val(userName);
    //comment out this field if edit form needs to be read only  
    spPeoplePicker.AddUnresolvedUserFromEditor(true);
     
    //disable the field
     
    spPeoplePicker.SetEnabledState(false);
     
    //hide the delete/remove use image from the people picker
     
    $('.sp-peoplepicker-delImage').css('display','none');
     
    }
    
    
    GetCurrentUser();
    });
    </script>

    and I've found some more great code that will find the current users manager here:

    http://www.mikesel.info/get-current-sharepoint-user-manager-details-using-javascript/

    <script src="//ajax.aspnetcdn.com/ajax/jQuery/jquery-1.9.0.min.js"></script>
    <script src="/_layouts/15/SP.Runtime.js"></script>
    <script src="/_layouts/15/SP.js"></script>
    <script src="/_layouts/15/SP.UserProfiles.js"></script>
    <script type="text/javascript">
    'use strict';
    
    var clientContext = new SP.ClientContext.get_current();
    var peopleManager = new SP.UserProfiles.PeopleManager(clientContext);
    var userProfileProperties = peopleManager.getMyProperties()
    var managerProfileProperties = [];
    
    clientContext.load(userProfileProperties);
    clientContext.executeQueryAsync(GetManagerObject, failure);
    
    function GetManagerObject() {
        var profilePropertyNames = ["Manager"];
        var targetUser = userProfileProperties.get_accountName();
        var userProfilePropertiesForUser = new SP.UserProfiles.UserProfilePropertiesForUser(clientContext, targetUser, profilePropertyNames);
    
        managerProfileProperties = peopleManager.getUserProfilePropertiesFor(userProfilePropertiesForUser);
    
        clientContext.load(userProfilePropertiesForUser);
        clientContext.executeQueryAsync(GetManagerDetails, failure);
    };
    
    
    function GetManagerDetails() {
        var profilePropertyNames = ["PreferredName", "WorkPhone", "Title"];
        var targetUser = managerProfileProperties[0];
        var userProfilePropertiesForUser = new SP.UserProfiles.UserProfilePropertiesForUser(clientContext, targetUser, profilePropertyNames);
    
        managerProfileProperties = peopleManager.getUserProfilePropertiesFor(userProfilePropertiesForUser);
    
        clientContext.load(userProfilePropertiesForUser);
        clientContext.executeQueryAsync(showDetails, failure);  
    }
    
    function showDetails() {
       // alert(managerProfileProperties[0] + " " + alert(managerProfileProperties[1] + " " + alert(managerProfileProperties[3])));
    var mgrPP = (managerProfileProperties[0]);
    alert(mgrPP);
    //alert(managerProfileProperties[1]);
    //alert(managerProfileProperties[3]);
    }
    function failure() {
        alert("Failed");
    };

    I have a list with Name and Supervisor columns that are people picker fields in Sharepoint 2013

    Now what I would like to do is to combine the code so I can automatically place the current user in a column labeled "Name" (top snipet does this) and then fill in a column labeled "Supervisor" with managers name (this is where I need help adding the bottom snippet.) and disable the supervisor field and remove the image from people picker, like the top snippet does.

    If you have not figured this out I am not a developer...

    Thanks for the help.

    Monday, November 4, 2013 8:38 PM

Answers

  • var managerName;

    //Add new Function with the
    funciton GetCurrenUserManagerObject(){

    var clientContext = new SP.ClientContext.get_current();
    var peopleManager = new SP.UserProfiles.PeopleManager(clientContext);
    var userProfileProperties = peopleManager.getMyProperties()
    var managerProfileProperties = [];

    clientContext.load(userProfileProperties);
    clientContext.executeQueryAsync(GetManagerObject, failure);

    }


    //Modify ShowDetails
    function showDetails() {
      
    managerName = (managerProfileProperties[0]);

    }

    GetCurrenUserManagerObject();

    SetUserFieldValue(fieldName, managerName); 


    Of course you need bevor your code from above.

    Chears
    Martin

    • Edited by Martin Kazakov Tuesday, November 5, 2013 7:52 PM
    • Marked as answer by Davidatek Thursday, November 7, 2013 10:15 PM
    Tuesday, November 5, 2013 7:51 PM

All replies

  • You could try to use SharePoint Designer workflow to retrieve the manager details of the current user and set that in a field. Refer to the following post for more information

    https://www.nothingbutsharepoint.com/sites/eusp/Pages/How-to-retrieve-Users-Manager-in-SharePoint-Designer-Workflows.aspx


    --Cheers

    Tuesday, November 5, 2013 10:23 AM
  • Yes, I can lookup via SharePoint workflow, but I would rather do this in code, if possible.
    Tuesday, November 5, 2013 1:16 PM
  • var managerName;

    //Add new Function with the
    funciton GetCurrenUserManagerObject(){

    var clientContext = new SP.ClientContext.get_current();
    var peopleManager = new SP.UserProfiles.PeopleManager(clientContext);
    var userProfileProperties = peopleManager.getMyProperties()
    var managerProfileProperties = [];

    clientContext.load(userProfileProperties);
    clientContext.executeQueryAsync(GetManagerObject, failure);

    }


    //Modify ShowDetails
    function showDetails() {
      
    managerName = (managerProfileProperties[0]);

    }

    GetCurrenUserManagerObject();

    SetUserFieldValue(fieldName, managerName); 


    Of course you need bevor your code from above.

    Chears
    Martin

    • Edited by Martin Kazakov Tuesday, November 5, 2013 7:52 PM
    • Marked as answer by Davidatek Thursday, November 7, 2013 10:15 PM
    Tuesday, November 5, 2013 7:51 PM
  • Thanks, I will try it out as soon as I can.
    Wednesday, November 6, 2013 1:19 PM
  • Martin,

    Thanks for the help! With your sample I was able to get it working with the script below.

    I assume that the code could be cleaned up a bit...

    <script src="http://...../SiteAssets/jquery.min.js"></script> 
    <script src="/_layouts/15/SP.Runtime.js"></script>
    <script src="/_layouts/15/SP.js"></script>
    <script src="/_layouts/15/SP.UserProfiles.js"></script>
    <script type="text/javascript">
    $(document).ready(function () {
    var userid = _spPageContextInfo.userId;
    //alert(userid)
    function GetCurrentUser() {
    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 : onSuccess,
      error : onError
    });
    }
    function onSuccess(data, request){
     var userName = data.d.Title;
     //set following "field" as column to set in people picker
     SetUserFieldValue("Name",userName);
    
    }
    
    function onError(error) {
      //alert(error);
    }
    function SetUserFieldValue(fieldName, userName) {
     
    var controlName = fieldName;
     
    var peoplePickerDiv = $("[id$='ClientPeoplePicker'][title='" + controlName + "']");
     
    var peoplePickerEditor = peoplePickerDiv.find("[title='" + controlName + "']");
     
    var spPeoplePicker = SPClientPeoplePicker.SPClientPeoplePickerDict[peoplePickerDiv[0].id];
     
    //comment out this field if edit form needs to be read only 
    peoplePickerEditor.val(userName);
    //comment out this field if edit form needs to be read only  
    spPeoplePicker.AddUnresolvedUserFromEditor(true);
     
    //disable the field
     
    spPeoplePicker.SetEnabledState(false);
     
    //hide the delete/remove use image from the people picker
     
    $('.sp-peoplepicker-delImage').css('display','none');
     
    }
    //this code to to find the mgr of current user
    'use strict';
    
    var clientContext = new SP.ClientContext.get_current();
    var peopleManager = new SP.UserProfiles.PeopleManager(clientContext);
    var userProfileProperties = peopleManager.getMyProperties()
    var managerProfileProperties = [];
    
    clientContext.load(userProfileProperties);
    clientContext.executeQueryAsync(GetManagerObject, failure);
    
    function GetManagerObject() {
        var profilePropertyNames = ["Manager"];
        var targetUser = userProfileProperties.get_accountName();
        var userProfilePropertiesForUser = new SP.UserProfiles.UserProfilePropertiesForUser(clientContext, targetUser, profilePropertyNames);
    
        managerProfileProperties = peopleManager.getUserProfilePropertiesFor(userProfilePropertiesForUser);
    
        clientContext.load(userProfilePropertiesForUser);
        clientContext.executeQueryAsync(GetManagerDetails, failure);
    };
    
    
    function GetManagerDetails() {
        var profilePropertyNames = ["PreferredName"];
        var targetUser = managerProfileProperties[0];
        var userProfilePropertiesForUser = new SP.UserProfiles.UserProfilePropertiesForUser(clientContext, targetUser, profilePropertyNames);
    
        managerProfileProperties = peopleManager.getUserProfilePropertiesFor(userProfilePropertiesForUser);
    
        clientContext.load(userProfilePropertiesForUser);
        clientContext.executeQueryAsync(showDetails, failure);  
    	
    }
    
    
    function showDetails() {
       
    var mgrName = (managerProfileProperties[0]);
    
    	SetmgrFieldValue("Supervisor",mgrName);
    }
    function failure() {
        alert("Failed");
    };
    
    
    //set manager of current user
    
    function SetmgrFieldValue(fieldName, mgrName) {
     
    var controlmgrName = fieldName;
     
    var peoplemgrPickerDiv = $("[id$='ClientPeoplePicker'][title='" + controlmgrName + "']");
     
    var peoplemgrPickerEditor = peoplemgrPickerDiv.find("[title='" + controlmgrName + "']");
     
    var spPeoplemgrPicker = SPClientPeoplePicker.SPClientPeoplePickerDict[peoplemgrPickerDiv[0].id];
     
    //comment out this field if edit form needs to be read only 
    peoplemgrPickerEditor.val(mgrName);
    //comment out this field if edit form needs to be read only  
    spPeoplemgrPicker.AddUnresolvedUserFromEditor(true);
     
    //disable the field
     
    spPeoplemgrPicker.SetEnabledState(false);
     
    //hide the delete/remove use image from the people picker
     
    $('.sp-peoplepicker-delImage').css('display','none');
     
    };
    
    GetCurrentUser();
    
    });
    </script>

    Thursday, November 7, 2013 10:15 PM