Windows application to create Incident with attachment using SOAP web service

General Add comments
by:

Nowadays it’s getting more common to use Software as Services (SaaS) applications than traditional applications. There are enough advantages for using SaaS but you need to consider the (obvious?) circumstances. Most obvious circumstance is that you must have a connection to the Internet in order for your SaaS applications to function. Therefore I investigated the possibility of a traditional Windows Front End application. In order to communicate with Service-now.com you have to establish a connection through web services. These phases are explained on the SNC wiki step by step.

In this blog post I would like to talk about uploading an attachment through web services. Normally you can upload an attachment by specifying the folder path in the programming code. For security reasons this does not work in Service-now.com since foreign programming code is not allowed on the Service-now.com servers. But there is a workaround called Base64. This function encodes the attachment file on the client side and through a directional SOAP message is able to communicate with Service-now.com.

The attachment creator I’ve build is programmed in C#. Most obvious tool you can use is Microsoft Visual Studio. You can download the Express version for free on the Microsoft website. Through this blog post I will provide you the basic elements to program this application. Programming an application such as mine isn’t that difficult if you have the right fundaments. Therefore I assume you are familiar with programming in C#.

 

Step 1: Change ElementFormDefault Property in Service-now.com

You have to set the elementFormDefault property to false.
Go to System Properties >> Web Services

 

 

Step 2: Create method for Base64 function in C#

You will have to create a method for the Base64 function – which we will use later on.
 


private string base64;

public string FileToBase64String(string path) {
FileStream fileStream = newFileStream(path, FileMode.Open, FileAccess.Read);
byte[] buffer = new byte[fileStream.Length];
fileStream.Read(buffer, 0, (int)fileStream.Length);

MemoryStream memory = newMemoryStream(buffer);
string base64 = Convert.ToBase64String(memory.ToArray());
memory.Close();

return base64;
}

Step 3: Define Base64 Button method in C#

Define Base64 in your Button method as mentioned below:

private void buttonBrowse_Click(object sender, EventArgs e) {

OpenFileDialog openDlg = newOpenFileDialog();
openDlg.Filter = “All Supported Files (All Files (*.*)|*.*”;
if (openDlg.ShowDialog(this) == DialogResult.OK) {
Cursor = Cursors.WaitCursor;
textBoxAttachFile.Text = openDlg.FileName;

try {
base64 = FileToBase64String(openDlg.FileName);
} catch (Exception) {
MessageBox.Show(this, string.Format(“An error occurred whilst converting to base-64.”),
Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
Cursor = Cursors.Default;
}
}

 

Step 4: Program a class to define Mime Type in C#

Program a class to define the Mime Type of an attachment file.

Step 5: Code to push attachment into Service-now.com in C#

This piece of code will push the attachment into Service-now.com:

try {
insertIncident.caller_id = “Joe Employee”;
insertIncident.short_description = “Incident with Attachment”;
responseIncident = soapClientIncident.insert(insertIncident);
} catch (Exception error) {

MessageBox.Show(error.Message);
} finally {

insertAttachment.agent = “AttachmentCreator”;
insertAttachment.topic = “AttachmentCreator”;
insertAttachment.source = “incident:” + responseIncident.sys_id;

var getAttachFile = textBoxAttachFile.Text;
var inputAttach = Path.GetFileName(getAttachFile;

var mime = MimeType.getMimeFromFile(getAttachFile);

insertAttachment.name = inputAttach + “:” + mime;
insertAttachment.payload = base64;

try {
 responseAttachment = soapClientAttachment.insert(insertAttachment);
MessageBox.Show(“Your Incident with an attachment has been created: “ + responseIncident.number + “\nCheck your Incident on http://demo.service.com”);
} catch (Exception error) {
MessageBox.Show(error.Message);
}
}

 

Step 6: Test your work

If you’ve followed the instructions as they were mentioned above you can now create an attachment within an incident in Service-now.com.
 

Click HERE to download a pre-compiled demo application as discussed in this blog.

The provided zipfile contains a compiled executable (AttachmentCreator.exe) that:

  • Allows you to select a file that you would like to upload/attach;
  • Creates an Incident record with the attachment supplied in the Service-now.com demo instance;
  • Responds with the Incident number of the created Incident ticket that holds the attachment.

Please make sure that – for testing this demo application – Step 1 as mentioned above has been performed.

Please don’t hesitate to contact me if you have any questions. You can reach me via email on: jeffrey.meijer@2e2.nl

Leave a Reply