Quantcast
Channel: SharePoint 2013 - Development and Programming forum
Viewing all articles
Browse latest Browse all 7589

Fetch Manager based on people picker value

$
0
0

Hello Techies,

I have list with 2 people picker columns :

  1. Employee Name
  2. Manager

The requirement is to fetch the Manager name based on the name entered in 'Employee Name' field

Though am able get the employee name from the people picker but while trying to fetch Manager's name is throws:

Uncaught TypeError: this.get_context is not a function

Here is the html:

<html><head><script src="/_layouts/15/sp.UserProfiles.js" type="text/javascript"></script><script src="https://code.jquery.com/jquery-3.4.0.min.js"></script><script src="/function/IS/EnterpriseApps/Sachchin/SiteAssets/fetchManager.js" type="text/javascript"></script><link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.8.1/css/all.css"></head><body><h1>Saach</h1><div style="float:right;margin-top:0px;display:none" id="searchDiv"><button  style="height: 25px;padding: 0px;" type="button" id="getManager" value="Get Manager" ><i id="fetchManager" class="fas fa-search"></i></button></div></body></html>

Here is the js:

$(document).ready(function () {    
    $("div[title='Manager']").css("width", "300");
    var attach = $("div[title='Manager']").closest("td");
    $("#searchDiv").detach().prependTo(attach);
    $("#searchDiv").show();

    $("#searchDiv").click(function () {
       SP.SOD.executeOrDelayUntilScriptLoaded(getManager1, 'SP.UserProfiles.js'); 

    });

}); /// end of doc ready

var inputField;

function getManager1() {
    var pickerDiv = document.getElementById("Employee_x0020_Name_5df01b8e-b24b-4aba-90a0-c1fa43654264_$ClientPeoplePicker");
    var peoplePicker = SPClientPeoplePicker.SPClientPeoplePickerDict[pickerDiv.id];
    var theUser = peoplePicker.GetAllUserInfo();
    console.log(theUser);
    if (theUser[0]) {
        var theUserId = theUser[0].Key;
        console.log(theUserId);
        getProfile(theUserId);
    } else {
        alert("Please Enter Employee Name");
    }
    inputField = "Manager";
}

function getProfile(user) {
    clientContext = new SP.ClientContext.get_current();
    var peopleManager = new SP.UserProfiles.PeopleManager(clientContext);
    var profilePropertyNames = ["PreferredName", "Manager", "Department", "Title", "AccountName"];
    console.log("profile for: " + user);
    var userProfilePropertiesForUser = new SP.UserProfiles.UserProfilePropertiesForUser(clientContext, user, profilePropertyNames);
    this.userProfileProperties = peopleManager.getUserProfilePropertiesFor(userProfilePropertiesForUser);
    clientContext.executeQueryAsync(onSuccess, onQueryError);
}
///////////  success state ////////////
function onSuccess() {
    console.log(userProfileProperties);
    var manager = userProfileProperties[1];
    manager = manager.replace("i:0#.f|membership|", "");
    console.log("success = " + manager + "accname= " + userProfileProperties[4])
    $("input[title='Manager']").val(userProfileProperties[3]);   
    SetAndResolvePeoplePicker(inputField, manager);
}
////////error state //////////
function onQueryError(sender, args) {
    alert(args.get_message());
}

function SetAndResolvePeoplePicker(fieldName, userAccountName) {
    var controlName = fieldName;
    var peoplePickerDiv = $("[id$='ClientPeoplePicker'][title='" + controlName + "']");
    var peoplePickerEditor = peoplePickerDiv.find("[title='" + controlName + "']");
    var spPeoplePicker = SPClientPeoplePicker.SPClientPeoplePickerDict[peoplePickerDiv[0].id];
    peoplePickerEditor.val(userAccountName);
    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');
}

Any idea what is the issue


Viewing all articles
Browse latest Browse all 7589

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>