【问题标题】:SharePoint REST show current user profile pictureSharePoint REST 显示当前用户个人资料图片
【发布时间】:2016-10-12 06:54:32
【问题描述】:

当通过 REST 从当前用户的配置文件中获取 URL 时,更新 src 时图像标签无法显示:

<img id="escMyPic" src="" alt="" />

<script type="text/javascript">
$.ajax({
    url: strURL + "/_api/sp.userprofiles.peoplemanager/getmyproperties",
    type: "GET",
    headers: {
        "accept": "application/json;odata=verbose",
    },
    success: MyPictureSucceeded,
    error: MyPictureFailed
});
function MyPictureSucceeded(data) {
    if(data.d.PictureUrl != null) {
        $('#escMyPic').attr('src', data.d.PictureUrl);
    }
}
function MyPictureFailed() {
        alert('Error: Unexpected error loading profile picture.');
}
</script>

返回 URL 在 SharePoint Online 中如下所示: https://tenant-my.sharepoint.com:443/User%20Photos/Profile%20Pictures/email_address_MThumb.jpg?t=63601764394

它重定向到: https://tenant-my.sharepoint.com/User%20Photos/Profile%20Pictures/email_address_MThumb.jpg?t=63601764394

使用浏览器开发工具,您会看到返回的 mime 类型不是 image/jpg 而是 text/plain。我已经删除了查询字符串参数,甚至将硬编码的 URL 放在了图像标签 src 中,但它就是不显示。开发工具中的响应正文也是空白的。它似乎在显示之前进行了重定向和身份验证。

将硬编码的URL放在浏览器地址栏时,图片显示正常,开发工具响应头显示图片为URL内容,MIME类型为image/jpg。

如何从 REST 调用中显示个人资料图片?

【问题讨论】:

    标签: sharepoint sharepoint-2013 office365 office365-apps office365-restapi


    【解决方案1】:

    除了返回PersonalUrl属性的/_api/sp.userprofiles.peoplemanager/getmyproperties端点外,还可以通过_layouts/15/userphoto.aspx?size=&lt;size&gt;&amp;accountname=&lt;accountname&gt;页面请求用户头像,其中

    • size - 可以设置为S/M/L,代表小/中/大 图片尺寸
    • accountname - 用户帐号名

    示例

    var url = getMyPictureUrl(_spPageContextInfo.webAbsoluteUrl,_spPageContextInfo.userLoginName,"M");
    $('#escMyPic').attr('src', url);
    

    在哪里

    function getMyPictureUrl(webUrl,accountName,size){
        return webUrl + "/_layouts/15/userphoto.aspx?size=" + size + "&accountname=" + accountName;
    }
    

    【讨论】:

    • 这里的账户名是什么?是电子邮件地址吗?还是i:0#.f|membership|emailAddress 格式?因为每次我得到默认图像。此外,我正在尝试获取其他用户的个人资料图片,而不是当前登录的用户。
    • 第二个,accountName需要以claims格式提供
    • 使用“siteAbsoluteUrl”而不是“webAbsoluteUrl”。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-09-18
    • 1970-01-01
    • 1970-01-01
    • 2021-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多