Skip to main content

HealthVault Hello World

Creating the project

  1. In Visual Studio 2005, select File>New Web Site.
  2. In the New Web Site dialog box, select ASP.NET Web Site.
  3. In the Location list, keep the default selection of File System.
  4. Name the site and select a location.
  5. Click OK.
  6. Press F5 to open an Untitled Page in the browser.

Deriving from HealthServicePage

  1. In default.aspx.cs, rename the application class to HelloWorldPage.
  2. Set the base type to HealthServicePage.
  3. Change the Page tag in default.aspx so that it inherits from the new name as well.
  4. To be able to reference that page, add the two HealthVault assemblies to the project:
    1. Right-click the project.
    2. Select Add Reference.
    3. Browse to C:\Program Files\Microsoft HealthVault\SDK\DotNet\Assemblies.
    4. Add Microsoft.Health.dll and Microsoft.Health.Web.dll to the project.
  5. Add these references to default.aspx.cs:
    1. using Microsoft.Health;
    2. using Microsoft.Health.ItemTypes;
    3. using Microsoft.Health.Web;
    4. using System.Collections.Generic;
  6. Select Build>Build Web Site to verify that the changes have been made successfully.

Adding entries to the Web.config file

The HealthVault classes look in the web.config file to find a number of items: where the HealthVault servers are, what ApplicationID to use, etc. You need to add those entries into your file.

Rather than including them here, we recommend that you get them from one of the SDK examples (perhaps the HelloWorld one, if it is there). After you copy it over, find the section where that looks like this:

<add key="WCPage_ActionHome" value="default.aspx"/>
<add key="WCPage_ActionAppAuthSuccess" value="default.aspx"/>
<add key="WCPage_ActionSignOut" value="SignedOut.aspx" />

These keys control the redirect after authorization. Make sure that the first two keys point to the main page of your application, and you can leave the third one alone for now.

These values are used only when talking to the pre-production environment (PPE) server (https://account.healthvault-ppe.com). When you eventually deploy an application, these values are hardcoded on the server so that anybody using a specific ApplicationID always redirects to the same URL.

You also need to add a "redirect" page (that is, redirect.aspx and redirect.aspx.cs). We suggest getting this directly from one of the samples, and adding it to your project. Note that the NonProductionActionUrlRedirectOverride key in web.config already points at redirect.aspx.

Adding a master page

You don't strictly need a master page, but it makes things a bit simpler, so go ahead and add one. Call it HelloWorld.master, and modify redirect.aspx and default.aspx to reference it.

Testing the application

Press F5. If everything is working fine, you sign in to HealthVault, authorize the application (if you haven't authorized that app before), and then are redirected back to your application page.

These steps get you "up and running"; now you can start doing something useful.

Displaying the name of the user

Start with a bit of code that displays the name of the user. You add a control to the page:

Name: <asp:LabelID="c_UserName" runat="Server" /><br />

The HealthServicePage that you're derived from has a property named PersonInfo, so grab the name from there:

c_UserName.Text = PersonInfo.Name;   // added to the Page_Load method

Displaying the birth year of the user

Add a control on the page:

Birth Year: <asp:LabelID="c_BirthYear" runat="Server" /><br />

And now fetch the value from HealthVault. To fetch the value, use the HealthRecordSearcher class. Create one using the following:

HealthRecordSearcher searcher = PersonInfo.SelectedRecord.CreateSearcher();

Note carefully the "SelectedRecord" part. Because HealthVault supports a user having access to more than one health record (perhaps you have access to your child’s record) you need to use the current "selected" one.

Next, you need to constrain the search to get only the information you want:

HealthRecordFilter filter = new HealthRecordFilter(Basic.TypeId);
searcher.Filters.Add(filter);

This code returns all instances of the "Basic" type in the health record. You could further constrain the query; for example, to a specific date range, by adding other items to the searcher.Filters collection.

Finally, you need to actually fetch the values and display them:

HealthRecordItemCollection items = searcher.GetMatchingItems()[0];
Basic basic = (Basic) items[0];
if (basic != null && basic.BirthYear.HasValue)
{
    c_BirthYear.Text = basic.BirthYear.ToString();
}

You can simplify the fetching by writing a generic method:

T GetSingleValue<T>(Guid typeID) where T : class
{
  HealthRecordSearcher searcher = PersonInfo.SelectedRecord.CreateSearcher();
  HealthRecordFilter filter = new HealthRecordFilter(typeID);
  searcher.Filters.Add(filter);


HealthRecordItemCollection items = searcher.GetMatchingItems()[0];
  return items[0] as T;
}

And then you can simply write:

Basic basic = GetSingleValue<Basic>(Basic.TypeId);

to fetch the value.

Inserting values into HealthVault

Add a button on the page:

<asp:Button ID="c_AddHeightEntry" Text="Add Height Entry" runat="server" />

and the following code to the Page_Load() method:

if (IsPostBack)
{
    AddHeightEntry();
}

Write the method to do the add:

void AddHeightEntry()
{
    Length value = new Length(2.0);
    Height height = new Height(new HealthServiceDateTime(DateTime.Now), value);

PersonInfo.SelectedRecord.NewItem(height);
}

You create an instance of the Height class, and then call NewItem() to save it to HealthVault. You can go to the HealthVault user page if you want to see the Height values.

Fetching and displaying the Height values

Note that this is the simplified version of how to fetch and display values.

Add a table to the page:

<asp:TableID="c_HeightTable" runat="Server" />

Add a generic method to fetch the items:

List<T> GetValues<T>(Guid typeID) where T : HealthRecordItem
{
  HealthRecordSearcher searcher = PersonInfo.SelectedRecord.CreateSearcher();
  HealthRecordFilter filter = new HealthRecordFilter(typeID);
  searcher.Filters.Add(filter);
HealthRecordItemCollection items = searcher.GetMatchingItems()[0];
  List<T> typedList = new List<T>();
foreach(HealthRecordItem item in items)
  {
    typedList.Add((T) item);
  }
returntypedList;
}

which is very much like the single-element fetch, except that it iterates through all the items.

Then add code to populate the table:

void PopulateHeightTable()
{
  c_HeightTable.Rows.Clear();

  TableRow headerRow = new TableRow();
  TableHeaderCell headerDateCell = new TableHeaderCell();
  headerDateCell.Text = "Date";
  headerRow.Cells.Add(headerDateCell);
TableHeaderCell headerHeightCell = new TableHeaderCell();
  headerHeightCell.Text = "Height";
  headerRow.Cells.Add(headerHeightCell);
  c_HeightTable.Rows.Add(headerRow);

List<Height> heightMeasurements = GetValues<Height>(Height.TypeId);
  foreach (Height height in heightMeasurements)
  {
    TableRow row = new TableRow();
    c_HeightTable.Rows.Add(row);
    TableCell dateCell = new TableCell();
    dateCell.Text = height.When.ToString();
    row.Cells.Add(dateCell);

TableCell heightCell = new TableCell();
    heightCell.Text = String.Format("{0:F2}", height.Value.Meters);
    row.Cells.Add(heightCell);
  }
}

And now, the app displays the values and adds another one if you press Add.