Skip to content

Instantly share code, notes, and snippets.

@incoplex
Forked from svdoever/ODataFinder.Xaml.cs
Created September 14, 2019 03:53
Show Gist options
  • Save incoplex/6a1b938b3390f45d7f63853d1180d60b to your computer and use it in GitHub Desktop.
Save incoplex/6a1b938b3390f45d7f63853d1180d60b to your computer and use it in GitHub Desktop.

Revisions

  1. @svdoever svdoever created this gist Mar 23, 2013.
    103 changes: 103 additions & 0 deletions ODataFinder.Xaml.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,103 @@
    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<LightSwitch.EntityContainer>())
    {
    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;
    }
    }
    }