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:
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
I'm not sure I understand the question:
Show us what you have tried and what you are having trouble with.
Thanks,
- Matt
Thanks for the clarification. The code to do that would depend on whether you wanted to get
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
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?
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
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
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
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
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.
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!
Unfortuantely, I don't know the answer to the question of why it is like that.