Inserting images in text fields

The following example describes how to add an image to a multi-line text field in an issue. You can perform similar steps to insert images in test cases, manual test runs, requirements, and requirement documents.

Note:  You can only add images to multi-line text fields.

1. Localize external namespaces.

using System;

using System.Collections.Generic;

using System.Text;

2. Set up the namespace, provide the login information, and define the project name.

namespace InlineAttachment

{

class InlineAttachment

{

static void Main (string[] args)

{

long cookie = -1;

ttsoapcgi ttsdk = null;

 

try

{

string username = "username";

string password = "password";

string ProjectName = "ProjectName";

3. Create an instance of the ttsoapcgi class, which is generated from the ttsoapcgi.wsdl file.

ttsdk = new ttsoapcgi();

4. Set the URL that the ttsdk object connects to if you are not using the default URL from the WSDL file.

ttsdk.Url = "http://localhost:80/scripts/ttsoapcgi.exe";

5. Select the Helix ALM project to connect to and set proj to reference it.

CProject[] projects = ttsdk.getProjectList(username, password);

CProject proj = null;

 

foreach (CProject p in projects)

{

if (p.database.name.Equals(projectName))

{

proj = p;

break;

}

}

6. Log in to the project.

cookie = ttsdk.ProjectLogon(proj, username, password);

7. Enable formatted text support for multi-line fields.

ttsdk.formattedTextSupport(cookie, true);

8. Use ttsoapcgi.editDefect() or ttsoapcgi.editDefectByRecordId() to retrieve and lock the record.

This prevents other clients or SOAP applications from editing the record. To search for an issue by summary, set the issue number to 0 and the summary to a non-empty string.

CDefect def = ttsdk.editDefect(cookie, 0, "Example Defect 1, With Formatted Text.", false);

9. Modify the HTML to add an <img> tag and set the image src attribute to the image name you are adding.

def.reportedbylist[0].comments =

"<p align=\"center\"><span style=\" font-size:24pt; color:#3366ff;\">Welcome To Helix ALM SDK Development</span></p><hr /><p> </p>\r\n"

+ "<p><img src=\"Example2.png\" />This is a paragraph of regular text. Here's a link to the <a href=\"https://www.perforce.com/products/helix-alm\"><span style=\" color:#0000ff;\"><u>Helix ALM page</u></span></a>.</p>\r\n"

+ "<p> </p>\r\n"

+ "<p>A Table:</p>\r\n"

+ "<p> </p>\r\n"

+ "<table border=\"1\" width=\"600\" cellspacing=\"0\" cellpadding=\"3\" bgcolor=\"#faebd7\"><tr><td><p>A list:</p>\r\n"

+ "<p> </p>\r\n"

+ "<ol><li>First item</li>\r\n"

+ "<li>Second Item</li>\r\n"

+ "<li>Third item</li>\r\n"

+ "<li>Link to <a href=\"https://www.perforce.com/products/helix-alm\"><span style=\" color:#0000ff;\"><u>Helix ALM Page</u></span></a> on perforce.com</li>\r\n"

+ "</ol></td> <td><p> Welcome to the Helix ALM SDK. The power of the SDK gives you the ability to create applications which can make almost any change to data that a human\r\n"

+ "using the full Helix ALM Client can. This means you can create automated processes to update the database, based on input from a variety of sources, such as user submissions\r\n"

+ "in an application, or info in third-party databases.</p></td></tr>\r\n"

+ "</table><p>\r\n"

+ "<br /> </p>";

10. Retrieve the image file data and add it to the issue reported by record inline attachment list.

string filename = "Example2.png";

CFileAttachment file = new CFileAttachment();

file.mstrFileName = filename;

11. Open the file attach and read the contents into the CFileAttachment object.

System.IO.FileStream fs = new System.IO.FileStream(filename, System.IO.FileMode.Open, System.IO.FileAccess.Read);

System.IO.BinaryReader reader = new System.IO.BinaryReader(fs);

file.mpFileData = reader.ReadBytes((int)fs.Length);

reader.Close();

fs.Close();

12. Add the object to the reported by record inline attachment list.

def.reportedbylist[0].commentsInlineAttachList = new CFileAttachment[1];

def.reportedbylist[0].commentsInlineAttachList[0] = file;

13. Save the changes.

ttsdk.saveDefect(cookie, def);

14. Finish editing the issue and catch exceptions.

Console.WriteLine ("Finished editing defect");

}

 

catch (Exception e)

{

Console.WriteLine("An exception occurred:");

Console.WriteLine(e.Message);

}

15. End the session and log out of the Helix ALM SDK.

finally

{

if (ttsdk != null && cookie != -1)

{

ttsdk.DatabaseLogoff(cookie);

}

Console.WriteLine("Press any key to exit.");

Console.ReadKey();

}