none
How to pre-populate Person field with current user's manager (as set in Active Directory)? RRS feed

  • Question

  • Hi there

    How to pre-populate Person field with current user's manager (as set in Active Directory)?
    JavaScript or OOB SharePoint way?

    Thanks.

    Wednesday, February 22, 2017 5:14 PM

Answers

  • You can read an example here

    https://theezitguy.wordpress.com/2014/05/26/sharepoint-auto-fill-fields-using-jquery-and-spservices/

    http://sympmarc.com/2010/04/29/populating-a-sharepoint-list-form-with-the-current-user-information/

    Thanks

    Kislay

    Mark as answer if it helps you

    • Marked as answer by ran009 Wednesday, March 8, 2017 10:07 PM
    Wednesday, February 22, 2017 7:07 PM
  • Hi,

    To add to the abive answers, please follow the instructions in the below article to get and set the Current User's Manager field using JSOM/REST API:

    Get Current Logged in User & Manager using REST API in SharePoint Online

    REST API call to get the properties of the current user:

     var url=_spPageContextInfo.webAbsoluteUrl + "/_api/SP.UserProfiles.PeopleManager/GetMyProperties"
    


    Cheers,
    Maruthu
    Please click the 'Mark as Answer' if this post solves your problem or "Vote As Helpful" if it was useful! :)
    Explore the SharePoint

    Thursday, February 23, 2017 10:00 AM

All replies

  • One way to achieve this would be to make use of the User Profile service and some Rest API/jQuery.

    Create User Profile Property and map it to the desired account. Use the below function in the New/Edit form.

    var currentsiteURL; //Get your current site url
    function getUserProfileProperties(userLoginName) {
    	restURL = currentsiteURL + "/_api/SP.UserProfiles.PeopleManager/GetMyProperties";
    	$.ajax({
    		url: restURL,
    		method: "GET",
    		async: true,
    		headers: { "Accept": "application/json; odata=verbose" },
    		success: function (data) {
    			var properties = data.d.UserProfileProperties.results;
    			for (var i = 0; i < properties.length; i++) {
    				var property = properties[i];
    				if (property.Key == "<<YourCustomProperty>>") {
    					//Get your value by  using property.Value and assign it to the Person fields
    				}
    			}
    		},
    		error: function (data) {
    			console.log('err  ' + getUserProfileProperties);
    		}
    	});
    }

    Please remember to mark the replies as answers if they help.

    Wednesday, February 22, 2017 7:00 PM
  • You can read an example here

    https://theezitguy.wordpress.com/2014/05/26/sharepoint-auto-fill-fields-using-jquery-and-spservices/

    http://sympmarc.com/2010/04/29/populating-a-sharepoint-list-form-with-the-current-user-information/

    Thanks

    Kislay

    Mark as answer if it helps you

    • Marked as answer by ran009 Wednesday, March 8, 2017 10:07 PM
    Wednesday, February 22, 2017 7:07 PM
  • To add to Giridharan_G's response, the property.Key you need is called Manager, and what this key will give you is the Manager's account name. One thing to keep in mind if you are trying to populate a Client-Side People Picker, you have to resolve the Manager's account name once it's set in the correct People Picker element.

    I took the liberty of extending the code above to reflect the approach that I normally take when working with the Poeple Picker.

    /*Variables*/
    var objManager; //will have the Manager property value assigned to it.
    var divPeoplePicker = "div[id*='People Picker DIV ID']" //You can find this with a debugging tool such as Google Chrome debugging tool.
    var objPeoplePickerEditor = "input[id*='People Picker EditorInput']" //You can find this with a debugging tool such as Google Chrome debugging tool.
    var objPPLID; //will have the People Picker ID attribute assigned to it.
    var objPPLDict; //will have the People Picker instance assigned to it.
    var objUser; //will be used to get all the information from the user in the People Picker.
    
    function GetCurrentUserManager()
    {
    	var requestUri = _spPageContextInfo.webAbsoluteUrl + "/_api/SP.UserProfiles.PeopleManager/GetMyProperties";
    	var requestHeaders =  { "accept" : "application/json;odata=verbose" };
    		
    	$.ajax({
    		url: requestUri,
    		method: "GET",
    		headers: requestHeaders,
    		success: onSuccess,
    		error: onError
    	});
    }
    /*onSuccess function for the GetCurrentUser function*/
    function onSuccess(data, request)
    {
    	var properties = data.d.UserProfileProperties.results;
    	for (var i = 0; i < properties.length; i++) {
    		var property = properties[i];
    		if (property.Key == "Manager") {
    			/*Get your value by  using property.Value and assign it to the Person fields*/
    			objManager = property.Value;
    			
    			/*Variables for the People Picker controls*/
    			/*Variable to retrieve the People Picker DIV ID attribute*/
    			objPPLID = $(divPPLName).attr('id');
    				
    			/*Variable to get the instance of the People Picker*/
    			objPPLDict = SPClientPeoplePicker.SPClientPeoplePickerDict[objPPLID];
    				
    			/*Set the value of the Manager retrieved from the objManager variable above*/
    			$(objPeoplePickerEditor).val(objManager);
    			
    			/*Resolved the user in the People Picker*/
    			objPPLDict.AddUnresolvedUserFromEditor(true);
    		}
    	}
    }
    /*onError function for the GetCurrentUser function*/
    function onError(error)
    {
    	console.log('err  ' + getUserProfileProperties);
    }

    I just worked on something like this recently, so it's still fresh in my mind. Please let us know if you have any questions.

    Here's a link with tons of info on getting user information using REST:
    SharePoint 2013: Get UserProfile Properties with REST API


    Fausto Capellan, Jr - SharePoint Admin


    Wednesday, February 22, 2017 9:19 PM
  • Hi,

    To add to the abive answers, please follow the instructions in the below article to get and set the Current User's Manager field using JSOM/REST API:

    Get Current Logged in User & Manager using REST API in SharePoint Online

    REST API call to get the properties of the current user:

     var url=_spPageContextInfo.webAbsoluteUrl + "/_api/SP.UserProfiles.PeopleManager/GetMyProperties"
    


    Cheers,
    Maruthu
    Please click the 'Mark as Answer' if this post solves your problem or "Vote As Helpful" if it was useful! :)
    Explore the SharePoint

    Thursday, February 23, 2017 10:00 AM
  • Hi,

    Would you please provide us with an update on the status of your issue? Please feel free to let us know if further assistance is required.

    Best Regards,

    Lee


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

    Wednesday, March 8, 2017 9:05 AM