你的語法看起來不錯。你在哪裏看到「......鑰匙不存在......」錯誤發生?訪問結果集合時?
我在下面的代碼示例中說明了在訂閱中查詢用戶並總結他們的Workspace和Project Permissions。我希望這有幫助。
// 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");
}
FYI當檢查UserPermissions是必要的,因爲默認情況下,在拉力賽的WSAPI try/catch塊,訂閱管理員可以空UserPermissions – 2012-04-01 20:00:28
感謝@馬克,現在它工作。似乎需要將一個Query對象分配給Request。 – 2012-04-03 04:43:59
此外,我們可以使用thisPermission [「_ type」]來確定要處理的權限類型(工作區或項目)。 – 2012-04-03 06:23:47