I have some code which will look up the user in the SP PeoplePicker and return the department of the user.
What I want to achieve is automatically filling a field with the current user's department, as I do not want the peoplepicker field. Either the current user data should be retrievable or I should be able to search though a peoplepicker without having a peoplepicker field.
So, I add the following script into a content editor:
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/jquery.SPServices/2014.02/jquery.SPServices-2014.02.min.js"></script>
<script>
$(document).ready(function()
{
var PeoplePicker = $("div[title='test_column']");
var PeoplePickerTopId = PeoplePicker.attr('id');
var ppobject = SPClientPeoplePicker.SPClientPeoplePickerDict[PeoplePickerTopId];
ppobject.OnValueChangedClientScript = function(elementId, userInfo){getUserData()};
});
function getUserData()
{
//Set variables
var UserDept;
var i;
var _PeoplePicker = $("div[title='test_column']");
var _PeoplePickerTopId = _PeoplePicker.attr('id');
var ppobject = SPClientPeoplePicker.SPClientPeoplePickerDict[_PeoplePickerTopId];
// Call GetAllUserInfo to read PeoplePicker entries
editorsInfo = ppobject.GetAllUserInfo();
// Set Customer Info
if(editorNames!= null)
{UserDept = editorsInfo[0].EntityData.Department;}
$("input[title='Department']").val(UserDept)
}
</script>
Note, I do not have access to Sharepoint Designer. I am trying to get this working from the web view only.
Additionally, if the answer requires AJAX or returning the value from a httpget post, can you please be thorough with the answer as I have never used AJAX before and have limited experience with receiving responses from web servers.
thanks in advance!
You can use SPServices Jquery. Just download the latest one. You are using Sharepoint 2013 so use Script Editor instead of Content Editor. Below is the script i use regularly and works fine.
<script type="text/javascript" src="/your path to JS/SiteAssets/JS/jquery-3.1.1.js"></script>
<script type="text/jscript" src="/your path to JS/SiteAssets/JS/jquery.SPServices.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
var UserDept = $().SPServices.SPGetCurrentUser({
fieldName: "Department",
debug: false
});
$("input[title*='Department']").val(UserDept); // For assigning to SharePoint column named 'Department'
$("#txtDept").val(UserDept); // For assigning to textbox
});
</script>