0

I'm making a provider-hosted app for SharePoint on Office 365 and I need to display a list of the users within a site collection. Can anyone point me in the right direction with some code? Your help will be much appreciated.

2

You can use Sharepoint client object model to display the list.

Sample code:

var targetSite = new Uri("site");
            var login = "username";
            var password = "password";
            var securePassword = new SecureString();
            foreach (char c in password)
            {
                securePassword.AppendChar(c);
            }

            var onlineCredentials = new SharePointOnlineCredentials(login, securePassword);

            using (ClientContext clientContext = new ClientContext(targetSite))
            {


                clientContext.Credentials = onlineCredentials;

                Web web = clientContext.Web;
                UserCollection users = web.SiteUsers;
                clientContext.Load(users);
                clientContext.ExecuteQuery();

                foreach(User user in users)
                {
                    string username = user.Email;
                }
            }
| improve this answer | |
0

You could use SharePoint Online Management Shell to remotely manage users and groups in SharePoint Online.

Get-SPOUser cmdlet returns the SharePoint Online user or security group accounts that match a given search criteria, for example:

Get-SPOUser -Site https://contoso.sharepoint.com/sites/finance

returns all user or security group accounts from the site collection http://contoso.sharepoint.com/sites/finance


In addition, SharePoint API provides Web.SiteUsers to get the collection of all users that belong to the site collection

REST endpoint example:

http://contoso.sharepoint.com/sites/finance/_api/web/SiteUsers

JSOM example:

(function(){ 

var context = SP.ClientContext.get_current();
var siteUsers = context.get_web().get_siteUsers();

context.load(siteUsers);    
context.executeQueryAsync(
   function() {
      //print site users 
      for (var i = 0; i < siteUsers.get_count(); i++) {
            var siteUser = siteUsers.getItemAtIndex(i);
            console.log(siteUser.get_title());
      }  
   },
   function(sender,args){
       console.log(args.get_message());
   }
);

})();
| improve this answer | |

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.