Calling Service Now Webservice with .Net

VydorScope picture VydorScope · Sep 30, 2011 · Viewed 7.8k times · Source

I am trying to call Service Now's webservice from .Net and I can make it work fine for inserting a record, but I can not get any GETs to work. Here is my working INSERT code:

public void insertTable(string tableName, string schema, string columnInfo, string shortDesrcipt, string longDescript)
{
    using (ServiceNow_u_database_table tableReference = new ServiceNow_u_database_table())
    {
        insertResponse response = new insertResponse();

        System.Net.ICredentials cred = new System.Net.NetworkCredential(Properties.Settings.Default.UserName, Properties.Settings.Default.Password);
        tableReference.Credentials = cred;

        insert tableInsert = this.getTableData(tableName, schema, columnInfo, shortDesrcipt, longDescript);
        try
        {
            response = tableReference.insert(tableInsert);
        }
        catch (Exception error)
        {
            Console.WriteLine(error.Message);
        }
    }
}

That works fine. Here is the code that does not work for GET:

using (ServiceNow_u_database_table tableReference = new ServiceNow_u_database_table())
{
    ServiceNowExport.com.servicenow.libertydev.u_database_table.getRecords recordGet = new getRecords();
    System.Net.ICredentials cred = new System.Net.NetworkCredential(Properties.Settings.Default.UserName, Properties.Settings.Default.Password);
    tableReference.Credentials = cred;

    recordGet.name = this._tablePrefix + tableName;
    getRecordsResponseGetRecordsResult[] response = tableReference.getRecords(recordGet);
    if (response != null && response.Count() > 0)
    {
        return true;
    }
}

When I run that code, response is always null. I am following the instructions on this page.

I know that it is connecting because if I kill the credentials line I get an unauthorized error. Any ideas what I am doing wrong here? Thanks!

Answer

Joey picture Joey · May 13, 2012

From the ServiceNow wiki on C# Web Services: here

If you are receiving a "null" response from your web service in your client code, then you may have missed the step in this tutorial for setting the elementFormDefault setting to "False" ... Please remember to recompile your code against the WSDL after you have changed this setting and saved it.

The property can be found within the ServiceNow instance at: System Properties > Web Services. Set it to false, and you should no longer receive null from the getRecords response.

Here's a screenshot of the property: Screenshot of the image