BillMartin
Giga Sage

If you’ve ever needed a quick export of incident data and didn’t want to babysit a manual UI download, you already know the pain. You can export from list views in ServiceNow, but that approach is manual, easy to repeat incorrectly, and hard to schedule or automate.

 

This walkthrough shows you how to export incidents from ServiceNow using a simple C# .NET console app that pulls real-time records from a ServiceNow developer instance (PDI) and writes them to a CSV file on your local machine. You also get logging, a query template you can swap out, and a clean shutdown that closes the connection properly.

 

You’ll see the programmatical version of exporting records, the same idea as a UI export, but built for repeatable runs.

 

 

 

Why use a C# console app for ServiceNow data exports?

ServiceNow already lets you export from the UI, so why add code?

 

Because teams often need ad hoc exports based on encoded queries, and they need them to be repeatable. A console app is a practical middle ground. It’s light, easy to run, and close to production-ready if you want to deploy it inside your environment later.

 

This style of export is useful when you need:

 

  • Reporting extracts that you can run on demand
  • Testing and validation (grab a set of records, compare expected values)
  • Lightweight automation without building a full app or integration platform

 

UI exports work fine for one-offs. They break down when you want consistent results across runs, the same filters every time, and a file saved to the same place automatically.

 

If your goal is how to export incidents from ServiceNow with less clicking and more control, this pattern gets you there.

 

Live demo: what happens when you run the app

 

When you run the console application, you see a log-style output that makes it clear what the code is doing. That visibility matters because exports can fail for simple reasons, wrong query, wrong access, wrong instance name, or a file path issue.

 

Here’s the flow you observe during the run:

 

  1. You start the app from the top (standard console run).
  2. The console prints logs for the key steps, so you can track progress.
  3. The app builds the export URL (the same concept you’d use when constructing list export URLs, covered in an earlier demo).
  4. The app uses a query template in code that you can replace with your own encoded query.
  5. The app writes the results to a CSV file in a target folder you choose.
  6. You see a success-style validation message so you know the export completed.
  7. The app disposes the connection properly when it finishes, which helps prevent memory leaks and keeps the connection lifecycle clean.

 

Verifying the CSV output locally

 

After the run, you check a local folder (a simple “test” folder in the demo). The exported file is there, created from ServiceNow data.

 

To confirm the contents, you open the CSV, using Excel in the demo. That step is simple, but important, because it proves the export isn’t just “successful” in logs, it’s also readable in the tool people usually use for ad hoc analysis.

 

A practical note from the demo setup: you can do this kind of workflow even if you’re running parallel desktops (Windows and Apple side-by-side). The export still lands in the folder you configured on the machine where the console app runs.

 

If you want to pull incident records, the same flow applies. You run the app, filter with an encoded query, export to CSV, open in Excel, done.

 

Code walkthrough: how the console app is put together

 

The project is designed to be easy to follow in Visual Studio, with the key entry point in Program.cs and supporting components organized so you can extend it later.

 

You’re not just getting a script that “works once.” You’re getting a small structure that supports clean code habits, logging, and maintainable growth.

 

Program.cs (the entry point)

 

In Solution Explorer, Program.cs is where execution starts.

 

At the top, the app creates an options object. This is where you supply the basics to connect to your ServiceNow instance and run an export. The demo calls out three main parameters you need to populate, including:

 

  • Your instance name (replace it with your PDI or your target instance)
  • Your username (an account with access to the records)
  • Your credential and request configuration values needed by the app (as defined in the project)

 

For a PDI, using an admin account is fine for testing. In a real production instance, you don’t want to run exports under a personal admin. You want a service account with access limited to what it needs.

Right under that, you see request attributes and parameters that are set up as mandatory. The goal is simple: reduce the chance you forget a prerequisite and wonder why the export failed.

 

The encoded query is a big part of this. Your familiarity with encoded queries has “parallel importance” to the export itself. The app can only pull the right incident set if your filter is correct.

 

Encoded query and URL construction

 

The export URL is constructed using a pattern shown in an earlier demo. In this project, you get a template in code that you can reuse and replace.

 

That template approach matters because it keeps the mechanics stable:

 

  • You keep the same base pattern for the export request
  • You swap only what changes, like the encoded query, sort, or limits (as supported by the implementation)

This makes it easier to run “the same export” again later without re-building it by hand in the UI.

 

Connection handling with IDisposable

 

As the app connects from your environment to ServiceNow, it uses an IDisposable pattern to manage that connection lifecycle.

 

You get two wins from this:

 

  • Your code cleans up after itself when the export finishes
  • You lower the risk of resource issues over repeated runs

 

The demo calls out this cleanup step as important for avoiding memory leaks between the source (ServiceNow) and the destination (your local environment). Even in a small console app, this is a good habit, especially if you later run the export on a schedule or loop through multiple queries.

 

ExportService, client contracts, and a controller layer

 

The app creates an export service and passes a typed client object (an IServiceNowClient abstraction). That design choice is practical because it separates responsibilities:

 

  • The client focuses on talking to ServiceNow
  • The service focuses on the export behavior
  • A controller layer ties the flow together, even though this is “just” a console app

 

You also see an MVC-style approach combined with service-oriented structure. In a console application, that might feel like extra structure, but it pays off when you want to add another export type or extend the behavior without turning Program.cs into a long, fragile file.

 

Async/await for HTTPS REST calls

 

The export call is built around async and await. That fits the reality of what you’re doing:

 

  • You’re calling a ServiceNow REST API over HTTPS
  • Network calls take time and can fail
  • Async code keeps the flow clean and responsive

 

Even if you’re not building a UI, async patterns make it easier to scale the design later, like running multiple exports or adding retries and better error handling.

 

Models, constraints, contracts, and dependency injection

 

The project layout includes supporting parts you’d expect in a maintainable C# app:

 

  • Models and objects used to shape data and requests
  • Constraints and contracts that define what is required
  • A service implementation that supports dependency injection

 

That structure is why this kind of tool can move from “quick extract” to “nearly production-ready.” You don’t have to rewrite it from scratch just to make it clean enough for a team environment.

 

How you adapt this to export incidents

 

The demo shows exporting records from ServiceNow into a CSV. If your focus is incidents, your main change is what you query for and how you filter it.

 

To export incidents reliably, you typically adjust:

 

  • The target set of records to the incident table
  • The encoded query so you only pull the incidents you need
  • Any request parameters already supported in the project, such as sorting or record limits (as covered in the tutorial description)

 

The most important part is that you don’t treat the encoded query like an afterthought. Your export is only as accurate as the filter you feed into it.

 

When you build a repeatable export, you can run the same incident extract every day, every week, or for every test cycle, and get consistent output without redoing UI steps.

 

Download the source code and run it locally

 

The full source code is publicly available, and it includes a README with instructions for running the application.

 

Use the repository here: ServiceNow Data Export C# console application source code

Once you have it, you can open it in Visual Studio, update the options object values for your instance and account, then run the console app and confirm the CSV lands in your chosen folder.

 

The design is meant to be reused. You don’t need to rebuild the structure each time you want a new export. Reuse it by swapping the query and output location.

 

What you get out of this approach (beyond “a CSV file”)

 

When you stop relying on manual exports, you gain consistency.

 

This tutorial’s approach supports practical needs that come up all the time in ServiceNow work:

 

  • Exporting to CSV programmatically, so you can repeat the same extract
  • Using encoded queries to filter the exact incident set you want
  • Running exports with a C# console app, which is quick to execute and easy to automate
  • Understanding list exports and data handling by seeing the mechanics in code
  • Applying clean patterns for automation, including logging, disposal, and separation of concerns

 

Video timestamps for quick navigation

 

If you want to rewatch specific parts, these timestamps map to the flow covered:

 

  1. 00:00 Introduction and overview
  2. 00:40 Setting up the C# console application
  3. 01:14 Running the application
  4. 03:06 Code walkthrough
  5. 05:15 Final thoughts and next steps

 

Conclusion

 

If your work depends on reliable exports, a repeatable console app beats manual UI downloads every time. You get logging, a query template you can reuse, and clean connection handling with IDisposable. Once you’re comfortable swapping the encoded query, you can use the same tool for incident exports whenever you need them. Keep the structure, adjust the filter, and let the app produce a clean CSV you can open in Excel and share with your team.

Version history
Last update:
3 weeks ago
Updated by:
Contributors