SharePoint 通过JavaScript获取UserProfile文件

  前言

  最近又收到一个需求,需要通过JavaScript代码,获取用户的一些属性。好的,我们有API可以做,安排!

  正文

  1.获取UserProfiles的脚本,通过Get方式获取,我这里比较简单,用了默认的Ajax请求,其实我们可以用JQuery的方式封装,看起来更简便。

 1 var siteUrl = _spPageContextInfo.siteAbsoluteUrl;
 2 var accountName = "i:0#.f|membership|" + _spPageContextInfo.userLoginName;
 3 var xhr = new XMLHttpRequest();            
 4     xhr.open('GET', siteUrl + "/_api/SP.UserProfiles.PeopleManager/GetPropertiesFor(accountName=@v)?@v='" + encodeURIComponent(accountName) + "'", true);
 5     xhr.setRequestHeader( "Accept", "application/json; odata=verbose");
 6     xhr.onreadystatechange = function() {
 7       // readyState == 4说明请求已完成
 8       if (xhr.readyState == 4 && xhr.status == 200 || xhr.status == 304) { 
 9         // 从服务器获得数据 
10         console.log(JSON.parse(xhr.responseText).d);  
11       }
12     };
13     xhr.send();

  2.获取的结果,会有用户的基本信息,想知道详细文件的,大家自己试试获取吧~^_^

posted @ 2023-03-16 22:44  霖雨  阅读(33)  评论(0编辑  收藏  举报