Find your people. Pick a challenge. Ship something real. The CreatorCon Hackathon is coming to the Community Pavilion for one epic night. Every skill level, every role welcome. Join us on May 5th and learn more here.

C# example: posting CSV or Excel files directly to an import set

WilliamHazelrig
Giga Contributor

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);

      }

  }

}

10 REPLIES 10

Hi Jace,



I am facing similar issue


Let us know what was the fix you applied