【问题标题】:Retrieve the custom user profile properties using JSOM with rest api and display it into a SharePoint page使用带有 rest api 的 JSOM 检索自定义用户配置文件属性并将其显示到 SharePoint 页面中
【发布时间】:2018-04-19 13:16:34
【问题描述】:

如何使用带有 rest api 的 JSOM 检索自定义用户配置文件属性。

因为我需要检索当前登录用户的员工成本中心(自定义属性) 但下面的代码不起作用。

<script>

$.ajax({

        url: _spPageContextInfo.webAbsoluteUrl + "/_api/SP.UserProfiles.PeopleManager/GetMyProperties",  
        headers: { Accept: "application/json;odata=verbose" },  
        success: function (data) {  
            try {  

               // userDisplayName=data.d.DisplayName;   
               //AccountName = data.d.AccountName;  
                var properties = data.d.UserProfileProperties.results;  
                for (var i = 0; i < properties.length; i++) {  



                    if (properties[i].Key == "Employee Cost Center") {  
                       var CostCenter= properties[i].Value;  
                    }  

                                       }  

                $('#Diviision').text(CostCenter);  

            } catch (err2) {  

            }  
        },  
        error: function (jQxhr, errorCode, errorThrown) {  
            alert(errorThrown);  
        }  
    });  

    </script>

【问题讨论】:

    标签: sharepoint-2013 jquery-ajaxq sharepoint-jsom


    【解决方案1】:

    修改代码如下:

    <div id="Diviision"></div>
    <script src="//code.jquery.com/jquery-1.12.4.min.js"></script>
    <script type="text/javascript">
    $(function () {
        var requestUri = _spPageContextInfo.webAbsoluteUrl + "/_api/SP.UserProfiles.PeopleManager/GetMyProperties";
        //execute AJAX request
        $.ajax({
            url: requestUri,
            type: "GET",
            headers: { "ACCEPT": "application/json;odata=verbose" },
            async: false,
            success: function (data) {          
                var properties = data.d.UserProfileProperties.results;  
                for (var i = 0; i < properties.length; i++) {
                    if (properties[i].Key == "Employee Cost Center") {  
                        var CostCenter= properties[i].Value;
                        $('#Diviision').text(CostCenter);
                    }
                }           
            },
            error: function () {
                //alert("Failed to get details");                
            }
        });
    });
    </script>
    

    【讨论】:

      猜你喜欢
      • 2017-10-06
      • 1970-01-01
      • 2017-12-05
      • 1970-01-01
      • 1970-01-01
      • 2020-11-17
      • 1970-01-01
      • 2014-05-21
      • 1970-01-01
      相关资源
      最近更新 更多