Rally API Toolkit for .NET: how to create a testset in a user's non-default project

Document ID : KB000057539
Last Modified Date : 14/02/2018
Show Technical Document Details

Issue

How to create a testset in a user's non-default project using Rally API Toolkit for .NET?

Resolution

The code example below is based on v.3.0.1 of Rally API Toolkit for .NET
The code is available in Rally Community repo here.

Setup: the user whose ApiKey is used to authenticate the requests in the code has different default Workspace and Project than those referenced in the code.
Here is the project tree relevant to this example:

User-added image

TestSet created in the code will be created in Child2 project.

The WS API query below illustrates that the ObjectID of workspace and project used in the code below reference the projects shown above:

https://rally1.rallydev.com/slm/webservice/v2.0/project?workspace=https://rally1.rallydev.com/slm/webservice/v2.0/workspace/1011574887&query=(Name = "Child2")

Here is the result of the query:

User-added image

Lines 19 and 20 here set project and workspace references as shown below:
?
String workspaceRef = "/workspace/1011574887"; 
String projectRef = "/project/1791269111";

First, a TestSet is created:
DynamicJsonObject myTestSet = new DynamicJsonObject();
myTestSet["Name"] = "important set " + DateTime.Now;
myTestSet["Project"] = projectRef;

CreateResult createTestSet = restApi.Create(workspaceRef, "TestSet", myTestSet);
myTestSet = restApi.GetByReference(createTestSet.Reference, "FormattedID", "Project");
Console.WriteLine(myTestSet["FormattedID"] + " " + myTestSet["Project"]._refObjectName);

To show how to use the reference to the newly created TestSet, we query for a current iteration. If iteration is found, we update the TestSet:
?
Request iterationRequest = new Request("Iteration");
iterationRequest.Project = projectRef;
iterationRequest.ProjectScopeDown = false;
iterationRequest.ProjectScopeUp = false;
iterationRequest.Fetch = new List<string>() { "ObjectID", "Name" };
iterationRequest.Query = new Query("(StartDate <= Today)").And(new Query("(EndDate >= Today)"));
QueryResult queryResults = restApi.Query(iterationRequest);
if (queryResults.TotalResultCount > 0)
    {
        Console.WriteLine(queryResults.Results.First()["Name"] + " " + queryResults.Results.First()["ObjectID"]);
         myTestSet["Iteration"] = queryResults.Results.First()._ref;
         OperationResult updateResult = restApi.Update(myTestSet["_ref"], myTestSet);
    }

Here is the console output:

User-added image
Screenshot below show that the TestSet was created in the intended project.
Here is a view in Child2 project. TS37 printed in the console is shown below on IterationStatus page:

User-added image
Depending on the current global scope the TestSet may or may not appear in Parent project.
Here is IterationStatus page in Parent project with no scoping down:

User-added image

The same IterationStatus page in Parent project with scoping down enabled:

User-added image
NOTE: the example is available AS IS. It is not supported by Rally support. It is outside of Rally support's scope to write or debug custom code. The example is available for illustration purposes only.