using System; using System.Linq; using System.Windows; using System.Windows.Controls; using Microsoft.LightSwitch.Framework.Client; using LightSwitch = Microsoft.LightSwitch.Model.Storage; using Microsoft.LightSwitch.Model; using System.Runtime.InteropServices.Automation; namespace ODataFinderLibrary { public partial class ODataFinder : UserControl { public ODataFinder() { InitializeComponent(); try { var modules = ClientApplicationProvider.Current.Details.GetModules(); IModuleDefinition module = null; if (modules.Where(i => i.Name.Contains("LightSwitchCommonModule")).Count() != 0) { module = modules.Where(i => i.Name.Contains("LightSwitchCommonModule")).First(); } else if (modules.Where(i => i.Name.Contains("LightSwitchApplication")).Count() != 0) { module = modules.Where(i => i.Name.Contains("LightSwitchApplication")).First(); } if (module != null) { // EntityContainer objects will be things such as ApplicationData, NorthwindData,... foreach (LightSwitch.EntityContainer globalItem in module.GlobalItems.OfType()) { string dataServiceUri = GetDataServiceBaseUri() + "/" + globalItem.Name + ".svc" + "/"; ServicesDropDownList.Items.Add(dataServiceUri); } } else { ServicesDropDownList.Items.Add("No services found."); } } catch (Exception) { // if we fail then we just won't populate any data sources } } private void ServicesDropDownList_DropDownClosed(object sender, EventArgs e) { try { string dataServiceUri = ServicesDropDownList.SelectedItem.ToString(); // Check to see if we are running in the Desktop if (AutomationFactory.IsAvailable) { var shell = AutomationFactory.CreateObject("Shell.Application"); shell.ShellExecute(dataServiceUri); } // Else we are running in a web client else { //Open up the OData service in a new web page System.Windows.Browser.HtmlPage.Window.Navigate(new Uri(dataServiceUri), "_blank"); } } catch (Exception) { // if we fail then just don't display anything } } string GetDataServiceBaseUri() { string absoluteUri = Application.Current.Host.Source.AbsoluteUri; System.UriBuilder appUri = new UriBuilder(absoluteUri); string host = Application.Current.Host.Source.Host; int indexOfHost = absoluteUri.IndexOf(host); int indexOfEndOfHost = absoluteUri.IndexOf("/", indexOfHost + host.Length); string dataServiceUri = absoluteUri.Substring(0, indexOfEndOfHost); if (!host.Contains("localhost")) { //Example format: http://somIIS/TestingDS2/Web/Application18.Client.xap?v=1.0.2.0?v=1.0.2.0 int indexOfEndOfAppName = absoluteUri.IndexOf("/", indexOfEndOfHost + 2); int lengthOfAppName = indexOfEndOfAppName - indexOfEndOfHost; string appName = absoluteUri.Substring(indexOfEndOfHost, lengthOfAppName); dataServiceUri = dataServiceUri + appName + "/"; } return dataServiceUri; } } }