You are here:   Blog
Register   |  Login

LightSwitch News

 

Mar 1

Written by: Michael Washington
3/1/2012 12:43 AM  RssIcon

Visual Studio LightSwitch 2011 (In Visual Studio 11) allows you to access your LightSwitch data via OData. This allows your LightSwitch application to communicate with web pages, mobile clients (IPad, Android, Windows Phone) and other clients such as Excel PowerPivot.

In this article we will cover the simplest scenarios for connecting to LightSwitch using OData. We will not cover, inserting, updating, deleting, or validation.

The Application

clip_image001

We start with a simple LightSwitch application that contains a few Entities (tables) and Screens.

clip_image002

We Publish the application.

clip_image003

We enter some sample data.

Connecting To LightSwitch Using OData

clip_image004

We change the URL that we use to get to the application …

clip_image005

… to a URL with ApplicationData.svc.

clip_image006

This will allow us to see the OData Feed.

Using LinqPad

clip_image007

We can download and install LinqPad from: http://www.linqpad.net/.

clip_image008

We add a connection.

clip_image009

We select OData.

clip_image010

We enter the URL and click OK.

clip_image011

The Entities will show.

We can use the following query (in C# Statement(s) mode):

 

var DozenRedRoses = 
from Product in Products
where Product.ProductName == "Dozen Red Roses"
select Product;
DozenRedRoses.Dump("The Dozen Red Roses");
// Set Product ID
var intProductID = DozenRedRoses.FirstOrDefault().Id;
var OrderDetailsForRoses = 
from OrderDetail in OrderDetails
where OrderDetail.Product.Id == intProductID
where OrderDetail.Quantity > 1
select OrderDetail;
OrderDetailsForRoses.Dump("The order details for more than 2 roses");
// Get Order Detail IDs
List<int> OrderDetailIDs = new List<int>();
foreach (var element in OrderDetailsForRoses)
{
	OrderDetailIDs.Add(element.OrderDetail_Order);
}
foreach (var element in OrderDetailIDs)
{
	var OrderForRoses = 
	from Order in Orders
	where Order.Id == element 
	select Order;
	
	OrderForRoses.Dump("A order for more than 2 Red Roses");
}

 

To produce this result:

clip_image012

Security

clip_image013

We can enable Forms Authentication and deploy the application again.

clip_image014

When we try to navigate to the OData methods it prompts us for a valid account.

Further Reading

Exposing LightSwitch Application Data

Walkthrough: Exposing and Consuming an OData Service in LightSwitch

OData by Example

Calling Your Custom WCF RIA Services Using OData

Download Code

The LightSwitch project is available at:

http://lightswitchhelpwebsite.com/Downloads.aspx

14 comment(s) so far...


Gravatar

Re: Accessing Your Visual Studio 2011 LightSwitch Application Using OData

Nice! Great job!

By Jewel on   3/1/2012 10:36 AM
Gravatar

Re: Accessing Your Visual Studio 2011 LightSwitch Application Using OData

Great, really informative!

By Vedran on   3/2/2012 6:23 AM
Gravatar

Re: Accessing Your Visual Studio 2011 LightSwitch Application Using OData

Is it possible using OData to communicate with mobile devices like Motorola (Symbol) MC3000 running windows CE 5.0?
I would like to upgrade an application I have that communicate with these kind of mobile using WCF, obviously the mobile side application must be developed using VS2008 as it is not supported neither in VS2010 nor VS2011.
If this could be feasible it would be great, any hint will be appreciated.
Thanks

By Antonio Budano on   4/27/2012 4:29 AM
Gravatar

Re: Accessing Your Visual Studio 2011 LightSwitch Application Using OData

@Antonio Budano - It should work but I have no code examples.

By Michael Washington on   4/27/2012 4:29 AM
Gravatar

Re: Accessing Your Visual Studio 2011 LightSwitch Application Using OData

Hi Michael,

Nice Blog. I had download your sample. But it required latest version visual studio to open it..I only had Visual Studio 2010.
So can you do an example for VS2010? or may be give me the detail of those entity or attribute? I will create it myself. Because i need the example to run your another project.
http://lightswitchhelpwebsite.com/Blog/tabid/61/EntryId/135/Visual-Studio-LightSwitch-and-Windows-Phone-7-OData-Full-CRUD-Example.aspx

Appreciate for your reply. thank you.

By hueikar on   5/12/2012 7:40 AM
Gravatar

Re: Accessing Your Visual Studio 2011 LightSwitch Application Using OData

@hueikar - It only works with the latest version of Visual Studio LightSwitch.

By Michael Washington on   5/12/2012 8:31 AM
Gravatar

Re: Accessing Your Visual Studio 2011 LightSwitch Application Using OData

Hi Michael,
I did using Visual Studio LightSwitch 2011.but my Visual Studio is 2010. So is it ok?Or i must use Visual Studio 2011? Thank you.

By hueikar on   5/12/2012 7:25 PM
Gravatar

Re: Accessing Your Visual Studio 2011 LightSwitch Application Using OData

@hueikar - It wont work with LightSwitch 2011 in Visual Studio is 2010. It will only work with LightSwitch in Visual Studio 2011 Beta.

By Michael Washington on   5/12/2012 7:47 PM
Gravatar

Re: Accessing Your Visual Studio 2011 LightSwitch Application Using OData

Ok. I will download Visual Studio 2011 Beta to try it. Between, I had try create one sample. But when i run it into browser,
It show me:

http://localhost:4258/default.htm?IsLaunchedByVS=True AuthenticationType=None

and I try to change it become

http://localhost:4258/ApplicationData.svc/

It show me
Server Error in '/' Application.
The resource cannot be found.
Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name change, or its temporarily unavailable.

Do you know how to solve it? Thank you.

By hueikar on   5/12/2012 8:03 PM
Gravatar

Re: Accessing Your Visual Studio 2011 LightSwitch Application Using OData

@hueikar - If you are using LightSwitch in Visual Studio 2011 Beta then it should work. I don't know why it is not :(

By Michael Washington on   5/12/2012 8:13 PM
Gravatar

Re: Accessing Your Visual Studio 2011 LightSwitch Application Using OData

Hi Michael,

I had install Visual Studio 2011 Beta and can run your file finally..
But I am still having the error as previous post...
Just want to is it need to setup anything in order to run at IIS?

Thank you.

By hueikar on   5/13/2012 10:30 AM
Gravatar

Re: Accessing Your Visual Studio 2011 LightSwitch Application Using OData

@hueikar - Sorry I do not know why it is not working for you.

By Michael Washington on   5/13/2012 10:32 AM
Gravatar

Re: Accessing Your Visual Studio 2011 LightSwitch Application Using OData

Hi Michael,

I had fix the error finally. Thank you.
Nice post btw. ^^

By hueikar on   5/14/2012 11:44 PM
Gravatar

Re: Accessing Your Visual Studio 2011 LightSwitch Application Using OData

Hello hueikar, could you please tell more about how you had it fixed so it could help others -like me- ?

By Giovanni Maggini on   11/8/2012 5:08 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