Doing vocabulary search from a mobile device

Mobile devices use a variant of offline access to connect to the HealthVault service, and the current release doesn’t support calling the SearchVocabulary  method.

You can, however, call the method that we use to populate search dropdown boxes in the HealthVault shell. Here’s a bit of code that shows how to do it; it’s written for the .NET desktop library but can be easily adapted to run on a mobile device.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Web;
using System.Text.RegularExpressions;
using System.Xml.Linq;

namespace MobileVocabSearch
{
    class Program
    {
        const string serviceToken = "<your client token here>";
       
        const string baseSearchUrl = "https://platform.healthvault-ppe.com/platform/hvclientservice.ashx";

        static void Main(string[] args)
        {
            List<XElement> items = SearchVocabulary("RxNorm Active Medicines", "RxNorm", 10, "al");

        }
       
        static List<XElement> SearchVocabulary(string vocabName, string vocabFamily, int maxResults, string searchString)
        {
            string url =    baseSearchUrl +
                            "?service=searchVocab&searchMode=fulltext&output=XML&serviceToken=" + serviceToken +
                            "&vocabName=" + HttpUtility.UrlEncode(vocabName) +
                            "&vocabFamily=" + HttpUtility.UrlEncode(vocabFamily) +
                            "&maxResults=" + maxResults.ToString() +
                            "&culture=en-US" +
                            "&searchString=" + HttpUtility.UrlEncode(searchString);

            List<XElement> items = new List<XElement>();

            WebRequest request = WebRequest.Create(url);

            WebResponse response = request.GetResponse();

            Stream responseStream = response.GetResponseStream();
            using (StreamReader reader = new StreamReader(responseStream))
            {
                string responseString = reader.ReadToEnd();

                XElement responseElement = XElement.Parse(responseString);

                items.AddRange(responseElement.Descendants("code-item"));
            }

            return items;
        }
    }
}

You must get a client token for your application before this will work; you do this by going to the ApplicationConfigurationCenter, clicking on the application you want to use, go to the “Misc” tab, and then select the “Generate a new guid” next to the client token box. Save this configuration, and then take the token and put it into the program above.