Last active
March 28, 2016 08:44
-
-
Save marineko/2a60182fb6eb42a9783f to your computer and use it in GitHub Desktop.
Get & Set SharePoint PeoplePicker login user info via REST api.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(document).ready(function () { | |
SP.SOD.executeFunc('sp.js', 'SP.ClientContext', SetCurrentUserInfo); | |
}); | |
function SetCurrentUserInfo() { | |
$.ajax({ | |
url: _spPageContextInfo.webAbsoluteUrl + "/_api/SP.UserProfiles.PeopleManager/GetMyProperties", | |
contentType: "application/json;odata=verbose", | |
headers: { "accept": "application/json;odata=verbose" }, | |
success: onSuccess, | |
error: onError | |
}); | |
} | |
function onSuccess(data, request) { | |
// User Account | |
var loginName = data.d.AccountName; | |
// Display Name | |
var dispName = data.d.DisplayName; | |
// Department | |
var arr = data.d.UserProfileProperties.results; | |
var dept; | |
for (var i = 0, len = arr.length; i < len; i++) { | |
if (arr[i].Key == "Department") { | |
dept = arr[i].Value; | |
} | |
} | |
//Set PeoplePicker | |
var PeoplePickerId = $("div[title='Author']").attr('id'); | |
var PeoplePickerInput = $("input[title='Author']"); | |
PeoplePickerInput.val(dispName); | |
var PeoplePicker = SPClientPeoplePicker.SPClientPeoplePickerDict[PeoplePickerId]; | |
PeoplePicker.AddUnresolvedUserFromEditor(true); | |
//Set Department to text field. | |
$("input[title='Department']").val(dept); | |
} | |
function onError(error) { | |
console.log(error); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment