Alex Choroshin
Alex Choroshin

Reputation: 6187

Getting Project List via Rally C# API

I need a way to retrieve a list of projects using C#.

Tried doing something like this:

    DynamicJsonObject sub = restApi.GetSubscription("Projects");
        //query the project collection
        Request wRequest = new Request(sub["Projects"]);
        QueryResult queryResult = restApi.Query(wRequest);

        return queryResult.Results.Select(result => new Project()
        {
            Id = result["ObjectID"],
            Name = result["Name"]
        }).ToList();

unfortunately with no success. Can anyone help please?

Upvotes: 0

Views: 516

Answers (1)

nickm
nickm

Reputation: 5966

The code below should print workspaces and projects to which the user whose account is used to authenticate the code has access to.

            DynamicJsonObject sub = restApi.GetSubscription("Workspaces");

            Request wRequest = new Request(sub["Workspaces"]);
            wRequest.Limit = 1000;
            QueryResult queryResult = restApi.Query(wRequest);
            int allProjects = 0;
            foreach (var result in queryResult.Results)
            {
                var workspaceReference = result["_ref"];
                var workspaceName = result["Name"];
                Console.WriteLine("Workspace: " + workspaceName);
                Request projectsRequest = new Request(result["Projects"]);
                projectsRequest.Fetch = new List<string>()
                {
                    "Name"
                };
                projectsRequest.Limit = 10000; //project requests are made per workspace
                QueryResult queryProjectResult = restApi.Query(projectsRequest);
                int projectsPerWorkspace = 0;
                foreach (var p in queryProjectResult.Results)
                {
                    allProjects++;
                    projectsPerWorkspace++;
                    Console.WriteLine(projectsPerWorkspace + " Project: " + p["Name"] + " State: " + p["State"]);
                } 
            }
            Console.WriteLine("Returned " + allProjects + " projects in the subscription");

Upvotes: 1

Related Questions