SharePoint 2013 从 SharePoint 托管应用程序调用 REST API
-
10-12-2019 - |
题
我已成功将 SharePoint 托管应用程序部署到 Office 365 和本地安装。该应用程序要做的就是调用用户配置文件管理器的 REST API 来获取用户的配置文件属性。无论我如何尝试调用 API,它都会失败。
1)如果我尝试使用 $.getJson
和网址为 http://host web url/_api/SP.UserProfiles.PeopleManager/GetMyProperties
跨域调用失败。
2)如果我尝试使用请求执行器和 url 作为 http://app web url/_api/SP.UserProfiles.PeopleManager/GetMyProperties
它失败了(未找到)。
3)如果我尝试使用请求执行器和 url 作为 http://app web url/_api/SP.AppContextSite(@t)/SP.UserProfiles.PeopleManager/GetMyProperties?@t='" + encodeURIComponent(hostweburl) + "'"
;它在访问被拒绝和/或未找到时失败。
如果我使用完全相同的 SP.AppContextSite
并尝试简单地获取网络标题,它工作得很好。
是个 SP.AppContextSite
仅限于网络对象?有没有办法从 SharePoint 托管应用程序调用用户配置文件 REST API?
解决方案
将权限设置为 User Profiles = Read
正如 Rob's Answer 中提到的,然后下面的代码应该可以工作。我们不需要使用请求执行器,因为我们在这里不进行跨域调用。
$.ajax({
url:"http://<app web url>/_api/SP.UserProfiles.PeopleManager/GetMyProperties",
headers:{ Accept:"application/json;odata=verbose" },
success:function(data){
alert(data.d.DisplayName);
},
error:function(jQxhr,errorCode,errorThrown){
alert(errorThrown);
}
});
其他提示
您的应用程序是否请求读取用户个人资料的权限?
您还必须按顺序添加权限。首先是租户,然后是用户个人资料(社交)