SharePoint Online 开发篇(SharePoint Hosted Apps获取用户ID)

学向勤中得,萤窗万卷书。这篇文章主要讲述SharePoint Online 开发篇:SharePoint Hosted Apps获取用户ID相关的知识,希望能为你提供帮助。
Blog链接:https://blog.51cto.com/13969817
我们使用SharePoint Hosted App获取用户ID时,你将发现SharePoint中的javascript Object Model(JSOM)比REST API更容易使用。
REST Protocol的第一个限制是它只返回100个条目,我还没有找到一个方法来增加这个,而使用JSOM可以获取所有用户的列表,并按照ID 排序。
比如我们需要list来保存User ID,然后分别使用REST API和JSOM获取用户列表的代码。
使用REST API来获得用户的列表,并按ID排序,Sample Code如下:function?getUsers() {
????var?pUrl = _spPageContextInfo.webAbsoluteUrl +?" /_api/site/rootweb/lists/getByTitle(‘User Information List‘)/items?$orderby=Id" ;
????//var pUrl = _spPageContextInfo.webAbsoluteUrl + " /_api/site/rootweb/lists/getByTitle(‘User Information List‘)/items?$orderby=Id& $select=Id,Title,Name,EMail" ;
????$.ajax(pUrl, { method:?" GET" , headers: {?" accept" :?" application/json; odata=https://www.songbingjia.com/android/verbose" ?} }).done(storeUsers).fail(getUserError);
}
?
function?storeUsers(data) {
????var?responseParse = JSON.parse(data.body);
????user_list = responseParse.d.results;
}
?
function?getUserError(jqXHR, textStatus) {
????alert(textStatus);
}
但这只会返回100个项目,而使用JSOM可以获取按照User ID排序所有User 列表,sample Code如下:【SharePoint Online 开发篇(SharePoint Hosted Apps获取用户ID)】function?getAllUsers() {
????var?userInfoList = context.get_site().get_rootWeb().get_siteUserInfoList();
?
????var?camlQuery =?new?SP.CamlQuery(); < br>
????camlQuery.set_viewXml(‘< View> < Query> < OrderBy> < FieldRef Name=‘ID‘ /> < /OrderBy> < /Query> < /View> ‘); ? ?
????userListItemCollection = userInfoList.getItems(camlQuery);
?
????context.load(userListItemCollection);
????//context.load(userListItemCollection, ‘Include(Title,ID,Name,EMail)‘);
?
????context.executeQueryAsync(onGetAllUsersSuccess, onGetAllUsersFail);
}
?
function?onGetAllUsersSuccess() {
????var?userArr = [];
????var?arrNames = [];
????var?listEnumerator = userListItemCollection.getEnumerator();
?
????while?(listEnumerator.moveNext()) {
????????var?oList = listEnumerator.get_current();
?
????????//avoid duplicates
????????var?index = $.inArray(oList.get_item(‘Title‘), arrNames);
????????if?(index == -1) {
????????????userArr.push({
????????????????Id: oList.get_item(‘ID‘),
????????????????Title: oList.get_item(‘Title‘),
????????????????Name: oList.get_item(‘Name‘),
????????????????EMail: oList.get_item(‘EMail‘)
????????????});
????????????arrNames.push(oList.get_item(‘Title‘));
????????}
????}
?
????user_list = userArr;
}
?
function?onGetAllUsersFail(sender, args) {
????alert(" Unable to load user information: " ?+ args.get_message());

    推荐阅读