1

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?

2 Answers 2

0

Yes the error is because you are calling your function inside ready, call the below function outside ready function

SP.SOD.executeFunc('sp.js', 'SP.ClientContext', sharePointReady);
2
  • In this case code is not working even in editor mode. Commented Jun 16, 2016 at 9:34
  • Check you may have other script errors. Commented Jun 16, 2016 at 9:52
0

Try this below code:

SP.SOD.executeFunc('SP.js', 'SP.ClientContext', function() {

     clientContext = SP.ClientContext.get_current();
    SP.SOD.executeFunc('SP.UserProfiles.js', 'SP.UserProfiles.PeopleManager', getUserProperties);

});
6
  • This code is not working too. Commented Jun 16, 2016 at 9:51
  • Please add this script reference in your code: <script type="text/javascript" src="/_layouts/15/sp.js"></script><script type="text/javascript" src="/_layouts/15/sp.runtime.js"></script> Commented Jun 16, 2016 at 10:13
  • These libraries are not automatically loaded? Commented Jun 16, 2016 at 10:17
  • Yes they are. Sometimes it gives this kind of error Commented Jun 16, 2016 at 10:37
  • I added these libraries. Error is still there. Commented Jun 16, 2016 at 12:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.