C# example: posting CSV or Excel files directly to an import set
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-10-2015 07:52 AM
There are PERL and Java code samples in the Wiki for posting CSV or Excel files directly to an import set, but at present there are not any code samples for doing this with .Net (C#, VB, etc.). I am experimenting with HttpWebClient and with HttpWebRequest to try to find the correct method of making this work, but to date my best result has been an Error from the Import Processor reporting a java.lang.NullPointerException, which tells me my code uploaded something - but it wasn't the expected content. Has anyone else attempted this and made it work?
Below is a simplified version of my code from that attempt:
class CSVposter
{
private System.Net.HttpWebRequest request;
private string FilePath;
public CSVposter(string SNinstance, string ImpTable, string FilePath, string UID, string Pwd)
{
this.FilePath = FilePath;
string ImportURL = "https://" + SNinstance + ".service-now.com/sys_import.do?sysparm_import_set_tablename="
+ ImpTable + "&sysparm_transform_after_load=true&uploadfile=" + FilePath;
this.request = (HttpWebRequest) System.Net.WebRequest.Create(ImportURL);
this.request.Credentials = new System.Net.NetworkCredential(UID, Pwd);
this.request.PreAuthenticate = true;
this.request.Method = "POST";
}
public void send()
{
FileStream fs = new System.IO.FileStream(this.FilePath, FileMode.Open, FileAccess.Read);
StreamReader src = new StreamReader(fs, System.Text.Encoding.ASCII);
byte[] Content = System.Text.Encoding.ASCII.GetBytes(src.ReadToEnd());
Stream dest = this.request.GetRequestStream();
dest.Write(Content, 0, Content.length);
dest.Close();
try
{
System.Net.WebResponse resp = this.request.GetResponse();
}
catch (WebException Ex)
{
Console.WriteLine(Ex.Message);
}
}
}
- Labels:
-
Integrations
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-10-2015 07:54 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-10-2015 12:44 PM
Thank you, Mike. That tutorial actually got me started with one of our earliest ServiceNow integrations back in 2011, and we still have a couple of Web Services -based integrations written in C# running against our ServiceNow instances. Unfortunately, I believe the problem in this case is the format of the upload, which is not covered in that tutorial.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-10-2015 08:48 AM
Hi William,
It may be helpful to know that the functionality you wish to implement is described here:
http://httpkit.com/resources/HTTP-from-the-Command-Line/
Section "POST HTML Multipart / File Forms"
As a first step you could use any 3rd party REST tool that supports file attachments and simply POST to the URL as described in the wiki article:
Most 3rd party REST tools will allow you to view the headers and body that was sent.
Best Regards
Tony
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-10-2015 12:45 PM
Thank you, Tony. I will see if a combination of Fiddler and CURL (assuming that I can get the upload working in CURL) will give me the necessary clues to figure out what's wrong with the format of what I'm sending from the C# program.