Calling Service Now Webservice with .Net
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 GET
s 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();
开发者_如何学JAVA 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!
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:
If you're interested I've written a set of soap classes to get and put some information into servicenow. It's pretty limited at the moment to just what I need to be able to do at work, but should work for anyone.
https://github.com/jeffpatton1971/mod-ServiceNOW
Feel free to check it out
精彩评论