Pulling data from the SN Web Service using C#

Jay_Ford
Kilo Guru

I recently had a requirement where I needed to pull data from our Service-Now incident table to use in an internal Flex application that is fed by a C# web service. Previous to our go live with SN, the C# web service pulled data directly from a MS SQL database. The challenge was to point this web service out to our SN instance to capture the same data. Below is how I accomplished this.

The first step for me was to create a new web service page in my C# project. After I created the page it was time to create a Web Reference in the project that points back to our SN incident list. In my case I named my web reference "incident_list" and pointed it to "https://myinstancename.service-now.com/incident_list.do?WSDL".

When the SN web service returns items contained in a reference field from the incident list or form, it returns the sys_id of the associated item. So in this case I needed the "caller_id", the "assignment_group", and the "assigned_to", and in order to get the friendly name of these items I had to query those tables separately via their web service WSDL page. So I created two more web references in my project, one for the sys_user_list, and the other for sys_user_group_list. I then created two functions in my app to query those web references based on the sys_id's returned from SN in the initial query.

caller_id/assigned_to (sys_user_list) = "https://myinstancename.service-now.com/sys_user_list.do?WSDL"
assignment_group (sys_user_group_list) = "https://myinstancename.service-now.com/sys_user_group_list.do?WSDL"

Below is the sample code from my app, I have commented it and I hope that it will help anyone else trying to use C# to get data from the SN web services. I just know enough C# to get by so if anyone has improvements to the code please let me know as well so that I can learn as well.



using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;

namespace FlexSendMail
{
/// <summary>
/// Summary description for SN_Demo
/// </summary>
[WebService(Namespace = "http://snwebservicedemo/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ToolboxItem(false)]
public class SN_Demo : System.Web.Services.WebService
{

[WebMethod]
public DataTable getSnCritical()
{
//get date 7 days ago for use in the query to SN
DateTime MyDate;
MyDate = DateTime.Now - TimeSpan.FromDays(7);
//format the date 7 days ago as a string to use in the query
string theDate = String.Format("{0:yyyy-MM-dd}", MyDate);

//connect to the SN web service
incident_list.ServiceNow_incident soapClient = new FlexSendMail.incident_list.ServiceNow_incident();
//provide credentials for your instance, I used a user with itil role
System.Net.ICredentials cred = new System.Net.NetworkCredential("demo", "demopw");
soapClient.Credentials = cred;
incident_list.getRecords record = new FlexSendMail.incident_list.getRecords();
//filter your query here, here I am looking for critical incidents updated in the last 7 days
record.priority = "1";
record.sys_updated_on = " >= " + theDate;
//grab the records and place them in an array
incident_list.getRecordsResponseGetRecordsResult[] records = soapClient.getRecords(record);

//I put the results into a DataTable which my C# web service returns to my Flex app
DataTable dt = new DataTable();
dt.TableName = "P1s";
dt.Columns.Add("Ticket");
dt.Columns.Add("Description");
dt.Columns.Add("CustomerName");
dt.Columns.Add("AssignGroup");
dt.Columns.Add("AssignPerson");
dt.Columns.Add("Created");
dt.Columns.Add("Date");
dt.Columns.Add("Time");
dt.Columns.Add("TimeGMT");

//Loop through the records and add each to the DataTable
for (int i = 0; i < records.Length; i++)
{

DataRow dr = dt.NewRow();
//write the incident number to the DataTable
dr["Ticket"] = records<i>.number;
//write the short description to the DataTable
dr["Description"] = records<i>.short_description;
//use the getName funtion below to lookup the callers name from the sys_id returned in the initial query
dr["CustomerName"] = getName(records<i>.caller_id.ToString());
//use the getGroup funtion below to lookup the assignment_group name from the sys_id returned in the initial query
dr["AssignGroup"] = getGroup(records<i>.assignment_group.ToString());
//use the getName funtion below to lookup the assigned_to name from the sys_id returned in the initial query
dr["AssignPerson"] = getName(records<i>.assigned_to.ToString());
//get the created date time from the record, this is returned as a string
dr["Created"] = records<i>.opened_at;
//use the getDate funtion below to return friendly UK date format for my Flex app eg. 29/03/2010 dd/MM/yyyy
dr["Date"] = getDate(records<i>.opened_at.ToString());
//use the getTime funtion below to return friendly GMT time for my Flex app eg. 4:54 PM GMT
dr["Time"] = getTime(records<i>.opened_at.ToString());
//add the current time to the DataTable for my Flex app
dr["TimeGMT"] = String.Format("{0:t}", DateTime.Now) + " GMT";
dt.Rows.Add(dr);
}
//After all the rows are added, return the DataTable
return dt;
}

//Function to get the friendly name of the caller and assigned_to
public string getName(string theId)
{
sys_user_list.ServiceNow_sys_user soapClient = new FlexSendMail.sys_user_list.ServiceNow_sys_user();
System.Net.ICredentials cred = new System.Net.NetworkCredential("demo", "demopw");
soapClient.Credentials = cred;
sys_user_list.getRecords record = new FlexSendMail.sys_user_list.getRecords();
record.sys_id = theId;
sys_user_list.getRecordsResponseGetRecordsResult[] records = soapClient.getRecords(record);
string answer = "";

for (int i = 0; i < records.Length; i++)
{
answer = records<i>.name.ToString();

}
return answer;
}

//Function to get the friendly name of the assignment_group
public string getGroup(string theId)
{
sys_user_group_list.ServiceNow_sys_user_group soapClient = new FlexSendMail.sys_user_group_list.ServiceNow_sys_user_group();
System.Net.ICredentials cred = new System.Net.NetworkCredential("demo", "demopw");
soapClient.Credentials = cred;
sys_user_group_list.getRecords record = new FlexSendMail.sys_user_group_list.getRecords();
record.sys_id = theId;
sys_user_group_list.getRecordsResponseGetRecordsResult[] records = soapClient.getRecords(record);
string answer = "";

for (int i = 0; i < records.Length; i++)
{
answer = records<i>.name.ToString();

}
return answer;
}

//Function to get the date from the returned created date string and returns UK date format eg. 29/03/2010 dd/MM/yyyy
public string getDate(string created)
{
DateTime createdDateTime = DateTime.Parse(created);
string answer = String.Format("{0:dd/MM/yyyy}", createdDateTime);
return answer;
}

//Function to get the time from the returned created date string and returns short time format eg. 4:54 PM GMT
public string getTime(string created)
{
DateTime createdDateTime = DateTime.Parse(created);
string answer = String.Format("{0:t}", createdDateTime) + " GMT";
return answer;
}
}
}


1 REPLY 1

Jace Benson
Mega Sage

I've been gathering a list of Servicenow Resouces and found this on github a few weeks ago.

Feel free to check out other resources here; 

https://hub.sndevs.com