Mar
 
  1
 
 
  
   Written by:
   Michael Washington
  
  
  3/1/2012 12:43 AM 
   
 
 
 
 
 
 
  
 
   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
  
  We start with a simple LightSwitch application that contains a few Entities (tables) and Screens.
  
  We Publish the application.
  
  We enter some sample data.
  Connecting To LightSwitch Using OData
  
  We change the URL that we use to get to the application …
  
  … to a URL with ApplicationData.svc.
  
  This will allow us to see the OData Feed.
  Using LinqPad
  
  We can download and install LinqPad from: http://www.linqpad.net/.
  
  We add a connection.
  
  We select OData.
  
  We enter the URL and click OK.
  
  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:

Security

We can enable Forms Authentication and deploy the application again.

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...
  
  
  
  
			
				| 
					
     
      
      By Jewel on 
       
      3/1/2012 10:36 AM
       
       
       
       
       
       
       
       
       Nice!  Great job!
       | 
				| 
					
     
      
      By Vedran on 
       
      3/2/2012 6:23 AM
       
       
       
       
       
       
       
       
       Great, really informative!
       | 
				| 
					
     
      
      By Antonio Budano on 
       
      4/27/2012 4:29 AM
       
       
       
       
       
       
       
       
       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 Michael Washington on 
       
      4/27/2012 4:29 AM
       
       
       
       
       
       
       
       
       @Antonio Budano - It should work but I have no code examples.
       | 
				| 
					
     
      
      By hueikar on 
       
      5/12/2012 7:40 AM
       
       
       
       
       
       
       
       
       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 Michael Washington on 
       
      5/12/2012 8:31 AM
       
       
       
       
       
       
       
       
       @hueikar - It only works with the latest version of Visual Studio LightSwitch.
       | 
				| 
					
     
      
      By hueikar on 
       
      5/12/2012 7:25 PM
       
       
       
       
       
       
       
       
       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 Michael Washington on 
       
      5/12/2012 7:47 PM
       
       
       
       
       
       
       
       
       @hueikar  - It wont work with LightSwitch 2011 in Visual Studio is 2010. It will only work with LightSwitch in Visual Studio 2011 Beta.
       | 
				| 
					
     
      
      By hueikar on 
       
      5/12/2012 8:03 PM
       
       
       
       
       
       
       
       
       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 Michael Washington on 
       
      5/12/2012 8:13 PM
       
       
       
       
       
       
       
       
       @hueikar  - If you are using LightSwitch in Visual Studio 2011 Beta then it should work. I don't know why it is not :(
       | 
				| 
					
     
      
      By hueikar on 
       
      5/13/2012 10:30 AM
       
       
       
       
       
       
       
       
       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 Michael Washington on 
       
      5/13/2012 10:32 AM
       
       
       
       
       
       
       
       
       @hueikar  - Sorry I do not know why it is not working for you.
       | 
				| 
					
     
      
      By hueikar on 
       
      5/14/2012 11:44 PM
       
       
       
       
       
       
       
       
       Hi Michael,
 I had fix the error finally. Thank you.
 Nice post btw. ^^
 | 
				| 
					
     
      
      By Giovanni Maggini on 
       
      11/8/2012 5:08 AM
       
       
       
       
       
       
       
       
       Hello hueikar, could you please tell more about how you had it fixed so it could help others -like me- ? 
       |