SharePoint REST显示当前用户配置文件图片

det*_*ode 3 sharepoint office365 sharepoint-2013 office365-apps office365-restapi

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

<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>
Run Code Online (Sandbox Code Playgroud)

在SharePoint Online中,返回URL如下所示: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放置在浏览器地址栏中时,图片会显示正常,而dev工具的响应标头将图片显示为URL的内容,而MIME类型是image / jpg。

您如何从REST呼叫中显示个人资料图像?

Vad*_*hev 7

除了/_api/sp.userprofiles.peoplemanager/getmyproperties返回PersonalUrl属性的端点外,还可以通过_layouts/15/userphoto.aspx?size=<size>&accountname=<accountname>页面请求用户个人资料图片,其中

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

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

哪里

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