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
11-02-2017 03:23 AM
Hi Jace,
I am facing similar issue
Let us know what was the fix you applied