如何使用Rally REST API检索某些用户字段?

时间:2014-04-30 07:36:27

标签: rest rally

当我查询Rally REST API(.net)时,我能够获得除以下字段之外的所有用户值:

  1. 角色
  2. CostCenter
  3. LastLoginDate
  4. OfficeLocation
  5. c_RoleType
  6. CreationDate
  7. 当我在浏览器中启动网址“https://rally1.rallydev.com/slm/webservice/v2.0/user/xxxxxxxxxx”时,我可以访问以上所有字段。

    但是,当我使用REST API时,结果集不包含上述任何字段。

    不确定我是否需要做任何不同的事情。

    request = new Request("user");
    request.Fetch = fetchList;
    request.Workspace = RallyInterface.workspaceRef;
    QueryResult queryResult = restApi.Query(request);
    

    PS。在上面的示例中,Fetch字符串为空,目的是获取所有可能的用户字段。当我调试时,我只能得到以下18个字段

        [0] "_rallyAPIMajor"    string
        [1] "_rallyAPIMinor"    string
        [2] "_ref"  string
        [3] "_refObjectUUID"    string
        [4] "_objectVersion"    string
        [5] "_refObjectName"    string
        [6] "ObjectID"  string
        [7] "Department"    string
        [8] "Disabled"  string
        [9] "DisplayName"   string
        [10]    "EmailAddress"  string
        [11]    "FirstName" string
        [12]    "LastName"  string
        [13]    "MiddleName"    string
        [14]    "Phone" string
        [15]    "Role"  string
        [16]    "UserName"  string
        [17]    "_type" string
    

    此外,当我使用GetByReference()时,我可以获取“角色”的值,但不能获取以下任何字段的值:

    1. CostCenter
    2. LastLoginDate
    3. OfficeLocation
    4. c_RoleType
    5. CreationDate
    6. GetByReference()响应返回字段未找到消息。

1 个答案:

答案 0 :(得分:0)

fetchList应该包含这些字段,或者应该完全省略。下面的代码示例在两种情况下都返回这些字段。这意味着如果我在下面的代码中注释掉userRequest.Fetch,那么这些字段仍会被返回。如果存在提取但不包括这些字段,则不会返回它们。

            userRequest.Fetch = new List<string>()
                {
                   "Role",
                   "CostCenter",
                   "LastLoginDate",
                   "OfficeLocation",
                   "CreationDate"
                };

            userRequest.Query = new Query("UserName", Query.Operator.Equals, "user@co.com");

            QueryResult userResults = restApi.Query(userRequest);

            foreach (var u in userResults.Results)
            {
                Console.WriteLine("Role: " + u["Role"] + " CostCenter: " + u["CostCenter"] + " LastLoginDate: " + u["LastLoginDate"] + " OfficeLocation: " + u["OfficeLocation"] + " CreationDate: " + u["CreationDate"]);
            }

以下是使用GetByReference时此代码的另一种变体:

userRequest.Query = new Query("UserName", Query.Operator.Equals, "user@co.com");

        QueryResult userResults = restApi.Query(userRequest);
        String userRef = userResults.Results.First()._ref;
        DynamicJsonObject user = restApi.GetByReference(userRef, "Name", "Role", "CostCenter", "LastLoginDate", "OfficeLocation", "CreationDate");
        String role = user["Role"];
        String costCenter = user["CostCenter"];
        String lastLoginDate = user["LastLoginDate"];
        String officeLocation = user["OfficeLocation"];
        String creationDate = user["CreationDate"];

        Console.WriteLine("Role: " + role + " CostCenter: " + costCenter + " LastLoginDate: " + lastLoginDate + " OfficeLocation: " + officeLocation + " CreationDate: " + creationDate);

完整代码可在this github repo中找到。