SharePoint Online developer: SharePoi
Blog link: https://blog.51cto.com/13969817
When we use SharePoint Hosted App to get the user ID, you will find that JavaScript Object Model (JSOM) in SharePoint is easier to use than REST API.
The first limitation of REST Protocol is that it returns only 100 entries, and I haven't found a way to add this, but using JSOM you can get a list of all users and sort by ID.
For example, we need list to save User ID, and then use REST API and JSOM to get the code for the user list, respectively.
Use REST API to get a list of users and sort by ID, Sample Code as follows:
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=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)
}
However, only 100 items will be returned, and using JSOM, you can get a list of all User sorted by User ID, sample Code as follows:
Function getAllUsers () {
Var userInfoList = context.get_site (). Get_rootWeb (). Get_siteUserInfoList ()
Var camlQuery = new SP.CamlQuery ()
CamlQuery.set_viewXml ('')
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 ())