Community
Vault Forum
Welcome to Autodesk’s Vault Forums. Share your knowledge, ask questions, and explore popular Vault topics.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Getting Group using .NET

10 REPLIES 10
Reply
Message 1 of 11
Alainmorin
742 Views, 10 Replies

Getting Group using .NET

How can we get the Group for each users, using .NET?  Similar to the example below.  (in VB!)

 

I am getting a empty answer, because we are using Group, instead of Roles for each users.

 

 

Link:

http://adndevblog.typepad.com/manufacturing/2012/07/vault-api-users-and-roles-part-1.html#comment-6a...

 

 

VB Code;

PublicSharedSub PrintUserInfo()

Try

Using mgr AsNewMyVaultServiceManager(MyVaultServiceManager.Mode.[ReadOnly])

' The GetAllUsers method provides all the users' info

'-----------------------------------------------------

Dim users AsUser() = mgr.Services.AdminService.GetAllUsers()

' We will show the information in a simple message box

Dim msg AsString = ""

ForEach user AsUserIn users

Dim userInfo AsUserInfo = mgr.Services.AdminService.GetUserInfoByUserId(user.Id)

msg += user.Name

EndIf

'Group for each user is missing????????????????????????????

If userInfo.Roles IsNotNothingAndAlso userInfo.Roles.Length > 0Then

msg += vbLf & " Roles:"

ForEach role AsRoleIn userInfo.Roles

msg += (vbTab & "Id: " & +role.Id & "." & vbTab & "Name: ") + role.Name

Next

EndIf

If userInfo.Vaults IsNotNothingAndAlso userInfo.Vaults.Length > 0Then

msg += vbLf & " Vaults:"

ForEach vault AsKnowledgeVaultIn userInfo.Vaults

msg += (vbTab & "Id: " & +vault.Id & "." & vbTab & "Name: ") + vault.Name

Next

EndIf

msg += vbLf & "=================================================" & vbLf

Next

 

MessageBox.Show(msg, "Completed!")

' using

EndUsing

Catch err As System.Exception

MessageBox.Show(err.Message)

EndTry

EndSub

 

 

 

Thank's!

Alain

10 REPLIES 10
Message 2 of 11
smithmat
in reply to: Alainmorin

I'm not sure I understand the question:

  • The below code *is* .NET (VB .NET)
  • Are you asking for corresponding C# code?  If so, that conversion should be pretty straight forward.  

Show us what you have tried and what you are having trouble with.

 

Thanks,

- Matt

Message 3 of 11
Alainmorin
in reply to: smithmat

Click on the link, you will see the Code example from Marat M., my question is, how to get the group instead of the Roles from that example?

Code example:
private void printUserInfo_button_Click(object sender, EventArgs e)
{
MyVault.AdminSample.PrintUserInfo();
}

This is the program output:

image

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
Message 4 of 11
smithmat
in reply to: Alainmorin

Thanks for the clarification.  The code to do that would depend on whether you wanted to get

  • The groups for which the user is a *direct* member, or
  • The groups for which the user is either a direct or indirect member.

For example, if GroupA contains GroupB contains User1, then User1 is a direct member of GroupB, but and indirect member of GroupA.

 

Below I've provided a code sample which deals only with getting the groups where the user is a *direct* member.  Though it could be modified to include groups where the user is an indirect member:

 

            var adminSvc = webServiceManager.AdminService;

            User[] allUsers = adminSvc.GetAllUsers();
            var allUserIds = allUsers.Select(u => u.Id).ToArray();

            Group[] allGroups = adminSvc.GetAllGroups();
            var allGroupIds = allGroups.Select(g => g.Id).ToArray();
            GroupInfo[] allGroupInfos = adminSvc.GetGroupInfosByGroupIds(allGroupIds);

            var userIdToGroupInfos = allUserIds.ToDictionary(uid => uid, uid => allGroupInfos.Where(gi => gi.Users != null && gi.Users.Any(u => u.Id == uid)));

            foreach (var user in allUsers)
            {
                Console.WriteLine("User {0} Groups (Direct Membership):", user.Name);
                var groupInfosForUser = userIdToGroupInfos[user.Id];
                foreach (var groupInfoForUser in groupInfosForUser)
                {
                    Group groupForUser = groupInfoForUser.Group;
                    Console.WriteLine("  Group {0}", groupForUser.Name);
                }

                Console.WriteLine();
            }

 

Hope that helps,

- Matt

Message 5 of 11
Alainmorin
in reply to: smithmat

I would like to get in the userInfo, the Groups for each users is associated (Administrator, Designer, Approvers, etc...).  In the example, it show how to get the Roles, but in my case, it's the Group who determine the Roles for each users.  So, how to get that information using the (Marat M) example?

Message 6 of 11
smithmat
in reply to: Alainmorin

It is very unclear what you want.  You've been given code to get from a given user to the groups they are in.

 

What have you tried, and where are you getting stuck?

 

- Matt

Message 7 of 11
Alainmorin
in reply to: smithmat

I just want to show for each users, the group witch they are listed.  In my case the Roles are define in the Group.

 

Example:

Alex      Groups: Administrator

Jo          Groups: Designer

Steve     Groups: Administrator and Engineering

Paul       Groups: Designer

Stan       Groups: Approvers

Al          Groups: Engineering

Etc...

 

I am using Vault Pro 2014.

 

See part of my code in VB:

'http://adndevblog.typepad.com/manufacturing/2012/07/vault-api-users-and-roles-part-1.html

       Public Shared Sub PrintUserInfo()

           Try

               Using mgr As New MyVaultServiceManager(MyVaultServiceManager.Mode.[ReadOnly])

                   ' The GetAllUsers method provides all the users' info

                   '-----------------------------------------------------

                   Dim users As User() = mgr.Services.AdminService.GetAllUsers()

 

                   Dim msg As String = ""

 

                   For Each user As User In users

                       Dim userInfo As UserInfo = mgr.Services.AdminService.GetUserInfoByUserId(user.Id)

                       msg += user.Name & " (" & userInfo.User.FirstName & " " & userInfo.User.LastName & ")"

                       msg += vbLf & "   Email: " & userInfo.User.Email

 

 

                       'Can not be use in our case????????????????????????????

                       If userInfo.Roles IsNot Nothing AndAlso userInfo.Roles.Length > 0 Then

                           msg += vbLf & "   Roles:"

                           For Each role As Role In userInfo.Roles

                               msg += vbTab & "Id: " & +role.Id & vbTab & "Name: " + role.Name

                           Next

                       End If

 

                       'In construction!!!!!!!!!!!!!!!!!!!!!

                       If userInfo.Vaults IsNot Nothing AndAlso userInfo.Vaults.Length > 0 Then

                           msg += vbLf & "   Groups:"

                           For Each group As KnowledgeVault In userInfo.Vaults

                               msg += vbTab & "Id: " & +group.Id & vbTab & "Name: " + group.Name

                           Next

                       End If

 

 

                       If userInfo.Vaults IsNot Nothing AndAlso userInfo.Vaults.Length > 0 Then

                           msg += vbLf & "   Vaults:"

                           For Each vault As KnowledgeVault In userInfo.Vaults

                               msg += vbTab & "Id: " & +vault.Id & vbTab & "Name: " + vault.Name

                           Next

                       End If

                       msg += vbLf & "=================================================" & vbLf

                   Next

 

 

                   MessageBox.Show(msg, "Completed!")

                   ' using

               End Using

           Catch err As System.Exception

               MessageBox.Show(err.Message)

           End Try

       End Sub

Message 8 of 11
smithmat
in reply to: Alainmorin

That is what the sample code I gave you does:  Prints out the user followed by the groups for which they are a direct member.  It uses the methods

  • GetAllGroups
  • GetGroupInfosByGroupIds

To get all the groups and groupInfos for the Vault.

The GroupInfo has a Users array which lists all the users in that group.

 

I used that information to create a mapping in the other direction (User to GroupInfo).

 

- Matt

 

 

Message 9 of 11
Alainmorin
in reply to: smithmat

Thank's!  I am just a beginer in VB.NET, I will have to dig more for the Vautl.  Bad thing VBA is not in Vault.

Message 10 of 11
Alainmorin
in reply to: smithmat

Question for you Matt.

 

Why I can't read the information for the Groups... in the User Profile? (Administrator,Designer)

 

I have access to all fields in the User Profile, except for that one.

 

I try to get the group.Name for that user.

 

Thank's!

 

Message 11 of 11
smithmat
in reply to: Alainmorin

Unfortuantely, I don't know the answer to the question of why it is like that.

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report