Feb 28

Written by: Michael Washington
2/28/2015 8:04 AM  RssIcon

image

You can connect to the OData feed of a LightSwitch application easily.

Deploy The LightSwitch Application

image

The first step is to download the Online Ordering System (An End-To-End LightSwitch Example).

image

Publish the application.

image

We can navigate to the ApplicationData.svc end-point to view the OData feed.

Create The Client Application

image

Create a new ASP.NET Web Application.

image

Create an Empty Web Forms project.

image

Right-click on the References folder and select Add Service Reference.

image

Enter the address to the OData end-point and click Go.

image

If you have forms security enabled in the LightSwitch application you will see a warning that your credentials will be required.

(you may be required to enter your credentials three times)

image

The credentials are only needed to create the proxy. At run-time we will specify the credentials to be used.

image

When it connects and shows the details for the service, enter a name for the Namespace and click OK.

image

The service proxy will be created.

Create The Client Page

image

Add a New Item to the project.

image

Add a Web Form.

Use the following code for the .aspx markup:

 

<%@ Page Language="C#" AutoEventWireup="true" 
    CodeBehind="Default.aspx.cs" 
    Inherits="ODataClient.Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>LightSwitch OData Client</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>   
        <h1>OData Client</h1></div>
        <table>
            <tr>
                <td>Location:</td>
                <td>
                    <asp:TextBox ID="txtLocation" runat="server" Columns="60">
                    </asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>User Name:</td>
                <td>
                    <asp:TextBox ID="txtUserName" runat="server">
                    </asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>Password:</td>
                <td>
                    <asp:TextBox ID="txtPassword" runat="server">
                    </asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>&nbsp;</td>
                <td>&nbsp;</td>
            </tr>
        </table>
        <asp:Button ID="btnGetData" runat="server" 
            Text="Get Data From The Product Table" 
            OnClick="btnGetData_Click" />
        <br />
        <asp:GridView ID="gvData" runat="server">
        </asp:GridView>
        <asp:Literal ID="txtError" runat="server"></asp:Literal>
    </form>
</body>
</html>

 

Use the following code for the code behind:

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
// A using statement for our OData service namespace
using ODataClient.ApplicationDataWS;
namespace ODataClient
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                // This sets default values
                txtLocation.Text = @"http://localhost/EndToEndExample/ApplicationData.svc/";
                txtUserName.Text = "Admin";
                txtPassword.Text = "password#1";
            }
        }
        protected void btnGetData_Click(object sender, EventArgs e)
        {
            try
            {
                // Instantiate the proxy to the OData service passing the location
                ApplicationData ODataService = new ApplicationData(new Uri(txtLocation.Text));
                // Set the credentials needed to access the service
                ODataService.Credentials = new NetworkCredential(txtUserName.Text, txtPassword.Text);
                // Query the Products table
                var result = from objProduct in ODataService.Products
                             select objProduct;
                // Bind the results to the datagrid
                gvData.DataSource = result;
                gvData.DataBind();
            }
            catch (Exception ex)
            {
                // Show any possible exceptions
                txtError.Text = ex.InnerException.Message;
            }
        }
    }
}

 

Further (full CRUD examples)

For an example of Create Read Update and Delete code see: A Full CRUD LightSwitch JQuery Mobile Application.

 

Links

OData and LightSwitch Book Excerpt - Chapter 1: What Is This Book About?

Visual Studio LightSwitch and Windows Phone 7 OData Full CRUD Example

Using The OData Explorer with LightSwitch OData

A Full CRUD LightSwitch JQuery Mobile Application

 

Download Code

The LightSwitch project is available at http://lightswitchhelpwebsite.com/Downloads.aspx

Tags: OData
Categories:

2 comment(s) so far...


Gravatar

Re: Connecting To LightSwitch OData From An Outside Application

This example shows VS2013. Is LightSwitch now in 2015? I have had a hard time determining if it is native to VS2015 and in which version.

By Mike D on   9/13/2015 12:39 AM
Gravatar

Re: Connecting To LightSwitch OData From An Outside Application

@Mike D - This will work in LightSwitch in Visual Studio 2015

By Michael Washington on   9/13/2015 4:19 AM

Your name:
Gravatar Preview
Your email:
(Optional) Email used only to show Gravatar.
Your website:
Title:
Comment:
Security Code
CAPTCHA image
Enter the code shown above in the box below
Add Comment   Cancel 
Microsoft Visual Studio is a registered trademark of Microsoft Corporation / LightSwitch is a registered trademark of Microsoft Corporation