Reputation: 87
I met KeyNotFound exception when i've tried to query the user list in rally rest .net api. The example code is following:
Request query = new Request("User");
query.Workspace = workspaceRef;
var response = api.Query(query);
The detail of exception is : "The given key was not present in the dictionary." and its stack traces:
at System.Collections.Generic.Dictionary`2.get_Item(TKey key)
at Rally.RestApi.DynamicJsonObject.GetMember(String name)
at Rally.RestApi.RallyRestApi.Query(Request request)
Please advise me the proper way to query a list of users of the workspace.
BTW, is there any way to get a list of users who has permission to access a project in Rally Rest .NET api?
Many thanks for ur helps.
Upvotes: 1
Views: 1114
Reputation:
your syntax looks Ok. Where are you seeing the "...key was not present..." error occur? When accessing the Results collection?
I'm including a code sample below that illustrates querying for users within a Subscription and summarizing their Workspace and Project Permissions. I hope this helps.
// Query for User
Request userRequest = new Request("user");
userRequest.Fetch = new List<string>()
{
"UserName",
"Subscription",
"DisplayName",
"UserPermissions"
};
userRequest.Query = new Query("");
QueryResult queryUserResults = restApi.Query(userRequest);
String userName;
String displayName;
String mySubscriptionRef;
String mySubscriptionName;
// Fetch strings to pull in Subscription and UserPermission metadata
string[] subscriptionFetch = { "Name", "SubscriptionID", "CreationDate" };
string[] userPermissionFetch = { "Name", "Role", "Workspace", "Project" };
string[] workspaceFetch = {"Name", "Description"};
foreach (var result in queryUserResults.Results)
{
userName = result["UserName"];
displayName = result["DisplayName"];
var mySubscription = result["Subscription"];
mySubscriptionRef = mySubscription["_ref"];
// Query by Workspace Ref.
var mySubscriptionFetched = restApi.GetByReference(mySubscriptionRef, subscriptionFetch);
mySubscriptionName = mySubscriptionFetched["Name"];
Console.WriteLine("Username: " + userName);
Console.WriteLine("Display Name: " + displayName);
Console.WriteLine("Subscription: " + mySubscriptionName);
var myUserPermissions = result["UserPermissions"];
// Loop through UserPermissions Collection
foreach (var thisPermission in myUserPermissions)
{
// Grab UserPermission ref
var myUserPermissionRef = thisPermission["_ref"];
// Query from UserPermission ref
var myUserPermissionFetched = restApi.GetByReference(myUserPermissionRef, userPermissionFetch);
// Output project name
Console.WriteLine(" Role: " + myUserPermissionFetched["Role"]);
// Try Workspace ref
try
{
var myWorkspace = myUserPermissionFetched["Workspace"];
var myWorkspaceRef = myWorkspace["_ref"];
// Query from Workspace ref
var myWorkspaceFetched = restApi.GetByReference(myWorkspaceRef, workspaceFetch);
// Output Workspace data
Console.WriteLine(" Workspace Name:" + myWorkspaceFetched["Name"]);
}
catch (KeyNotFoundException knfe)
{
Console.WriteLine("Key not found: " + "Workspace");
}
// Try Project ref
try
{
var myProject = myUserPermissionFetched["Project"];
var myProjectRef = myProject["_ref"];
// Query from Workspace ref
var myProjectFetched = restApi.GetByReference(myProjectRef, workspaceFetch);
// Output Project data
Console.WriteLine(" Project Name:" + myProjectFetched["Name"]);
}
catch (KeyNotFoundException knfe)
{
Console.WriteLine("Key not found: " + "Workspace");
}
Upvotes: 2