David Scott
David Scott

Reputation: 153

Rally Api - How do I page GetByReference results

I've got some code that looks something like this.

var userStory = restApi.GetByReference (userStoryRef, "Name", "FormattedID");
for (int i = 0; i < (int) userStory["TotalResultCount"]; i++)
{
    var name = userStory["Results"][i]["Name"];

...

Of course this fails if the GetByReference has more than 20 results (i.e. TotalResultCount > 20) since the default page size is 20.

So I need to page this but I can't work out how you set the page size, or request a second page from GetByReference.

I tried adding ?pagesize=100 to the reference but it doesn't appear to affect the result set.

e.g.

var userStory = restApi.GetByReference (userStoryRef + "?pagesize=100", "Name", "FormattedID");

I also tried giving up on this altogether and re-phrasing it as a query (which I do know how to page.)

e.g.

var request = new Request("HierarchicalRequirement")
{
    Fetch = new List<string>()
    {
        "Name", "FormattedID"
    }, Query = new Query("Parent.FormattedID", Query.Operator.Equals, featureId)
};

var result = myRestApi.Query(request);       

But this query doesn't work for me either (featureId is a valid formatted id with associated User Stories). But that query returns no results.

(I appreciate that's a different question altogether - happy to get either working but understanding both would be fantastic!)

Grateful as ever for any help.

Upvotes: 1

Views: 393

Answers (1)

nickm
nickm

Reputation: 5966

See code below. Notice storiesRequest.Limit = 1000 When this number is above default 20, it defaults to max 200, so there is no need to set storiesRequest.PageSize to 200

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Rally.RestApi;
using Rally.RestApi.Response;
using Rally.RestApi.Json;

namespace GetByRefExample
{
    class Program
    {
        static void Main(string[] args)
        {
            RallyRestApi restApi = new RallyRestApi(webServiceVersion: "v2.0");
            String apiKey = "_abc123";
            restApi.Authenticate(apiKey, "https://rally1.rallydev.com", allowSSO: false);
            String workspaceRef = "/workspace/123";
            String projectRef = "/project/456";  

            Request request = new Request("PortfolioItem/Feature");
            request.Fetch = new List<string>() { "Name", "FormattedID" };
            request.Query = new Query("FormattedID", Query.Operator.Equals, "F2356");
            QueryResult result = restApi.Query(request);

            String featureRef = result.Results.First()._ref;
            Console.WriteLine("found" + featureRef);

            //create stories
            try
            {
                for (int i = 1; i <= 25; i++)
                {
                    DynamicJsonObject story = new DynamicJsonObject();
                    story["Name"] = "story " + i;
                    story["PlanEstimate"] = new Random().Next(2,10);
                    story["PortfolioItem"] = featureRef;
                    story["Project"] = projectRef;
                    CreateResult createResult = restApi.Create(workspaceRef, "HierarchicalRequirement", story);
                    story = restApi.GetByReference(createResult.Reference, "FormattedID");
                    Console.WriteLine("creating..." + story["FormattedID"]);
                }
            //read stories
                DynamicJsonObject feature = restApi.GetByReference(featureRef, "UserStories");
                Request storiesRequest = new Request(feature["UserStories"]);
                storiesRequest.Fetch = new List<string>()
                {
                    "FormattedID",
                    "PlanEstimate"
                };
                storiesRequest.Limit = 1000;
                QueryResult storiesResult = restApi.Query(storiesRequest);
                int storyCount = 0;
                foreach (var userStory in storiesResult.Results)
                {
                    Console.WriteLine(userStory["FormattedID"] + " " + userStory["PlanEstimate"]);
                    storyCount++;
                }
                Console.WriteLine(storyCount);
            } 
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }
    }
}

Upvotes: 1

Related Questions