How to create new workitem with workitem type attributes?
7 answers
This may helps. This is the sample code I wrote as "server side" plugin. Not sure if it works for client as well. test1, test2, test3 is custom attributes (integer type), and it calculate test3 = test1 + test3, then set the value to test3 via workitem.setValue (IAttibute, Value).
// Obtain project area and workitem server.
IProjectAreaHandle iProjectArea = workItem.getProjectArea();
IWorkItemServer wiServer = getService(IWorkItemServer.class);
// Obtain custom attribute objects, and their values
//
IAttribute test1 = wiServer.findAttribute(iProjectArea, "test1", null);
IAttribute test2 = wiServer.findAttribute(iProjectArea, "test2", null);
IAttribute test3 = wiServer.findAttribute(iProjectArea, "test3", null);
Integer test1Value = (Integer) workItem.getValue(test1);
Integer test2Value = (Integer) workItem.getValue(test2);
// Calculate custom atrribute value based on other attribute values.
Integer test3Value = test1Value + test2Value;
// Update custom attributes.
// com.ibm.customatributes.test3
workItem.setValue(test3, (Object) test3Value);
// Obtain project area and workitem server.
IProjectAreaHandle iProjectArea = workItem.getProjectArea();
IWorkItemServer wiServer = getService(IWorkItemServer.class);
// Obtain custom attribute objects, and their values
//
IAttribute test1 = wiServer.findAttribute(iProjectArea, "test1", null);
IAttribute test2 = wiServer.findAttribute(iProjectArea, "test2", null);
IAttribute test3 = wiServer.findAttribute(iProjectArea, "test3", null);
Integer test1Value = (Integer) workItem.getValue(test1);
Integer test2Value = (Integer) workItem.getValue(test2);
// Calculate custom atrribute value based on other attribute values.
Integer test3Value = test1Value + test2Value;
// Update custom attributes.
// com.ibm.customatributes.test3
workItem.setValue(test3, (Object) test3Value);
Can you please be more specific on what you want to do? In General attributes are all handled alike. Based on what data you have the calls would look different.
It is a difference if you want to create a parent or other relationship or if you want to set a value or add something to a list.
If you have an ID you can search for the attribute e.g. using
There are other methods to get the custom attributes as a list from the work item. Look at the methods provided.
If you have that you can get to data about the Attribute, including types. You need to create objects of the type (depends on the type how to create values) you store and can then set the value similar to the other attributes. I haven't tried Attributes of type WorkItemList but assume there is a difference, where you get the list and add the value there.
It is a difference if you want to create a parent or other relationship or if you want to set a value or add something to a list.
If you have an ID you can search for the attribute e.g. using
IAttribute customAttri = workItemClient.findAttribute(fProjectArea,fCustomAttriAttributeID, null);
There are other methods to get the custom attributes as a list from the work item. Look at the methods provided.
If you have that you can get to data about the Attribute, including types. You need to create objects of the type (depends on the type how to create values) you store and can then set the value similar to the other attributes. I haven't tried Attributes of type WorkItemList but assume there is a difference, where you get the list and add the value there.
Hi,
find my workiteminitialization code below. Its working fine.
But i want to add value to the attribute of type workitem.
Example : Product Code is the attribute of the type workitem.
How can i set the value to this attribute.
Value of this attribute is must be another one workitem.
public class WorkItemInitialization extends AbstractWorkItemOperation {
private String fSummary;
private ICategoryHandle fCategory;
private IAttribute iattribute;
public WorkItemInitialization(String summary, ICategoryHandle category, IAttribute ia,IWorkItemClient workItemClient) {
super("Initializing Work Item");
fSummary = summary;
fCategory = category;
iattribute = ia;
this.workItemClient = workItemClient;
}
protected void execute(WorkItemWorkingCopy workingCopy,IProgressMonitor monitor) throws TeamRepositoryException {
IWorkItem workItem = workingCopy.getWorkItem();
workItem.setHTMLSummary(XMLString.createFromPlainText(fSummary));
java.util.Date date= new java.util.Date();
String str_date="08-05-2012";
DateFormat formatter ;
Date date1 = null ;
formatter = new SimpleDateFormat("dd-M-yy");
try {
date1 = (Date)formatter.parse(str_date);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
java.sql.Timestamp timeStampDate = new Timestamp(date1.getTime());
System.out.println("Today is " +timeStampDate);
workItem.setCategory(fCategory);
workItem.setDueDate(timeStampDate);
IAttribute attr = workItemClient.findAttribute(workingCopy.getWorkItem().getProjectArea(),"Development.Planned_Start_Date", monitor);
if (attr != null) {
workItem.setValue(attr,timeStampDate);
}
attr = workItemClient.findAttribute(workingCopy.getWorkItem().getProjectArea(),"Development.Planned_Target_Date", monitor);
if (attr != null) {
workItem.setValue(attr,timeStampDate);
}
}
}
Thanks
Glory
find my workiteminitialization code below. Its working fine.
But i want to add value to the attribute of type workitem.
Example : Product Code is the attribute of the type workitem.
How can i set the value to this attribute.
Value of this attribute is must be another one workitem.
public class WorkItemInitialization extends AbstractWorkItemOperation {
private String fSummary;
private ICategoryHandle fCategory;
private IAttribute iattribute;
public WorkItemInitialization(String summary, ICategoryHandle category, IAttribute ia,IWorkItemClient workItemClient) {
super("Initializing Work Item");
fSummary = summary;
fCategory = category;
iattribute = ia;
this.workItemClient = workItemClient;
}
protected void execute(WorkItemWorkingCopy workingCopy,IProgressMonitor monitor) throws TeamRepositoryException {
IWorkItem workItem = workingCopy.getWorkItem();
workItem.setHTMLSummary(XMLString.createFromPlainText(fSummary));
java.util.Date date= new java.util.Date();
String str_date="08-05-2012";
DateFormat formatter ;
Date date1 = null ;
formatter = new SimpleDateFormat("dd-M-yy");
try {
date1 = (Date)formatter.parse(str_date);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
java.sql.Timestamp timeStampDate = new Timestamp(date1.getTime());
System.out.println("Today is " +timeStampDate);
workItem.setCategory(fCategory);
workItem.setDueDate(timeStampDate);
IAttribute attr = workItemClient.findAttribute(workingCopy.getWorkItem().getProjectArea(),"Development.Planned_Start_Date", monitor);
if (attr != null) {
workItem.setValue(attr,timeStampDate);
}
attr = workItemClient.findAttribute(workingCopy.getWorkItem().getProjectArea(),"Development.Planned_Target_Date", monitor);
if (attr != null) {
workItem.setValue(attr,timeStampDate);
}
}
}
Thanks
Glory
Hi,
Thanks for your response. I have found the solution.
check my code blow.
public class WorkItemInitialization extends AbstractWorkItemOperation {
private String fSummary;
private ICategoryHandle fCategory;
private IAttribute iattribute;
IWorkItem iparent;
IWorkItemHandle target;
public WorkItemInitialization(String summary, ICategoryHandle category, IWorkItem parent, IWorkItemHandle parent2, IAttribute ia,IWorkItemClient workItemClient) {
super("Initializing Work Item");
fSummary = summary;
fCategory = category;
iattribute = ia;
iparent = parent;
target = parent2;
this.workItemClient = workItemClient;
}
protected void execute(WorkItemWorkingCopy workingCopy,IProgressMonitor monitor) throws TeamRepositoryException {
IWorkItem workItem = workingCopy.getWorkItem();
workItem.setHTMLSummary(XMLString.createFromPlainText(fSummary));
java.util.Date date= new java.util.Date();
String str_date="08-05-2012";
DateFormat formatter ;
Date date1 = null ;
formatter = new SimpleDateFormat("dd-M-yy");
try {
date1 = (Date)formatter.parse(str_date);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
java.sql.Timestamp timeStampDate = new Timestamp(date1.getTime());
System.out.println("Today is " +timeStampDate);
workItem.setCategory(fCategory);
workItem.setDueDate(timeStampDate);
IAttribute attr = workItemClient.findAttribute(workingCopy.getWorkItem().getProjectArea(),"Development.Planned_Start_Date", monitor);
if (attr != null) {
workItem.setValue(attr,timeStampDate);
}
attr = workItemClient.findAttribute(workingCopy.getWorkItem().getProjectArea(),"Development.Planned_Target_Date", monitor);
if (attr != null) {
workItem.setValue(attr,timeStampDate);
}
attr = workItemClient.findAttribute(workingCopy.getWorkItem().getProjectArea(),"Development.parent_dev", monitor);
System.out.println("------------------------------");
System.out.println(attr);
System.out.println("------------------------------");
if (attr != null) {
workItem.setValue(attr,iparent );
}
setParent(workingCopy,iparent);
}
private void setParent(WorkItemWorkingCopy wc, IWorkItem parent) {
IEndPointDescriptor referenceType= WorkItemEndPoints.PARENT_WORK_ITEM;
IReference reference= WorkItemLinkTypes.createWorkItemReference(parent);
wc.getReferences().add(referenceType, reference);
}
}
Thanks
Glory
Thanks for your response. I have found the solution.
check my code blow.
public class WorkItemInitialization extends AbstractWorkItemOperation {
private String fSummary;
private ICategoryHandle fCategory;
private IAttribute iattribute;
IWorkItem iparent;
IWorkItemHandle target;
public WorkItemInitialization(String summary, ICategoryHandle category, IWorkItem parent, IWorkItemHandle parent2, IAttribute ia,IWorkItemClient workItemClient) {
super("Initializing Work Item");
fSummary = summary;
fCategory = category;
iattribute = ia;
iparent = parent;
target = parent2;
this.workItemClient = workItemClient;
}
protected void execute(WorkItemWorkingCopy workingCopy,IProgressMonitor monitor) throws TeamRepositoryException {
IWorkItem workItem = workingCopy.getWorkItem();
workItem.setHTMLSummary(XMLString.createFromPlainText(fSummary));
java.util.Date date= new java.util.Date();
String str_date="08-05-2012";
DateFormat formatter ;
Date date1 = null ;
formatter = new SimpleDateFormat("dd-M-yy");
try {
date1 = (Date)formatter.parse(str_date);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
java.sql.Timestamp timeStampDate = new Timestamp(date1.getTime());
System.out.println("Today is " +timeStampDate);
workItem.setCategory(fCategory);
workItem.setDueDate(timeStampDate);
IAttribute attr = workItemClient.findAttribute(workingCopy.getWorkItem().getProjectArea(),"Development.Planned_Start_Date", monitor);
if (attr != null) {
workItem.setValue(attr,timeStampDate);
}
attr = workItemClient.findAttribute(workingCopy.getWorkItem().getProjectArea(),"Development.Planned_Target_Date", monitor);
if (attr != null) {
workItem.setValue(attr,timeStampDate);
}
attr = workItemClient.findAttribute(workingCopy.getWorkItem().getProjectArea(),"Development.parent_dev", monitor);
System.out.println("------------------------------");
System.out.println(attr);
System.out.println("------------------------------");
if (attr != null) {
workItem.setValue(attr,iparent );
}
setParent(workingCopy,iparent);
}
private void setParent(WorkItemWorkingCopy wc, IWorkItem parent) {
IEndPointDescriptor referenceType= WorkItemEndPoints.PARENT_WORK_ITEM;
IReference reference= WorkItemLinkTypes.createWorkItemReference(parent);
wc.getReferences().add(referenceType, reference);
}
}
Thanks
Glory