Could someone show some sample code that shows how one would attach an outside file to a version one request using the API Client? Assume we already have the filename and ticket id. I've found this done with the ObjectModel, but not any code that I could understand with the API client.
Asked
Active
Viewed 1,249 times
2
-
1Please mark MarkoPolo's answer as accepted, it works. – Jonathan Kittell Mar 30 '14 at 16:11
1 Answers
4
This sample attaches an rtf file to a project (scope). You would have to change the scope to the asset of your choice (likely story or defect). Here I have the OID, not the ID. You could query for the ID.Number (e.g. B-1234) if that is what you have.
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using VersionOne.SDK.APIClient;
namespace SampleAttachment
{
class Program
{
private static readonly string ApplicationURL = "https://myversionone/";
private static readonly string _username = "username";
private static readonly string _password = "passwerd";
private static readonly bool _integrated = false;
static void Main(string[] args)
{
//string file = args[0];
string file = @"C:\Users\MIRVIN\Desktop\Training Wheels\SampleAttachment\bin\Debug\testfile.rtf";
if (File.Exists(file))
{
Console.WriteLine("Uploading {0}", file);
string mimeType = MimeType.Resolve(file);
IMetaModel metaModel = new MetaModel(new V1APIConnector(ApplicationURL + "meta.v1/"));
IServices services = new Services(metaModel, new V1APIConnector(ApplicationURL + "rest-1.v1/", _username, _password, _integrated));
IAttachments attachments = new Attachments(new V1APIConnector(ApplicationURL + "attachment.img/", _username, _password, _integrated));
//cleanjeans scope
Oid attachmentContext = Oid.FromToken("Scope:5067", metaModel);
IAssetType attachmentType = metaModel.GetAssetType("Attachment");
IAttributeDefinition attachmentNameDef = attachmentType.GetAttributeDefinition("Name");
IAttributeDefinition attachmentContentDef = attachmentType.GetAttributeDefinition("Content");
IAttributeDefinition attachmentContentTypeDef = attachmentType.GetAttributeDefinition("ContentType");
IAttributeDefinition attachmentFileNameDef = attachmentType.GetAttributeDefinition("Filename");
Asset newAttachment = services.New(attachmentType, attachmentContext);
newAttachment.SetAttributeValue(attachmentNameDef, "New Attachment");
newAttachment.SetAttributeValue(attachmentContentDef, string.Empty);
newAttachment.SetAttributeValue(attachmentContentTypeDef, mimeType);
newAttachment.SetAttributeValue(attachmentFileNameDef, file);
services.Save(newAttachment);
//Setup and attach the payload
string attachmentKey = newAttachment.Oid.Key.ToString();
int buffersize = 4096;
using (FileStream input = new FileStream(file, FileMode.Open, FileAccess.Read))
{
using (Stream output = attachments.GetWriteStream(attachmentKey))
{
byte[] buffer = new byte[buffersize];
for (; ; )
{
int read = input.Read(buffer, 0, buffersize);
if (read == 0)
break;
output.Write(buffer, 0, read);
}
}
}
attachments.SetWriteStream(attachmentKey, mimeType);
Console.WriteLine("{0} uploaded", file);
}
else
Console.WriteLine("{0} does not exist", file);
}
}
}

Mark Irvin
- 830
- 1
- 6
- 16
-
I had to change V1APIConnector to VersionOneAPIConnector and for the credentials just do 'NetworkCredential cred = new NetworkCredential();' then pass them to 'services' and 'attachments' and it works perfectly. – Jonathan Kittell Mar 30 '14 at 16:10
-
Hey, i have an scenario where i load the attachment from an exchange email service and get the content in a byte array, but how do i write that byte array to an attachment in V1?. Thanks. – Thunder Aug 25 '15 at 07:59
-
@thunder You should be able to use the majority of the code above. Follow the same steps, first creating a container asset then streaming the content into it. Where it says int buffersize = 4096? That should be the size of your byte array. The line byte[] buffer = new byte{buffer size] is where you are creating the payload to be sent to VersionOne. Make sure you set the mimeType to match the proper file type. – Mark Irvin Aug 25 '15 at 22:09