Issue
I would like to get a list of the users registered at the Vault server. Could you provide a code sample for this task, please?
Solution
Autodesk.Connectivity.WebServices.AdminService is the class which manipulates users, groups, roles etc.
To access the list of the users, we will need to connect to the Vault server and read the WebServiceManager.AdminService property first. I will create a utility class to maintain the connection (see the MyVaultServiceManager class in the code below).
Next, there is the AdminService.GetAllUsers() method that returns an array of objects of the User class. From the User class we can get information about the user like his/her name, ID, email address etc.
Here is a sample. The AdminSample.PrintUserInfo() method is what you need to call from your program, for example, when the user presses a button:
private void printUserInfo_button_Click(object sender, EventArgs e)
{
MyVault.AdminSample.PrintUserInfo();
}
This is the program output:
And this is the code itself:
using System.IO;
using System.Windows.Forms;
using Autodesk.Connectivity.WebServices;
using Autodesk.Connectivity.WebServicesTools;
namespace MyVault
{
// A wrapper for the Vault server connection.
// Place it in a 'using' block for automatic call of Dispose(),
// which insures that it logs out when we are done.
//=================================================================
class MyVaultServiceManager : System.IDisposable
{
// We will incapsulate the WebServiceManager here.
// The WebServiceManager will be used for our Vault server calls.
private WebServiceManager _svcManager = null;
public WebServiceManager Services
{ get { return _svcManager; } }
public enum Mode { ReadOnly, ReadWrite };
// Preventing usage of the default constructor - made it private
private MyVaultServiceManager() { }
// Constructor.
// Parameter: - Log in as read-only, which doesn't consume
// a license.
//===============================================================
public MyVaultServiceManager(Mode i_ReadWriteMode)
{
UserPasswordCredentials login = new UserPasswordCredentials(
"localhost", "Vault", "Administrator", "",
(i_ReadWriteMode == Mode.ReadOnly) );
// Yeah, we shouldn't hardcode the credentials here,
// but this is just a sample
_svcManager = new WebServiceManager(login);
}
void System.IDisposable.Dispose()
{
_svcManager.Dispose();
}
} // class MyVaultServiceManager
// In this sample we will try different things related to the
// Vault administration.
//=================================================================
class AdminSample
{
// Lists all the users along with their roles and the vaults they
// have access to.
//===============================================================
public static void PrintUserInfo()
{
try
{
using (MyVaultServiceManager mgr = new MyVaultServiceManager(
MyVaultServiceManager.Mode.ReadOnly))
{
// The GetAllUsers method provides all the users' info
//-----------------------------------------------------
User[] users = mgr.Services.AdminService.GetAllUsers();
// We will show the information in a simple message box
string msg = "";
foreach (User user in users)
{
UserInfo userInfo =
mgr.Services.AdminService.GetUserInfoByUserId(user.Id);
msg += user.Name + "\n----------------";
if (userInfo.Roles != null && userInfo.Roles.Length > 0)
{
msg += "\n Roles:";
foreach (Role role in userInfo.Roles)
{
msg += "\n\tId: " + role.Id
+ ".\tName: " + role.Name;
}
}
if (userInfo.Vaults != null && userInfo.Vaults.Length >0)
{
msg += "\n Vaults:";
foreach (KnowledgeVault vault in userInfo.Vaults)
{
msg += "\n\tId: " + vault.Id
+ ".\tName: " + vault.Name;
}
}
msg += "\n================================\n";
}
MessageBox.Show( msg, "Completed!");
} // using
}
catch (System.Exception err)
{
MessageBox.Show(err.Message);
}
} // PrintUserInfo()
} // class AdminSample
} // namespace AdminExample