It's all about the answers!

Ask a question

Issue with programmatic link creation in RTC 4.0

Karthik Krishnan (8825115162) | asked Mar 01 '13, 9:23 a.m.

I am using this example to create links between two work items.

How manyever times I run this sample applicaiotn that many times the same link is created. Normally this is not possible in GUI but via the script it is creating same link many times.

Here is my code snippet

public class LinkingWI {
  * @param args
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  System.out.println("Platform started successfully");
   try {
   IProgressMonitor monitor = new SysoutProgressMonitor();
    ITeamRepository repo = Login.login(monitor);
    System.out.println("Logged in successfully");
    Integer iSource = 22;
    Integer iTarget = 23;
    IWorkItemClient workItemClient = (IWorkItemClient) repo.getClientLibrary(IWorkItemClient.class);
          IWorkItem workItem = workItemClient.findWorkItemById(iSource, IWorkItem.FULL_PROFILE, null);            
          IWorkItemWorkingCopyManager wcm = workItemClient.getWorkItemWorkingCopyManager();
          wcm.connect(workItem, IWorkItem.FULL_PROFILE, null);
             try {
                 WorkItemWorkingCopy wc = wcm.getWorkingCopy(workItem);
                 System.out.println( "inside");
                 //System.out.println( wc.getWorkItem().getCreator().toString());
                 System.out.println( wc.getWorkItem().getHTMLSummary().toString());
                 System.out.println( "Adding contentstarts");
                 System.out.println( "Summary - " + wc.getWorkItem().getHTMLSummary().toString()); 
                 System.out.println( "Linking starts");
                 //IWorkItemClient oppositeEorkItemClient = (IWorkItemClient) repo.getClientLibrary(IWorkItemClient.class);
               IWorkItem oppositeWorkItem = workItemClient.findWorkItemById(iTarget, IWorkItem.FULL_PROFILE, null);            
               IWorkItemWorkingCopyManager owcm = workItemClient.getWorkItemWorkingCopyManager();
               owcm.connect(workItem, IWorkItem.FULL_PROFILE, null);
             //  WorkItemWorkingCopy owc = owcm.getWorkingCopy(oppositeWorkItem);             
                 WorkItemReferencesModification oWIRFM = new WorkItemReferencesModification(oppositeWorkItem);
                 oWIRFM.execute(wc, monitor);
                 System.out.println( "Linking ends");
                 System.out.println( "save starts");
     IDetailedStatus s =;
     if (!s.isOK()) {
      //throw new TeamRepositoryException("Error saving work item",s.getException());
      System.out.println(" Skipping Work Item: " +  workItem.getId() + "\n");      
      System.out.println("INFO - Modified work item: " + workItem.getId() + ".\n"); 
     System.out.println( "save ends");
             catch(Exception e) {
              System.out.println( "Workitem not found");
  } catch (TeamRepositoryException e) {
   System.out.println( "Error found");
   System.out.println("Platform stopped successfully");
 private static class WorkItemReferencesModification extends WorkItemOperation {
  private IWorkItemHandle fOpposite;
  public WorkItemReferencesModification(IWorkItemHandle opposite) {
      super("Modifying Work Item References",IWorkItem.FULL_PROFILE);
      fOpposite = opposite;
  protected void execute(WorkItemWorkingCopy workingCopy, IProgressMonitor monitor) throws TeamRepositoryException {
      // Create a new reference to the opposite item
      IItemReference reference = IReferenceFactory.INSTANCE.createReferenceToItem(fOpposite);
      // Add the new reference using a specific work item end point
      //workingCopy.getReferences().add(WorkItemEndPoints.BLOCKS_WORK_ITEM, reference);
      workingCopy.getReferences().add(WorkItemEndPoints.RESOLVES_WORK_ITEM, reference);
      System.out.println( "Setting REsolved by link ends");

Kindly note the login is done in different class.

Any tips on how to avoid this?

Accepted answer

permanent link
Ralph Schoon (63.0k33645) | answered Mar 01 '13, 10:36 a.m.
Hi, I would implement a check and test if the link is already there. That is what the UI does, I think.
Karthik Krishnan selected this answer as the correct answer

Karthik Krishnan commented Mar 01 '13, 10:37 a.m.

Hi Ralph, 

Can you give me some heads up on how I can check for an existing link

Many thanks for the useful blogpost

Ralph Schoon commented Mar 01 '13, 10:47 a.m.

You get the references for the endpoint you are interested in. Then you have to iterate it and check if the work item that you want to link to is already there. shows how to iterate parent child links. I need to code something like that up too.....

Your answer

Register or to post your answer.