I need customize NewForm for list for automatical filling some fields based on user's profile data. I add Script Editor webpart to NewForm.aspx page and write into this code:
<script type="text/javascript" src="/_layouts/15/jquery-1.7.2.min.js"></script>
<script type="text/javascript" src="/_layouts/15/SP.UserProfiles.js"></script>
<script type="text/javascript">
var clientContext;
var personProperties;
$(document).ready(
function(){
SP.SOD.executeFunc('sp.js', 'SP.ClientContext', sharePointReady);
}
);
function sharePointReady(){
clientContext = SP.ClientContext.get_current();
SP.SOD.executeFunc('SP.UserProfiles.js', 'SP.UserProfiles.PeopleManager', getUserProperties);
}
function getUserProperties(){
var targetUser = "<domain>\\<login>"; // I write real domain and login in this line
var peopleManager = new SP.UserProfiles.PeopleManager(clientContext);
var profilePropertyNames = ["PreferredName", "Department", "Title"];
var userProfilePropertiesForUser = new SP.UserProfiles.UserProfilePropertiesForUser(
clientContext,
targetUser,
profilePropertyNames);
var userProfileProps = peopleManager.getUserProfilePropertiesFor(userProfilePropertiesForUser);
clientContext.load(userProfilePropertiesForUser);
clientContext.executeQueryAsync(
function () { alert(userProfileProps[0] + " works in " + userProfileProps[1] + " as a " + userProfileProps[2]); },
function () { alert("Failure") });
}
</script>
In edit page mode code works fine. In published page I get exception with text "TypeError: this.get_context is not a function" on this line:
var userProfileProps = peopleManager.getUserProfilePropertiesFor(userProfilePropertiesForUser);
Why it happens and how I can fix it?