Sandaru
Sandaru

Reputation: 1259

Web service not returning data in correct format

I have created following test application to retrieve data from Service Now.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel;

namespace ServiceNowConnection
{
    class Program
     {
         static void Main(string[] args)
        {
            Service_Now_Reference.getRecords records = new Service_Now_Reference.getRecords();
            records.sys_user = "XXXXXXX";

            Service_Now_Reference.ServiceNowSoapClient proxyUser = new Service_Now_Reference.ServiceNowSoapClient();
            proxyUser.ClientCredentials.UserName.UserName = "XXXXXX";
            proxyUser.ClientCredentials.UserName.Password = "XXXXXX";
            Service_Now_Reference.getRecordsResponseGetRecordsResult[] result = proxyUser.getRecords(records);

            foreach (var item in result)
            {
                Console.WriteLine(item.sys_created_by);
            }
            Console.ReadLine();
        }

    }
}

This connects to the service now application correctly with no errors, but when I try to print retrieved data it gives me keys instead string type answers.

enter image description here

For some properties it gives correct values (example sys_updated_by)

enter image description here

how can I avoid this situation.

Upvotes: 4

Views: 1056

Answers (2)

Sandaru
Sandaru

Reputation: 1259

The way you need to do it.

If you are using RESTfull API in C# of cause you can do it by sending direct HTTPS call to service now.

But things getting complicated when you are using SOAP api

While you're importing web service to your program it includes following XML codes to the App.config or Web.config depending on your application intention(Web application or stand alone Application).

  <system.serviceModel>
<bindings>
  <basicHttpBinding>
    <binding name="ServiceNowSoap">
      <security mode="Transport" />
    </binding>
    <binding name="ServiceNowSoap1" />
  </basicHttpBinding>
</bindings>
<client>
  <endpoint address="https://XXXXXXXX.service-now.com/service_subscribe_sys_user_list.do?SOAP"
    binding="basicHttpBinding" bindingConfiguration="ServiceNowSoap"
    contract="ServiceReference1.ServiceNowSoap" name="ServiceNowSoap" />
</client>
 </system.serviceModel>

First of all if you are expecting to have big set of records to be retrieved you need to increase the size of Max Received Message Size.

for that you need to edit the tag like following

<binding name="ServiceNowSoap" maxReceivedMessageSize="2000000000">

Next part is adding displayvalue=all to the URL.

We can't edit end point url withing the XML it self instead you can remove the URL and add it as a key value. But still you cant add parameters to the url with & sign you need to store values as separate keys and combine it withing the program to get the full URL

Final XML will be like this

<configuration>
  <appSettings>
    <add key="serviceNowUrl"
     value="https://XXXXXXXX.service-now.com/service_subscribe_sys_user_list.do?"/>
    <add key="displayvalue" value="displayvalue=true"/>
    <add key="protocol" value="SOAP"/>
  </appSettings>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
    <system.serviceModel>
        <bindings>
            <basicHttpBinding>
                <binding name="ServiceNowSoap" maxReceivedMessageSize="2000000000">
                    <security mode="Transport">
                        <transport clientCredentialType="Basic" proxyCredentialType="Basic"
                        realm="">
                            <extendedProtectionPolicy policyEnforcement="Never" />
                        </transport>
                        <message clientCredentialType="UserName" algorithmSuite="Default" />
                    </security>
                </binding>
                <binding name="ServiceNowSoap1" />
            </basicHttpBinding>
        </bindings>
        <client>
            <endpoint binding="basicHttpBinding" bindingConfiguration="ServiceNowSoap"
            contract="Service_Now_Reference.ServiceNowSoap" name="ServiceNowSoap" />
        </client>
    </system.serviceModel>
</configuration>

You can assemble the url as follow

    string url = ConfigurationSettings.AppSettings["serviceNowUrl"];
    string protocol = ConfigurationSettings.AppSettings["protocol"];
    string displayvalue = ConfigurationSettings.AppSettings["displayvalue"];

    System.ServiceModel.EndpointAddress endpoint = new System.ServiceModel.EndpointAddress(string.Format("{0}{1}{2}", url, protocol, displayvalue));

Upvotes: 3

Bryan
Bryan

Reputation: 551

If you update the URL you are using to get the WSDL and make requests to, to include 'displayvalue=all' the response will include display names as well as sys_id values (think foreign_key) of referenced records.

For more info check out: http://wiki.servicenow.com/?title=Direct_Web_Services#Return_Display_Value_for_Reference_Variables&gsc.tab=0

Thanks, Bryan

Upvotes: 1

Related Questions