【问题标题】:Sharepoint 2013 - User profiles JSOMSharepoint 2013 - 用户个人资料 JSOM
【发布时间】:2018-01-08 20:19:36
【问题描述】:

我有这部分代码应该让我获得例如 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>

你知道为什么会这样吗?

感谢您的任何建议。

汤姆

【问题讨论】:

    标签: javascript sharepoint sharepoint-jsom


    【解决方案1】:

    您可以使用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());
                }
    
       });
    
    });
    

    【讨论】:

    • 你只想要标题或任何其他属性?
    • 我想要地址和其他东西,这只是为了测试它是否工作..所以我需要getMyProperties函数的一些属性..
    • 有必要使用 jsom 吗?其余的 api 非常适合这些东西。如果您愿意,我可以发布与之相关的答案
    • 我使用 REST 作为标题、电子邮件地址等。但现在我需要存储在 AD 属性中的地址。如果有使用 REST 的解决方案,我将很高兴欢迎您的帮助.. :)
    • 在 REST api /_api/SP.UserProfiles.PeopleManager/GetMyProperties 中检查此端点以获取您的所有属性
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-06
    • 2010-12-01
    • 2013-05-06
    • 1970-01-01
    • 2014-06-13
    相关资源
    最近更新 更多