How to programmatically pull data in csv format from a ServiceNow account using C# on .NET 4.8
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-31-2022 07:57 AM
I'm new to C# and WebRequest.
I'm trying to figure out how to programmatically pull data in csv format from a ServiceNow account using C# on .NET 4.8. The login to ServiceNow automatically authenticates. I believe it uses OAuth. When accessing the url a Terms of Acceptance dialogue box pops up. I'm not sure exactly how to deal with how to authenticate when OAuth is being used and when a Terms of Acceptance dialog box pops up. I am able to download the table when I test the code with a developer ServiceNow instance that uses basic authentication where I can provide the user and password NetworkCredentials.
I've tried the code below but of course I end up receiving -- GetResponse() ... 401 not authorized exception thrown when retrieving the response
The url is something like this - "https://xxx.services-now.com/tablename.do?CSV"
Sample code or guidance on how to do this would be great.
Here's the code I have thus far:
using System; using System.IO; using System.Net; using System.Configuration; namespace ExportToCSV { internal class Program { static void Main(string[] args) { var localDestination = ConfigurationManager.AppSettings["localdestination"]; var svcnowListUrl = ConfigurationManager.AppSettings["svcnowlisturl"]; int read = DownloadFile(svcnowListUrl, localDestination); } public static int DownloadFile(String url, String localFilename) { int bytesProcessed = 0; Stream remoteStream = null; Stream localStream = null; WebResponse response = null; try { WebRequest request = WebRequest.Create(url); request.Credentials = credentialCache.DefaultCredentials; request.Timeout = 120000; if (request != null) { response = request.GetResponse(); if (response != null) { remoteStream = response.GetResponseStream(); localStream = File.Create(localFilename); byte[] buffer = new byte[1024]; int bytesRead; do { bytesRead = remoteStream.Read(buffer, 0, buffer.Length); localStream.Write(buffer, 0, bytesRead); bytesProcessed += bytesRead; } while (bytesRead > 0); } } } catch (Exception e) { Console.WriteLine(e.Message); } finally { if (response != null) response.Close(); if (remoteStream != null) remoteStream.Close(); if (localStream != null) localStream.Close(); } return bytesProcessed; } } }
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-31-2022 11:55 PM
Hi, if you are accessing the platform remotely to extract data then I would recommend that you utilize one of the OOB API's. Attempting to extract unfiltered data from a table via URL, may result in performance issues with your instance and may also require that you set extremely large export file size, which all users could then leverage.
From your example you appear to be missing the reference to the table list IE
incident_list.do?CSV
Export directly from a URL (servicenow.com)