0

I have this part of code which should get me for example the "title" of the current user in Sharepoint, but everytime it gives me error: Common Language Runtime detected an invalid program.

<script type="text/javascript">

        var personProperties;

        // Ensure that the SP.UserProfiles.js file is loaded before the custom code runs.
        SP.SOD.executeOrDelayUntilScriptLoaded(getUserProperties, 'SP.UserProfiles.js');

        function getUserProperties() {

            // Replace the placeholder value with the target user's credentials.
            // var targetUser = "domainName\\userName";

            // Get the current client context and PeopleManager instance.
            var clientContext = new SP.ClientContext.get_current();
            var peopleManager = new SP.UserProfiles.PeopleManager(clientContext);

            // Get user properties for the target user.
            // To get the PersonProperties object for the current user, use the
            // getMyProperties method.
            personProperties = peopleManager.getMyProperties();

            // Load the PersonProperties object and send the request.
            clientContext.load(personProperties);
            clientContext.executeQueryAsync(onRequestSuccess, onRequestFail);
        }

        // This function runs if the executeQueryAsync call succeeds.
        function onRequestSuccess() {

            // Get a property directly from the PersonProperties object.
            var messageText = personProperties.get_userProfileProperties()['Title'];

            alert(messageText);
        }

        // This function runs if the executeQueryAsync call fails.
        function onRequestFail(sender, args) {
            alert(args.get_message());
        }


    </script>

Do you have any ideas why it is happening?

Thank you for any suggestions.

Tom

1 Answers1

0

You can make use of SP.SOD.executeFunc to load the files in the correct manner.

Try the below code:

SP.SOD.executeFunc('SP.js', 'SP.ClientContext', function() {
   // Make sure PeopleManager is available 
   SP.SOD.executeFunc('userprofile', 'SP.UserProfiles.PeopleManager', function() {

            var clientContext = new SP.ClientContext.get_current();
            var peopleManager = new SP.UserProfiles.PeopleManager(clientContext);

            // Get user properties for the target user.
            // To get the PersonProperties object for the current user, use the
            // getMyProperties method.
            personProperties = peopleManager.getMyProperties();

            // Load the PersonProperties object and send the request.
            clientContext.load(personProperties);
            clientContext.executeQueryAsync(onRequestSuccess, onRequestFail);

            // This function runs if the executeQueryAsync call succeeds.
            function onRequestSuccess() {

                // Get a property directly from the PersonProperties object.
                var messageText = personProperties.get_userProfileProperties()['Title'];

                alert(messageText);
            }

            // This function runs if the executeQueryAsync call fails.
            function onRequestFail(sender, args) {
                alert(args.get_message());
            }

   });

});
Gautam Sheth
  • 2,442
  • 1
  • 17
  • 16
  • you just want the title or any other property as well ? – Gautam Sheth Jan 08 '18 at 12:29
  • I want the address and other things, this is just for a test if its working.. So I need some properties from getMyProperties function.. – Tomáš Vítů Jan 08 '18 at 12:59
  • Is it necessary to use jsom ? The rest api is quite good for this stuff. I can post an answer related to it if you want – Gautam Sheth Jan 08 '18 at 13:55
  • I used REST for Title, email address etc.. But now I need address which is stored in AD properties.. If there is a solution using REST, I will gladly welcome your help.. :) – Tomáš Vítů Jan 08 '18 at 14:05
  • check this endpoint in REST api `/_api/SP.UserProfiles.PeopleManager/GetMyProperties` to get all your properties – Gautam Sheth Jan 08 '18 at 14:38
  • I just checked that there are only properties used in User profile in SP, so Title, Email address, displayname, pictureurl.. but no for example SIP-Location etc.. – Tomáš Vítů Jan 08 '18 at 16:04
  • I ran in in our proper SP (not in test environment) and what I got is this error: UserProfileApplicationNotAvailableException_Logging :: UserProfileApplicationProxy.GetRawPartitionID has null proxy – Tomáš Vítů Jan 09 '18 at 13:20
  • Looks like something wrong with your user profile service application. Please check with your SharePoint admin. – Gautam Sheth Jan 09 '18 at 18:06