Sharepoint 2013 - 用户个人资料JSOM

时间:2018-01-08 12:06:02

标签: javascript sharepoint sharepoint-jsom

我有这部分代码应该让我举例如" title" Sharepoint中的当前用户,但每次都给我错误:公共语言运行时检测到无效程序。

<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>

你知道为什么会这样吗?

感谢您提出任何建议。

汤姆

1 个答案:

答案 0 :(得分:0)

您可以使用SP.SOD.executeFunc以正确的方式加载文件。

请尝试以下代码:

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());
            }

   });

});