jacobandrews - Thursday, August 22, 2013 4:48 AM:
I have a problem while trying to attach an existing file in the vault to an existing document.
//get Document Item
Item qryItem1 = MyInnovator.newItem("Document", "get");
qryItem1.setAttribute("select", "item_number");
qryItem1.setProperty("item_number", "5003");
qryItem1.setPropertyCondition("item_number", "eq");
Item results1 = qryItem1.apply();
int count1 = results1.getItemCount();
Item Doc1 = results1.getItemByIndex(0);
int iLstat1 = Doc1.getLockStatus();
if (iLstat1 == 0)
Doc1.lockItem();
Item relItem1 = MyInnovator.newItem("Document File", "add");
relItem1.setProperty("source_id", id1);
Item FileItem = relItem1.createRelatedItem("File", "add");
FileItem.setProperty("filename", itemNumber1);
FileItem.attachPhysicalFile("c:\temp\rrrr-test.txt"); /// It crashes at this line
Item res2 = relItem1.apply();
if (res2.isError())
{
Console.WriteLine(res1.getErrorString());
}
Doc1.addRelationship(relItem1);
///////////////////////////////////////////////////////////////////////////////////
it gives an error that the ITEM ID is not set at the statement : FileItem.attachPhysicalFile("c:\temp\rrrr-test.txt");
Harsha - Thursday, August 22, 2013 8:52 AM:
Hi jacobandrews,
Is this your entire code fragment? You have missed declaring some variable i.e id1, itemNumber1,res2,res1. And your code is not properly formatted, where 'filename' should have an extension, related item should be set as well.
1)If you are trying to attach a file which already exists in the Vault so it exists as File item, then
Innovator inn = this.getInnovator();
Item qryItem1 = inn.newItem("Document", "get");
qryItem1.setAttribute("where","[DOCUMENT].item_number like '5003' "); //item_number is unique so might be only one result, no need to loop
qryItem1 = qryItem1.apply();
Item file = inn.getItemById("File","D68843A4EA6744BB89FC11E6F3AA02BC"); //File item's id
file = file.apply();
string file_name = file.getProperty("filename");
Item relItem1 = this.newItem("Document File","add");
qryItem1.addRelationship(relItem1); //add relationship
relItem1.setRelatedItem(file); // related item
Item results = qryItem1.apply();
if (results.isError()) {
inn.newError(results.getErrorDetail());
}
return this;
2) if you are adding the file which does n' t exist in the vault, then
Item qryItem1 = this.newItem("Document","get");
qryItem1.setAttribute("where","[DOCUMENT].item_number like '5003' ");
qryItem1 = qryItem1.apply();
Item fileItem = this.newItem("File","add");
fileItem.setProperty("filename","test1.pdf");
fileItem.attachPhysicalFile("C:\temp\test1.pdf");
fileItem = fileItem.apply();
Item relItem1 = this.newItem("Document File","add");
qryItem1.addRelationship(relItem1);
relItem1.setRelatedItem(fileItem);
Item results = qryItem1.apply();
if (results.isError()) {
inn.newError(results.getErrorDetail());
}
return results;
Thanks-Harsha