To continue the theme of landing external data in Office 2010, I’ve taken a demo excerpt from a session I presented at a conference last week to show how easy it is to land OData information in an add-in. In this sample I simply use the Northwind Database which is a publicly available OData producer. You can read more about OData on the OData.org site. By simply clicking the Northwind link you can see a collection of the database tables you can access. This sample uses Customers, http://services.odata.org/Northwind/Northwind.svc/Customers, to retrieve an Atom document with all the customers in the database. You’ll then load the customers into the add-in using the same document-level add-in pattern as used in the previous post.
Sample Office Add-in OData Project
<StackPanel> <Button Name="button1" Content="Insert Customer Information" Margin="5" Click="button1_Click" /> <ListBox Name="customerListBox" ItemsSource="{Binding}"> <ListBox.ItemTemplate> <DataTemplate> <StackPanel> <StackPanel Orientation="Horizontal"> <TextBlock Text="{Binding CustomerID}" FontWeight="Bold" /> <TextBlock Text=": " FontWeight="Bold" /> <TextBlock Text="{Binding ContactName}" /> </StackPanel> <TextBlock Text="{Binding ContactTitle}" Margin="5 0 0 0" /> </StackPanel> </DataTemplate> </ListBox.ItemTemplate> </ListBox> </StackPanel>
using ServiceReference1; using System.Net;
// Instanitate Northwind Entities object NorthwindEntities dc = new NorthwindEntities( new Uri(http://services.odata.org/Northwind/Northwind.svc));
protected override void OnInitialized(EventArgs e) { // Bind Customers to the Listbox datacontext customerListBox.DataContext = dc.Customers; base.OnInitialized(e); }
private void button1_Click(object sender, RoutedEventArgs e) { // Cast the selected item to a customer object Customer customer = (Customer)customerListBox.SelectedItem;
// Load document content controls with selected customer data Globals.ThisDocument.wccCustomerID.Text = customer.CustomerID; Globals.ThisDocument.wccContactName.Text = customer.ContactName; Globals.ThisDocument.wccAddress.Text = customer.Address; Globals.ThisDocument.wccCity.Text = customer.City; Globals.ThisDocument.wccCountry.Text = customer.Country; Globals.ThisDocument.wccPostalCode.Text = customer.PostalCode; }
private CustomerUserControl CustomerActionPane = new CustomerUserControl();
ElementHost host = new ElementHost(); host.Dock = DockStyle.Fill; host.Child = new CustomerPicker(); CustomerActionPane.Controls.Add(host); this.ActionsPane.Controls.Add(CustomerActionPane);
So there you have it, another way to land data in the Office clients (think about pulling data into Excel this way). Accessing data like this is extremely powerful because you can essentially land data that is exposed as an OData end-point by simply using WCF Data Services.
Enjoy!