0

I have an Autohosted SharePoint 2013 App. Therefore I cannot use the Sharepoint built in People Picker controls.

My idea is to display all users which are available in the App/Web to display in a dropdown. I got the dropdown working fine with AjaxControlToolkit and jquery, but I have troubles retrieving the users.

If I use:

SP.UserCollection siteusers = cc.Web.SiteUsers;
cc.Load(siteusers);
cc.ExecuteQuery();

foreach (SP.User member in siteusers)
{

everything works fine if i give my app full permissions. But that is not what I need and also I do not want to use this permission as my app will never get published in the store that way. I tried setting Read permission to Web but then I get an Unauthorized Exception...

Can somebody tell me how to retriev all users from the Web or the App?

best regards Alex

2
  • What about read for site collection? Web.SiteUsers returns users for site collection, so read on Web may not suffice.
    – wjervis
    Commented Apr 16, 2014 at 15:15
  • Odd, I just tested in my site, and I was able to execute the code you had above. Though, I used C#, not js. My app has read permissions on web.
    – wjervis
    Commented Apr 16, 2014 at 15:33

1 Answer 1

0

the key here is to pass the client credentials to the current context! so use an account that would have privlages to search all users. You need to think of it this way... what if the user that your using doesnt have access to the group ect... thats why it works when you give a high privlage, create a new user account that would have high privlage to search through all users and groups. Then use that user within the context.

//create a new context to add the user
ClientContext cc = new ClientContext(weburl);

//make new secure string object
SecureString password = new SecureString();

//mask the password by looping each character 
foreach (char c in "PutYourPassword".ToCharArray()) password.AppendChar(c);

//set the credential of the account that has access to the client context
cc.Credentials = new SharePointOnlineCredentials(username, password);

//you should be able to get all users now from the web!
SP.UserCollection siteusers = cc.Web.SiteUsers;
cc.Load(siteusers);
cc.ExecuteQuery();

foreach (SP.User member in siteusers)
{

}

you need to replace 'PutYourPassword' with your password and username with the user account name.... this account should have sufficent access..... this way your using an account that you setup just for apps to perform tasks that requier elevation rather than giving admin level to the whole app (this method is similar to impersonation)

3
  • Hi, but I cannot know the user and password here. It is supposed to be an app which users can install from the store.
    – AdelPoint
    Commented Apr 16, 2014 at 6:07
  • no, its a fixed user account not related to any user.. its used just for the code to run rather than giving full control to the whole app!
    – Ali Jafer
    Commented Apr 16, 2014 at 8:26
  • AdelPoint cannot hardcode a user, if this app is to be in the store.
    – wjervis
    Commented Apr 17, 2014 at 13:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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