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?

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);
  • In this case code is not working even in editor mode. – DarkGenius Jun 16 '16 at 9:34
  • Check you may have other script errors. – Shiva Jun 16 '16 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);

});
  • This code is not working too. – DarkGenius Jun 16 '16 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> – Dikesh Gandhi Jun 16 '16 at 10:13
  • These libraries are not automatically loaded? – DarkGenius Jun 16 '16 at 10:17
  • Yes they are. Sometimes it gives this kind of error – Dikesh Gandhi Jun 16 '16 at 10:37
  • I added these libraries. Error is still there. – DarkGenius Jun 16 '16 at 12:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.