GetByReference() публичный Метод

Get the object described by the specified reference.
Rally returned an HTML page. This usually occurs when Rally is off-line. Please check the ErrorMessage property for more information. The JSON returned by Rally was not able to be deserialized. Please check the JsonData property for what was returned by Rally. Occurs if authentication is not completed prior to calling this method. Occurs if the passed in aRef parameter is null.
public GetByReference ( string aRef ) : dynamic
aRef string the reference
Результат dynamic
        static void Main(string[] args)
        {
            RallyRestApi restApi;
            restApi = new RallyRestApi("*****@*****.**", "secret", "https://rally1.rallydev.com", "v2.0");

            String projectRef = "/project/12352608219";
            Request defectRequest = new Request("Defect");
            defectRequest.Project = projectRef;
            defectRequest.Fetch = new List<string>()
                {
                    "Name",
            "FormattedID",
                    "Tasks"
                };

            defectRequest.Query = new Query("FormattedID", Query.Operator.Equals, "DE8");
            QueryResult defectResults = restApi.Query(defectRequest);
            String defRef = defectResults.Results.First()._ref;
            String defName = defectResults.Results.First().Name;
            Console.WriteLine(defName + " " + defRef);
            DynamicJsonObject defect = restApi.GetByReference(defRef, "Name", "FormattedID", "Tasks");
            String taskCollectionRef = defect["Tasks"]._ref;
            Console.WriteLine(taskCollectionRef);

            ArrayList taskList = new ArrayList();

            foreach (var d in defectResults.Results)
            {
                Request tasksRequest = new Request(d["Tasks"]);
                QueryResult tasksResult = restApi.Query(tasksRequest);
                foreach (var t in tasksResult.Results)
                {
                    var tName = t["Name"];
                    var tFormattedID = t["FormattedID"];
                    Console.WriteLine("Task: " + tName + " " + tFormattedID);
                    DynamicJsonObject task = new DynamicJsonObject();
                    task["_ref"] = t["_ref"];
                    taskList.Add(task);
                }
            }

            Console.WriteLine("Count of elements in the collection before adding a new task: " + taskList.Count);

            DynamicJsonObject newTask = new DynamicJsonObject();
            newTask["Name"] = "another last task";
            newTask["WorkProduct"] = defRef;
            CreateResult createResult = restApi.Create(projectRef, "Task", newTask);
            newTask = restApi.GetByReference(createResult.Reference, "FormattedID", "Name", "WorkProduct");
            Console.WriteLine(newTask["FormattedID"] + " " + newTask["Name"] + " WorkProduct:" + newTask["WorkProduct"]["FormattedID"]);
            taskList.Add(newTask);

            Console.WriteLine("Count of elements in the array after adding a new task: " + taskList.Count);
            defect["Tasks"] = taskList;
            OperationResult updateResult = restApi.Update(defRef, defect);
        }
        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);
            }
        }
        static void Main(string[] args)
        {
            RallyRestApi restApi;
            restApi = new RallyRestApi("*****@*****.**", "secret", "https://rally1.rallydev.com", "v2.0");
            String projectRef = "/project/12352608219";
            Request request = new Request("TestSet");
            request.Project = projectRef;
            request.Fetch = new List<string>()
            {
                "Name",
                "FormattedID",
                "TestCases"
            };
            request.Query = new Query("FormattedID", Query.Operator.Equals, "TS31");
            QueryResult result = restApi.Query(request);

            String testsetRef = result.Results.First()._ref;
            DynamicJsonObject testset = restApi.GetByReference(testsetRef, "TestCases");
            Request request2 = new Request(testset["TestCases"]);
            request2.Fetch = new List<string>()
            {
                "Name",
                "FormattedID"
            };
            request2.Limit = 1000;
            QueryResult result2 = restApi.Query(request2);
            int caseCount = 0;
            foreach (var testcase in result2.Results)
            {
                Console.WriteLine("TestCase: " + testcase["FormattedID"]);
                caseCount++;
            }
            Console.WriteLine(caseCount);
        }
    static void Main(string[] args)
    {
        RallyRestApi restApi = new RallyRestApi("*****@*****.**", "secret", "https://sandbox.rallydev.com", "v2.0");
        DynamicJsonObject user = restApi.GetCurrentUser();
        String userRef = user["_ref"];
        String workspaceRef = "/workspace/12352608129"; //use valid workspace OID in your Rally
        String projectRef = "/project/14018981229";         //use valid project OID in your Rally

        System.Diagnostics.TextWriterTraceListener myListener = new System.Diagnostics.TextWriterTraceListener("log.log", "myListener");

        try
        {
            //create story
            DynamicJsonObject myStory = new DynamicJsonObject();
            myStory["Name"] = "my story " + DateTime.Now;
            myStory["Project"] = projectRef;
            myStory["Owner"] = userRef;
            CreateResult createStory = restApi.Create(workspaceRef, "HierarchicalRequirement", myStory);
            myStory = restApi.GetByReference(createStory.Reference, "FormattedID", "Owner", "Project");
            myListener.WriteLine(DateTime.Now + "___________\r\n" +  myStory["FormattedID"] + " Owner: " + myStory["Owner"]._refObjectName + " Project: " + myStory["Project"]._refObjectName);

            //update story
            myStory["Description"] = "updated " + DateTime.Now;

            //create tasks
            for (int i = 1; i <= 3; i++)
            {
                DynamicJsonObject myTask = new DynamicJsonObject();
                myTask["Name"] = "task " + i + DateTime.Now;
                myTask["Owner"] = userRef;
                myTask["State"] = "In-Progress";
                myTask["WorkProduct"] = myStory["_ref"];
                CreateResult createTask = restApi.Create(workspaceRef, "Task", myTask);
                myTask = restApi.GetByReference(createTask.Reference, "FormattedID", "Owner", "State");
                myListener.WriteLine(myTask["FormattedID"] + " State: " + myTask["StateX"]);
            }
        }
        catch(Exception e)
        {
            myListener.WriteLine(e);
        }

        myListener.Flush();
    }
        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 projectRef = "/project/32904827032";
                try
                {
                    //create story
                    DynamicJsonObject myStory = new DynamicJsonObject();
                    myStory["Name"] = "another story " + DateTime.Now;
                    myStory["Project"] = projectRef;

                    CreateResult createStory = restApi.Create(workspaceRef, "HierarchicalRequirement", myStory);
                    myStory = restApi.GetByReference(createStory.Reference, "FormattedID", "Project");

                    //update story
                    myStory["Description"] = "updated " + DateTime.Now;
                    myStory["c_CustomString"] = "abc123";
                    Console.WriteLine("--------------------");
                    Console.WriteLine(myStory["FormattedID"]);
                    OperationResult updateResult = restApi.Update(myStory["_ref"], myStory);

                    //create tasks
                    for (int i = 1; i <= 3; i++)
                    {
                        DynamicJsonObject myTask = new DynamicJsonObject();
                        myTask["Name"] = "task " + i + DateTime.Now;
                        myTask["State"] = "In-Progress";
                        myTask["WorkProduct"] = myStory["_ref"];
                        CreateResult createTask = restApi.Create(workspaceRef, "Task", myTask);
                        myTask = restApi.GetByReference(createTask.Reference, "FormattedID", "Owner", "State");
                        Console.WriteLine(myTask["FormattedID"]);

                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                }
        }
        static void Main(string[] args)
        {
            RallyRestApi restApi;
            restApi = new RallyRestApi("*****@*****.**", "secret", "https://rally1.rallydev.com", "v2.0");

            String projectRef = "/project/222";
            Request testSetRequest = new Request("TestSet");
            testSetRequest.Project = projectRef;
            testSetRequest.Fetch = new List<string>()
                {
                    "Name",
            "FormattedID",
                    "TestCases"
                };

            testSetRequest.Query = new Query("FormattedID", Query.Operator.Equals, "TS22");
            QueryResult queryTestSetResults = restApi.Query(testSetRequest);
            String tsRef = queryTestSetResults.Results.First()._ref;
            String tsName = queryTestSetResults.Results.First().Name;
            Console.WriteLine(tsName + " "  + tsRef);
            DynamicJsonObject testSet = restApi.GetByReference(tsRef, "FormattedID", "TestCases");
            String testCasesCollectionRef = testSet["TestCases"]._ref;
            Console.WriteLine(testCasesCollectionRef);

            ArrayList testCasesList = new ArrayList();

            foreach (var ts in queryTestSetResults.Results)
            {
                Request tcRequest = new Request(ts["TestCases"]);
                QueryResult queryTestCasesResult = restApi.Query(tcRequest);
                foreach (var tc in queryTestCasesResult.Results)
                {
                    var tName = tc["Name"];
                    var tFormattedID = tc["FormattedID"];
                    Console.WriteLine("Test Case: " + tName + " " + tFormattedID);
                    DynamicJsonObject aTC = new DynamicJsonObject();
                    aTC["_ref"] = tc["_ref"];
                    testCasesList.Add(aTC);  //add each test case in the collection to array 'testCasesList'
                }
            }

             Console.WriteLine("count of elements in the array before adding a new tc:" + testCasesList.Count);

              DynamicJsonObject anotherTC = new DynamicJsonObject();
              anotherTC["_ref"] = "/testcase/123456789";             //any existing test to add to the collection

               testCasesList.Add(anotherTC);

               Console.WriteLine("count of elements in the array:" + testCasesList.Count);
               testSet["TestCases"] = testCasesList;
               OperationResult updateResult = restApi.Update(tsRef, testSet);
        }
        public override int Export()
        {
            int assetCounter = 0;

            RallyRestApi restApi = new RallyRestApi(_config.RallySourceConnection.Username, _config.RallySourceConnection.Password, _config.RallySourceConnection.Url, "1.43");

            SqlDataReader sdr = GetAttachmentsFromDB();
            string SQL = BuildAttachmentUpdateStatement();

            while (sdr.Read())
            {
                try
                {
                    DynamicJsonObject attachmentMeta = restApi.GetByReference("attachment", Convert.ToInt64(sdr["AssetOID"]), "Name", "Description", "Artifact", "Content", "ContentType");
                    DynamicJsonObject attachmentContent = restApi.GetByReference(attachmentMeta["Content"]["_ref"]);
                    byte[] content = System.Convert.FromBase64String(attachmentContent["Content"]);

                    using (SqlCommand cmd = new SqlCommand())
                    {
                        cmd.Connection = _sqlConn;
                        cmd.CommandText = SQL;
                        cmd.CommandType = System.Data.CommandType.Text;
                        cmd.Parameters.AddWithValue("@AssetOID", sdr["AssetOID"]);
                        cmd.Parameters.AddWithValue("@Name", attachmentMeta["Name"]);
                        cmd.Parameters.AddWithValue("@FileName", attachmentMeta["Name"]);
                        cmd.Parameters.AddWithValue("@Content", content);
                        cmd.Parameters.AddWithValue("@ContentType", attachmentMeta["ContentType"]);
                        cmd.Parameters.AddWithValue("@Description", String.IsNullOrEmpty(attachmentMeta["Description"]) ? DBNull.Value : attachmentMeta["Description"]);
                        cmd.ExecuteNonQuery();
                    }
                    assetCounter++;
                }
                catch
                {
                    continue;
                }
            }
            return assetCounter;
        }
        static void Main(string[] args)
        {
            RallyRestApi restApi = new RallyRestApi(webServiceVersion: "v2.0");
            String apiKey = "_abc777";
            restApi.Authenticate(apiKey, "https://rally1.rallydev.com", allowSSO: false);
            String workspaceRef = "/workspace/123";
            String projectRef = "/project/134";

            DynamicJsonObject badDefect = new DynamicJsonObject();
            badDefect["Name"] = "bad defect " + DateTime.Now;
            badDefect["Project"] = projectRef;

            CreateResult createRequest = restApi.Create(workspaceRef, "Defect", badDefect);
            badDefect = restApi.GetByReference(createRequest.Reference, "FormattedID", "Project");
            Console.WriteLine(badDefect["FormattedID"] + " " + badDefect["Project"]._refObjectName);
        }
    static void Main(string[] args)
    {
        RallyRestApi restApi = new RallyRestApi("*****@*****.**", "secret", "https://rally1.rallydev.com", "v2.0");
        DynamicJsonObject user = restApi.GetCurrentUser();
        String userRef = user["_ref"];
        String workspaceRef = "/workspace/11111"; //use valid workspace OID in your Rally
        String projectRef = "/project/12345";         //use valid project OID in your Rally

        DynamicJsonObject myStory = new DynamicJsonObject();
        myStory["Name"] = "my story";
        myStory["Project"] = projectRef;
        myStory["Owner"] = userRef;
        CreateResult createResult = restApi.Create(workspaceRef, "HierarchicalRequirement", myStory);
        myStory = restApi.GetByReference(createResult.Reference, "FormattedID", "Owner", "Project");
        Console.WriteLine(myStory["FormattedID"] + " " + myStory["Owner"]._refObjectName + " " + myStory["Project"]._refObjectName);
    }
        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/1011574887"; //non-default workspace of the user
            String projectRef = "/project/1791269111"; //a non-default project of the user (inside the workspace above)
            try
            {
                //create testset
                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);

                //find current iteration

                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);
                }
                else
                {
                    Console.WriteLine("No current iterations");
                }

            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }
        static void Main(string[] args)
        {
            RallyRestApi restApi = new RallyRestApi("*****@*****.**", "secret", "https://rally1.rallydev.com", "v2.0");
            String workspaceRef = "/workspace/11111"; //use valid workspace OID in your Rally
            String projectRef = "/project/12345";         //use valid project OID in your Rally
            String userRef = "/user/777";
            DynamicJsonObject d = new DynamicJsonObject();
            d["Name"] = "some bug";
            d["Project"] = projectRef;
            d["Owner"] = userRef;

            CreateResult createResult = restApi.Create(workspaceRef, "Defect", d);
            DynamicJsonObject defect = restApi.GetByReference(createResult.Reference, "FormattedID");
            Console.WriteLine(defect["FormattedID"]);

            //update defect
            defect["Description"] = "bad bug";
            OperationResult updateResult = restApi.Update(defect["_ref"], defect);
        }
        static void Main(string[] args)
        {
            //Initialize the REST API
            RallyRestApi restApi;
               restApi = new RallyRestApi("*****@*****.**", "topsecret", "https://rally1.rallydev.com", "v2.0");

            //Set our Workspace and Project scopings
            String workspaceRef = "/workspace/1111"; //use valid OID of your workspace
            String projectRef = "/project/2222";     //use valid OID of your project
            bool projectScopingUp = false;
            bool projectScopingDown = true;
            Request userRequest = new Request("User");

            userRequest.Workspace = workspaceRef;
            userRequest.Project = projectRef;
            userRequest.ProjectScopeUp = projectScopingUp;
            userRequest.ProjectScopeDown = projectScopingDown;

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

            userRequest.Query = new Query("UserName", Query.Operator.Equals, "*****@*****.**");

            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);
        }
        static void Main(string[] args)
        {
            RallyRestApi restApi;
            restApi = new RallyRestApi("*****@*****.**", "secret", "https://rally1.rallydev.com", "v2.0");

            String projectRef = "/project/123456";     //use valid OID of your project

            Request userRequest = new Request("User");

            userRequest.Query = new Query("UserName", Query.Operator.Equals, "*****@*****.**");

            QueryResult userResults = restApi.Query(userRequest);

            String userRef = userResults.Results.First()._ref;
            Console.WriteLine(userRef);

            DynamicJsonObject myStory = new DynamicJsonObject();
            myStory["Name"] = "a new story";
            myStory["Project"] = projectRef;
            myStory["Owner"] = userRef;
            CreateResult createResult = restApi.Create("HierarchicalRequirement", myStory);
            myStory = restApi.GetByReference(createResult.Reference, "FormattedID", "Owner", "Project");
            Console.WriteLine(myStory["FormattedID"] + " " + myStory["Owner"]._refObjectName + " " + myStory["Project"]._refObjectName);
        }
        static void Main(string[] args)
        {
            RallyRestApi restApi;

            String userName = "******";
            String userPassword = "******";

            String rallyURL = "https://rally1.rallydev.com";
            String wsapiVersion = "v2.0";

            restApi = new RallyRestApi(
                userName,
                userPassword,
                rallyURL,
                wsapiVersion
            );

            String workspaceRef = "/workspace/12352608129";
            String projectRef = "/project/12352608219";
            bool projectScopingUp = false;
            bool projectScopingDown = true;

            Request storyRequest = new Request("hierarchicalrequirement");
            storyRequest.Workspace = workspaceRef;
            storyRequest.Project = projectRef;
            storyRequest.ProjectScopeDown = projectScopingDown;
            storyRequest.ProjectScopeUp = projectScopingUp;

            storyRequest.Fetch = new List<string>()
                {
                    "Name",
                    "FormattedID",
                    "Attachments"
                };

            storyRequest.Query = new Query("FormattedID", Query.Operator.Equals, "US20");
            QueryResult queryResult = restApi.Query(storyRequest);
            DynamicJsonObject story = queryResult.Results.First();

            // Grab the Attachments collection
            Request attachmentsRequest = new Request(story["Attachments"]);
            QueryResult attachmentsResult = restApi.Query(attachmentsRequest);

            //Download the first attachment

            var myAttachmentFromStory = attachmentsResult.Results.First();
            String myAttachmentRef = myAttachmentFromStory["_ref"];
            Console.WriteLine("Found Attachment: " + myAttachmentRef);

            // Fetch fields for the Attachment
            string[] attachmentFetch = { "ObjectID", "Name", "Content", "ContentType", "Size" };

            // Now query for the attachment
            DynamicJsonObject attachmentObject = restApi.GetByReference(myAttachmentRef, "true");

            // Grab the AttachmentContent
            DynamicJsonObject attachmentContentFromAttachment = attachmentObject["Content"];
            String attachmentContentRef = attachmentContentFromAttachment["_ref"];

            // Lastly pull the content
            // Fetch fields for the Attachment
            string[] attachmentContentFetch = { "ObjectID", "Content" };

            // Now query for the attachment
            Console.WriteLine("Querying for Content...");
            DynamicJsonObject attachmentContentObject = restApi.GetByReference(attachmentContentRef, "true");
            Console.WriteLine("AttachmentContent: " + attachmentObject["_ref"]);

            String base64EncodedContent = attachmentContentObject["Content"];

            // File information
            String attachmentSavePath = "C:\\Users\\nmusaelian\\NewFolder";
            String attachmentFileName = attachmentObject["Name"];
            String fullAttachmentFile = attachmentSavePath + attachmentFileName;

            // Determine attachment Content mime-type
            String attachmentContentType = attachmentObject["ContentType"];

            // Specify Image format
            System.Drawing.Imaging.ImageFormat attachmentImageFormat;

            try
            {
                attachmentImageFormat = getImageFormat(attachmentContentType);
            }
            catch (System.ArgumentException e)
            {
                Console.WriteLine("Invalid attachment file format:" + e.StackTrace);
            }

            try
            {

                // Convert base64 content to Image
                Console.WriteLine("Converting base64 AttachmentContent String to Image.");

                // Convert Base64 string to bytes
                byte[] bytes = Convert.FromBase64String(base64EncodedContent);

                Image myAttachmentImage;
                using (MemoryStream ms = new MemoryStream(bytes))
                {
                    myAttachmentImage = Image.FromStream(ms);
                    // Save the image
                    Console.WriteLine("Saving Image: " + fullAttachmentFile);
                    myAttachmentImage.Save(fullAttachmentFile, System.Drawing.Imaging.ImageFormat.Jpeg);

                    Console.WriteLine("Finished Saving Attachment: " + fullAttachmentFile);
                }

            }
            catch (Exception e)
            {
                Console.WriteLine("Unhandled exception occurred: " + e.StackTrace);
                Console.WriteLine(e.Message);
            }

            Console.ReadKey();
        }