Tuesday, April 17, 2018

Get user's manager and insert into a column in SharePoint Online

Get user's manager name and insert into a list column for SharePoint Online

We can use SharePoint2013 designer workflow/MS Flow to achieve it and also we can use REST API to get user's manager details.



<script type="text/javascript" src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script type="text/javascript">

GetCurrentUser(encodeURIComponent($.parseJSON($("[title='Owner'] input").val())[0].Key));
//

function GetCurrentUser(loginName) {
var requestUri = _spPageContextInfo.webAbsoluteUrl + "/_api/SP.UserProfiles.PeopleManager/GetUserProfilePropertyFor(accountName=@v,propertyName='Manager')?@v=" + "'" + loginName + "'";
var requestHeaders = { "accept" : "application/json;odata=verbose" };
$.ajax({
  url : requestUri,
  method: "GET",
  async:false,
  headers : requestHeaders,
  success : onSuccess,
  error : onError
});

}
function onSuccess(data){
  var department = decodeURIComponent(data.d.GetUserProfilePropertyFor)
  SetUserFieldValue("OwnerManager",department);
 
  return true;
}
function SetUserFieldValue(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 onError(jqxr,errorCode,errorThrown){
alert("Error: " + jqxr.responseText)
}

</script>

No comments:

Post a Comment