SharePoint REST显示当前用户个人资料图片

时间:2016-10-11 16:52:40

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

通过REST从当前用户的个人资料中获取网址时,src更新时无法显示图片代码:

import groovy.transform.CompileStatic
import groovy.transform.Immutable

import java.util.function.Function
import static java.util.stream.Collectors.groupingBy;

@CompileStatic
public class ThanksForChecking {

    @Immutable
    public static class Relation {
        String entityUUID
    }

    public static void main(String[] args) {
        List<LinkedHashMap<String, String>> rows = [
                [entityUUID: "uuid 1"],
                [entityUUID: "uuid 2"]
        ]

        Map<String, List<Relation>> relations = rows.stream()
                .map { row -> new Relation(row as HashMap) }
                .collect(groupingBy((Function) { Relation r -> return r.entityUUID }))

        println(relations);

        assert relations == [
                "uuid 1": [new Relation(entityUUID: "uuid 1")],
                "uuid 2": [new Relation(entityUUID: "uuid 2")]
        ]
    }
}

返回网址在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放在浏览器地址栏中时,图片显示正常,dev工具响应标题将图片显示为URL的内容,MIME类型为image / jpg。

如何从REST调用中显示配置文件图像?

1 个答案:

答案 0 :(得分:6)

除了返回/_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);

,其中

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