0

My app is hosted here:

https://mySite.sharepoint.com/sites/path/myApp/

And user information is hosted here:

https://mySite.sharepoint.com/sites/path/_catalogs/users/simple.aspx

I already use this code to access lists outside of my app

    function getLists() {

        print("retrieving list");

        var hostUrl = decodeURIComponent(getQueryStringParameter("SPHostUrl"));
        currentcontext = new SP.ClientContext.get_current();
        hostcontext = new SP.AppContextSite(currentcontext, hostUrl);
        web = hostcontext.get_web(); // hostcontext instead of currentcontext

        listCollection = web.get_lists();
        list = listCollection.getByTitle(LIST_NAME);

        listItemCollection = list.getItems(""); // use CAML query for specific requests

        context.load(listItemCollection);
        context.executeQueryAsync(sharePointReady, queryFailed);
    }

How can I adjust this code to get a list of all the users in my website like their name and contact information? Or is there another method?

Thanks in advance.

1

REST

/_api/Web/SiteUsers

JSOM

var ctx = SP.ClientContext.get_current();
var users = ctx.get_web().get_siteUsers();
ctx.load(users);
ctx.executeQueryAsync(function() {
  var userArray = [];
  var e = users.getEnumerator();
  while (e.moveNext()) {
    userArray.push(e.get_current());
  }
  console.log(userArray);
});
0

In this case list = listCollection.getByTitle(LIST_NAME); would become list = listCollection.getByTitle("UserInformationList"); or list = listCollection.getByTitle("User Information List");.

That is the underlying list where the users are stored.

  • 1
    How about /_api/Web/SiteUsers or context.get_web().get_siteUsers()? – eirikb Feb 16 '15 at 21:37
  • @PirateEric, I get an error that it doesn't exist. However, @eirikb, both of those seem to work. After using get_siteUsers() do you know how I can iterate over the UserCollection in JavaScript? – Charles Clayton Feb 16 '15 at 22:00
  • 1
    @crclayton var userArray=[];var e = users.getEnumerator(); while(e.moveNext())userArray.push(e.get_current()); – eirikb Feb 16 '15 at 22:03
  • Thanks a ton eirikb! If you want to leave an answer with that info I'll mark it right. – Charles Clayton Feb 16 '15 at 22:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.