1

I want to auto populate user manager name from employee column which is a people picker column. I am able to get all properties and able to fetch user profile properties as well using below code but how to populate base on employee name which is people picker

<script>
    $( document ).ready(function() {
        getItems();     
    });

    function getItems() {
        //alert("djdj")
        var siteUrl = _spPageContextInfo.siteAbsoluteUrl;
        $.ajax({
            url: siteUrl + "/_api/SP.UserProfiles.PeopleManager/GetMyProperties",
            type: "GET",
            headers: {
                "accept": "application/json;odata=verbose",
            },
            success: function (data) {
                console.log(data);
                var Manager = data.d.UserProfileProperties.results[17].Value;
                console.log(Manager);
            },
            error: function (error) {
                alert(JSON.stringify(error));
            }
        });
    }
</script>
  • is it OOTB list form ? or your custom form – Siddharth Vaghasia Jun 21 at 7:07
  • its outbox list form – kumar Jun 21 at 8:48
0

Below is working fine, Inspired by answer from @Hellofiano_MSFT

     <script>
      function PreSaveAction() {
   GetValuePeoplePicker();
    return true;
}

    function getUserManager(username){
        var manager="";
        var requestUri = _spPageContextInfo.webAbsoluteUrl + "/_api/SP.UserProfiles.PeopleManager/GetUserProfilePropertyFor(accountName=@v,propertyName='Manager')?@v='"+username+"'";
        $.ajax({
             url: requestUri,
             method: "GET",
             async:false,
             headers: { "Accept": "application/json; odata=verbose" },
             success: function (data) {
                manager = data.d.GetUserProfilePropertyFor;    
                SetAndResolvePeoplePicker("Manager", manager)      
             },
             error: function (data) {
             }
         });
        return manager;
    }



    function SetAndResolvePeoplePicker(fieldName, userName) {
        var _PeoplePicker = $("div[title='" + fieldName + "']");
        var _PeoplePickerTopId = _PeoplePicker.attr('id');
        var _PeoplePickerEditer = $("input[title^='" + fieldName + "']");
        _PeoplePickerEditer.val(userName);
        var _PeoplePickerOject = SPClientPeoplePicker.SPClientPeoplePickerDict[_PeoplePickerTopId];
        _PeoplePickerOject.AddUnresolvedUserFromEditor(true);
    }
function GetValuePeoplePicker(fieldName) {
    var _PeoplePicker = $("div[title='" + fieldName + "']");
    var _PeoplePickerTopId = _PeoplePicker.attr('id');
    var _PeoplePickerEditer = $("input[title^='" + fieldName + "']");

    var _PeoplePickerOject = SPClientPeoplePicker.SPClientPeoplePickerDict[_PeoplePickerTopId];
   var userinfo = _PeoplePickerOject.GetAllUserInfo();
   console.log(userinfo);
   console.log(userinfo[0].EntityData.Email);
   getUserManager(userinfo[0].EntityData.Email)  
}

    </script>
  • thank you siddharth , it work for current user login name any way to find on sharepoint presave i want to get employee manager and employee is my people picker field in list form, – kumar Jun 21 at 9:46
  • what you need is your employees username...pass it to getUserManager field..let me check and edit answer. – Siddharth Vaghasia Jun 21 at 10:58
  • thank you it is working – kumar Jun 23 at 11:53
  • please upvote and mark it as answer if it was helpful.. – Siddharth Vaghasia Jun 23 at 13:33
1

Try to use the script as below:

<script src="//code.jquery.com/jquery-3.1.0.min.js" type="text/javascript"></script>
<script type="text/javascript">
function PreSaveAction() {
    PopulateManager();
    return true;
}
function PopulateManager() {
    var nominees = $(".sp-peoplepicker-userSpan[id*='Nomination_x0028_s_x0029']");
    $.each(nominees, function(index,value){
        var username = $(value).attr("sid").split("|")[1];
        var manager=getUserManager(username);
        SetAndResolvePeoplePicker("Managers", manager);    
    });
}
function getUserManager(username){
    var manager="";
    var requestUri = _spPageContextInfo.webAbsoluteUrl + "/_api/SP.UserProfiles.PeopleManager/GetUserProfilePropertyFor(accountName=@v,propertyName='Manager')?@v='"+username+"'";
    $.ajax({
         url: requestUri,
         method: "GET",
         async:false,
         headers: { "Accept": "application/json; odata=verbose" },
         success: function (data) {
            manager = data.d.GetUserProfilePropertyFor;         
         },
         error: function (data) {
         }
     });
    return manager;
}
function SetAndResolvePeoplePicker(fieldName, userName) {
    var _PeoplePicker = $("div[title='" + fieldName + "']");
    var _PeoplePickerTopId = _PeoplePicker.attr('id');
    var _PeoplePickerEditer = $("input[title^='" + fieldName + "']");
    _PeoplePickerEditer.val(userName);
    var _PeoplePickerOject = SPClientPeoplePicker.SPClientPeoplePickerDict[_PeoplePickerTopId];
    _PeoplePickerOject.AddUnresolvedUserFromEditor(true);
}
</script>

Reference: Populate people picker during PreSaveAction

  • i already tried above script giving me below error – kumar Jun 21 at 8:51
  • Object expected – kumar Jun 21 at 8:51

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.