Accessible Version
Skip Navigation
Search Fake

Examples

SOAP Setup

The following information will assist you in configuring your development environment to connect with and use the HIW's SOAP Service.

Microsoft Visual Studio 2008 & 2010

  1. In the Solution Explorer, right click on your project and choose Add Service Reference or Add Web Reference.
  2. Enter the WSDL URL listed on the Overview page in the Address field and click Go.
  3. Enter "HIWService" in the Namespace field and click OK.
  4. Open the App.config file and change the maxBufferSize and maxReceivedMessageSize attributes within the configuration\system.serviceModel\bindings\basicHttpBinding\binding\ element to a higher value such as "5242880" - otherwise large responses will not work. The original value is 65,536 B (65 KB) we recommend using a value of at least 5,242,880 B (5 MB).

SOAP Code Examples

Refer to the appropriate code below for your chosen development language.

using System;
using NCHS.HIW.Samples.CSharp.SOAP.HIWService;

namespace NCHS.HIW.Samples.CSharp.SOAP
{
    public class Program
    {
        /// <summary>The entry point of the application.</summary>
        static void Main()
        {
            if (ShowSearch())
                ShowIndicatorInfo();

            Console.WriteLine();
            Console.WriteLine("Done.");
            Console.ReadKey();
        }

        private static bool ShowSearch()
        {
            string searchTerms = null;

            //Get input from the user.
            while (string.IsNullOrEmpty(searchTerms))
            {
                Console.Clear();
                Console.Write("What would you like to search for?: ");
                searchTerms = Console.ReadLine();
            }

            Console.Clear();
            Console.WriteLine("Searching...");

            //Create a SOAPClient using the configuration from the App.config file.
            using (SOAPClient client = new SOAPClient())
            {
                //Perform a search by keyword.
                IndicatorDescription[] results = client.SearchIndicatorDescriptions(searchTerms.Split(' '), 1);

                Console.Clear();

                if (results == null || results.Length == 0)
                    Console.WriteLine("Your search for \"{0}\" did not return any results.", searchTerms);
                else
                {
                    //Write the information to the console.
                    Console.WriteLine("Your search for \"{0}\" returned {1} result(s).", searchTerms, results.Length);

                    foreach (IndicatorDescription result in results)
                        Console.WriteLine("\tID: {0}\t{1}", result.ID, result.ShortDescription);

                    return true;
                }
            }

            return false;
        }

        private static void ShowIndicatorInfo()
        {
            string idString = null;
            int id;

            //Ask for an indicator ID, loop until a valid number is entered.
            do
            {
                Console.WriteLine();
                Console.Write("Please enter the ID of the Indicator Description you would like to view.: ");
                idString = Console.ReadLine();
            } while (!int.TryParse(idString, out id));

            Console.Clear();
            Console.WriteLine("Loading...");
            
            //Create a SOAPClient using the configuration from the App.config file.
            using (SOAPClient client = new SOAPClient())
            {
                //Attempt to get the requested IndicatorDescription.
                IndicatorDescription indicatorDescription = client.GetIndicatorDescriptionByID(id);

                Console.Clear();

                //Write the information to the console.
                if (indicatorDescription == null)
                    Console.WriteLine("Could not locate Indicator Description with ID of \"{0}\".", id);
                else
                {
                    Console.WriteLine(indicatorDescription.ShortDescription);
                    Console.WriteLine(indicatorDescription.FullDescription);
                    Console.WriteLine("\tSupplier:    {0}", indicatorDescription.DataSupplier);
                    Console.WriteLine("\tDimensions:  {0}", indicatorDescription.AvailableDimensions);
                    Console.WriteLine("\tGeo. levels: {0}", indicatorDescription.GeographicLevels);
                    Console.WriteLine("\tNumerator:   {0}", indicatorDescription.NumeratorDescription);
                    Console.WriteLine("\tDenominator: {0}", indicatorDescription.DenominatorDescription);
                    Console.WriteLine("\tUnits:       {0}", indicatorDescription.ValueLabel);
                }
            }
        }
    }
}
Imports NCHS.HIW.Samples.VisualBasic.SOAP.HIWService

Namespace NCHS.HIW.Samples.VisualBasic.SOAP
    Module Program
        ''' <summary>The entry point of the application.</summary>
        Sub Main()
            If ShowSearch() Then
                ShowIndicatorInfo()
            End If

            Console.WriteLine()
            Console.WriteLine("Done.")
            Console.ReadKey()
        End Sub

        Private Function ShowSearch() As Boolean
            Dim searchTerms As String = Nothing

            'Get input from the user.
            While (String.IsNullOrEmpty(searchTerms))
                Console.Clear()
                Console.Write("What would you like to search for?: ")
                searchTerms = Console.ReadLine()
            End While

            Console.Clear()
            Console.WriteLine("Searching...")

            'Create a SOAPClient using the configuration from the App.config file.
            Using client As New SOAPClient()
                'Perform a search by keyword.
                Dim results As IndicatorDescription() = client.SearchIndicatorDescriptions(searchTerms.Split(" "), 1)

                Console.Clear()

                If (results Is Nothing OrElse results.Length = 0) Then
                    Console.WriteLine(String.Format("Your search for ""{0}"" did not return any results.", searchTerms))
                Else
                    'Write the information to the console.
                    Console.WriteLine(String.Format("Your search for ""{0}"" returned {1} result(s).", searchTerms, results.Length))

                    For Each result In results
                        Console.WriteLine(String.Format("{0}ID: {1}{0}{2}", vbTab, result.ID, result.ShortDescription))
                    Next

                    Return True
                End If
            End Using

            Return False
        End Function

        Private Sub ShowIndicatorInfo()
            Dim idString As String = Nothing
            Dim id As Integer

            Do
                Console.WriteLine()
                Console.Write("Please enter the ID of the Indicator Description you would like to view.: ")
                idString = Console.ReadLine()
            Loop While Not Integer.TryParse(idString, id)

            Console.Clear()
            Console.WriteLine("Loading...")

            'Create a SOAPClient using the configuration from the App.config file.
            Using client As New SOAPClient()
                'Attempt to get the requested IndicatorDescription.
                Dim indicatorDescription As IndicatorDescription = client.GetIndicatorDescriptionByID(id)

                Console.Clear()

                'Write the information to the console.
                If indicatorDescription Is Nothing Then
                    Console.WriteLine(String.Format("Could not locate Indicator Description with ID of ""{0}"".", id))
                Else
                    Console.WriteLine(indicatorDescription.ShortDescription)
                    Console.WriteLine(indicatorDescription.FullDescription)
                    Console.WriteLine(String.Format("{0}Supplier:    {1}", vbTab, indicatorDescription.DataSupplier))
                    Console.WriteLine(String.Format("{0}Dimensions:  {1}", vbTab, indicatorDescription.AvailableDimensions))
                    Console.WriteLine(String.Format("{0}Geo. levels: {1}", vbTab, indicatorDescription.GeographicLevels))
                    Console.WriteLine(String.Format("{0}Numerator:   {1}", vbTab, indicatorDescription.NumeratorDescription))
                    Console.WriteLine(String.Format("{0}Denominator: {1}", vbTab, indicatorDescription.DenominatorDescription))
                    Console.WriteLine(String.Format("{0}Units:       {1}", vbTab, indicatorDescription.ValueLabel))
                End If
            End Using
        End Sub
    End Module
End Namespace

REST Code Examples

Refer to the appropriate code below for your chosen development language.

<!DOCTYPE html />

<html>
    <head>
        <title>JavaScript SOAP Example</title>
        <script src="Content/JavaScripts/jquery-1.7.1.min.js"></script>
        <style type="text/css">
            #searchResultsContainer,
            #indicatorContainer
            {
            	display: none;
            }
            
            #searchResultsList strong,
            #indicatorContainer strong
            {
            	display: inline-block;
            	width: 150px;
            }
        </style>
    </head>
    <body>
        <h1>What would you like to search for?</h1>
        <input id="searchText" type="text" /> <button id="searchButton" onclick="search();">Search</button>
        
        <div id="searchResultsContainer">
            <h2 id="searchResultsHeader"></h2>
            <ul id="searchResultsList" />
        </div>

        <div id="indicatorContainer">
            <h2 id="indicatorHeader"></h2>
            <p id="indicatorDescription"></p>
            <ul id="indicatorDetails">
                <li><strong>Supplier:</strong> <span id="indicatorSupplier"></span></li>
                <li><strong>Dimensions:</strong> <span id="indicatorDimensions"></span></li>
                <li><strong>Geographic levels:</strong> <span id="indicatorGeographicLevels"></span></li>
                <li><strong>Numerator:</strong> <span id="indicatorNumerator"></span></li>
                <li><strong>Denominator:</strong> <span id="indicatorDenominator"></span></li>
                <li><strong>Units:</strong> <span id="indicatorUnits"></span></li>
            </ul>
        </div>

        <script type="text/javascript">
            //Set the baseUrl to the location of the REST service.
            var baseUrl = "http://services.healthindicators.gov/v2/REST.svc/";

            //Performs a search using the provided text.
            //  Calls:    IndicatorDescriptions/[keywords]/Search/[page]
            //  See:      http://www.healthindicators.gov/Developers/ServiceMethods#Search-IndicatorDescriptions
            function search()
            {
                //Get the search text from the search textbox.
                var searchText = $("#searchText").val();

                //Use the jQuery library to make an AJAX call to the service.
                $.ajax(
                {
                    url: baseUrl + "IndicatorDescriptions/" + searchText + "/Search/1",
                    type: "GET",
                    dataType: "json",
                    success: searchCallback,
                    accept: "application/json"
                });
            }

            //This function is called by the AJAX call which is created in the search function when the call has completed successfully.
            function searchCallback(data)
            {
                var resultCount = data.length;

                if (resultCount == 0)
                    $("#searchResultsHeader").text("Your search for \"" + $("#searchText").val() + "\" did not return any results.");
                else
                {
                    var searchResultsList = $("#searchResultsList");

                    $("#searchResultsHeader").text("Your search for \"" + $("#searchText").val() + "\" returned " + resultCount + " result(s).");
                    searchResultsList.empty();

                    //Add each result to the list.
                    $(data).each(function ()
                    {
                        searchResultsList.append($("<li><strong>ID: <a href=\"#\" onclick=\"getDetails($(this).text())\">" + this.ID + "</a></strong> " + this.ShortDescription + "</li>"));
                    });
                }

                //Show the results container.
                $("#searchResultsContainer").show();
            }

            //Gets information for the Indicator Description which matches the provided ID.
            //  Calls:    IndicatorDescription/[id]
            //  See:      http://www.healthindicators.gov/Developers/ServiceMethods#IndicatorDescription
            function getDetails(id)
            {
                //Use the jQuery library to make an AJAX call to the service.
                $.ajax(
                {
                    url: baseUrl + "IndicatorDescription/" + id,
                    type: "GET",
                    dataType: "json",
                    success: getDetailsCallback,
                    accept: "application/json"
                });
            }

            //This function is called by the AJAX call which is created in the getDetails function when the call has completed successfully.
            function getDetailsCallback(data)
            {
                //Update the displayed information.
                if (data == null)
                {
                    $("#indicatorHeader").text("Could not locate the indicator description.");
                    $("#indicatorDetails").hide();
                }
                else
                {
                    $("#indicatorHeader").text(data.ShortDescription);
                    $("#indicatorDescription").text(data.FullDescription);
                    $("#indicatorSupplier").text(data.DataSupplier);
                    $("#indicatorDimensions").text(data.AvailableDimensions);
                    $("#indicatorGeographicLevels").text(data.GeographicLevels);
                    $("#indicatorNumerator").text(data.NumeratorDescription);
                    $("#indicatorDenominator").text(data.DenominatorDescription);
                    $("#indicatorUnits").text(data.ValueLabel);
                    $("#indicatorContainer").show();
                    $("#indicatorDetails").show();
                }
            }
        </script>
    </body>
</html>

Filter Examples

This example details how to construct a SearchGroup and SearchParameters for use in the filter service methods. Using JSON and XML is only necessary if you are using the REST API. Using the SOAP API you can programmatically create your query in an object-oriented way. You must adhere to the following rules when using the filter methods via the REST API:

  • Use the POST HTTP method.
  • Indicate the content type of the POST data. This is accomplished by setting the Content-Type HTTP header of your request to either application/json or application/xml (default).
  • Indicate the expected return content type by setting the Accepts HTTP header to either application/json or application/xml (default). This applies to all of the REST API methods.
  • There are currently a few very important limitations and caveats you should be made aware of when constructing your JSON queries:
    • Must be cleanly formatted as shown in the examples.
    • Use tabs instead of spaces for formatting.
    • Each Element must include a __type (note, the word "type" is preceded by two underscores) node who's value must always be set to SearchParameter:#S3.Common.Search.
    • The __type node must be the first node within each Element.

For more information regarding how to use the filter API methods, please utilize the following resources:

The queries shown below are meant to be used with the FilterIndicators API Method. Use the REST Tester to build and test your own queries. The first example is a simple query which filters Obesity data within Ohio. The second example is a more complex query which filters Obesity data with a value greater than or equal to 25% in either 2007 or 2008.