2012-11-05 63 views
2

我是新來的拉力賽世界,我正在努力通過如何使用RallyRestAPI。有很多關於如何查詢拉力賽來取回我發現最有用的相關信息的例子。我想要做的是創建一個C#命令行工具來將用戶添加到項目中。看來我使用RallyRestAPI.Create(「projectpermission」,一些動態json對象)接口來完成創建。我的問題是理解「一些動態json對象」部分。我不確定如何正確設置它。如果有人有一個如何設置它的小例子,我將不勝感激。拉力賽API:將用戶添加到項目中

回答

0

下面是說明如何做到這一點的代碼示例。需要注意的是:

  • 用戶名運行的代碼來創建權限必須是工作區或訂閱管理員
  • 用戶必須已經具有權限的工作區(即是一個工作區用戶)爲了使ProjectPermission創建成功

    命名空間RestExample_AddUsersToProject { 類節目 { 靜態無效的主要(字符串[]參數) {

     String userName = "[email protected]"; 
         String userPassword = "topsecret"; 
         String serverUrl = "https://rally1.rallydev.com"; 
         String wsapiVersion = "1.38"; 
    
         RallyRestApi restApi = new RallyRestApi(
          userName, 
          userPassword, 
          serverUrl, 
          wsapiVersion 
         ); 
    
         restApi.Headers[RallyRestApi.HeaderType.Vendor] = "Rally Software"; 
         restApi.Headers[RallyRestApi.HeaderType.Name] = "RestExample_AddUsersToProject"; 
    
         // Query for Project for which we want to add permissions 
         Request projectRequest = new Request("project"); 
         projectRequest.Fetch = new List<string>() 
         { 
          "Name", 
          "Owner", 
          "State", 
          "Description" 
         }; 
         String projectName = "Avalanche Hazard Mapping"; 
    
         projectRequest.Query = new Query("Name", Query.Operator.Equals, projectName); 
         QueryResult queryProjectResults = restApi.Query(projectRequest); 
         var myProject = queryProjectResults.Results.First(); 
         String myProjectReference = myProject["_ref"]; 
    
         Console.WriteLine("Project Name: " + myProject["Name"]); 
         Console.WriteLine("State: " + myProject["State"]); 
    
         // Query for User for whom we wish to add ProjectPermission 
         Request userRequest = new Request("user"); 
         userRequest.Fetch = new List<string>() 
          { 
           "UserName", 
           "Subscription", 
           "DisplayName" 
          }; 
    
    
         // User needing the permissions 
         userRequest.Query = new Query("UserName", Query.Operator.Equals, "\"[email protected]\""); 
         QueryResult queryUserResults = restApi.Query(userRequest); 
    
         var myUser = queryUserResults.Results.First(); 
         String myUserReference = myUser["_ref"]; 
    
         Console.WriteLine("Username: " + myUser["UserName"]); 
         Console.WriteLine("Display Name: " + myUser["DisplayName"]); 
         Console.WriteLine("Subscription: " + myUser["Subscription"]); 
    
         // Setup required ProjectPermission data 
         DynamicJsonObject newProjectPermission = new DynamicJsonObject(); 
    
         newProjectPermission["User"] = myUser; 
         newProjectPermission["Project"] = myProject; 
         newProjectPermission["Role"] = "Editor"; 
    
         // Create the permission in Rally 
         CreateResult addProjectPermissionResult = restApi.Create("ProjectPermission", newProjectPermission); 
    
         DynamicJsonObject fetchedProjectPermission = restApi.GetByReference(addProjectPermissionResult.Reference, "Name"); 
         Console.WriteLine("Created ProjectPermission with Role: " + fetchedProjectPermission["Name"]);    
        } 
    } 
    

    }

+0

謝謝。這就是我需要看到的。 – dfrobison

0

有CRUD +查詢這裏的一些非常基本的例子:
http://developer.rallydev.com/help/rest-api-net

的一般流程總是會創建一個新的DynamicJsonObject,設置相應的字段,然後通過該對象的創建方法RallyRestApi。