Developer forum

Forum » Development » Get Users from API

Get Users from API

Anders Ebdrup
Anders Ebdrup
Reply

Dear Dynamicweb,

 

What is best practise for getting specific users from the api when the methods GetUsersBySql and and GetUsers(CommandBuilder) are obsolete?

 

Best regards,

Anders


Replies

 
Kevin Steffer
Kevin Steffer
Reply

Hi Anders, 

We typically use the methods on this Dynamicweb.Security.UserManagement.User class.

 
Anders Ebdrup
Anders Ebdrup
Reply

Dear Kevin,

 

Thanks for quick answer here. The case is that we need users with a certain customnumber and a specific value in a custom field on the user.

And I cannot see how this is done by the current api?

 

Best regards,

Anders

 
Nicolai Pedersen
Reply

You cannot - only by using the obsolete API - it will stick around until DW10 though...

 
Kevin Steffer
Kevin Steffer
Reply
This post has been marked as an answer

For such scenarios we've used a User Repository with index, queries etc. if you don't need the user object themselves.

Votes for this answer: 1
 
Adrian Ursu Dynamicweb Employee
Adrian Ursu
Reply

Hi Kevin,

I have tried this solution myself but I am encountering some errors every time I add a new user and try to read the users immediately after.

Have you seen something similar? Maybe you have found a solution to it?

Thank you,
Adrian

 
Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply
This post has been marked as an answer

You could create your own safe SQL query to fetch the users from the database and then use Fill to populate them:

private static UserCollection GetUsersByCustomerNumber(string customerNumber)
{
  var users = new UserCollection();
  var sqlCommandText = "SELECT * FROM AccessUser WHERE AccessUserCustomerNumber = {0} AND AccessUser_IsCompany = 1";
  var commandBuilder = new CommandBuilder();
  commandBuilder.Add(sqlCommandText, customerNumber);

  using (var dataReader = Database.CreateDataReader(commandBuilder))
  {
    while (dataReader.Read())
    {
      var user = new User();
      user.Fill(dataReader);
      users.Add(user);
    }
  }
  return users;
}

This only loads the basic user data, so not sure if this gives you exactly what you need (for example, I assume this won't load addresses). If that's a concern, you could do a double query: fetch user IDs with a custom query and then use the API to fetch those users by their ID.

Imar

 

Votes for this answer: 1

 

You must be logged in to post in the forum