none
How to override the Lightswitch data source service URL at runtime RRS feed

  • Question

  • I have Lightswitch Update 2 CTP4 and I'm currently using a Silverlight client. The data sources are all SharePoint lists which I suspect is simply OData under the hood. So, there is no SQL backend to this project.

    I have multiple sites in SharePoint that have the exact same lists in them. I would like to override the data service URL for the service calls at run-time. I see a couple of classes related to the data sources but I haven't found any properties relating to the underlying service URL that is being called. 

    Does anyone know how I can override the data service URL at runtime?

     
    Thursday, March 7, 2013 7:47 PM

Answers

  • Another option you could consider is CSOM, the Client SharePoint Object Model.  It'd be a bit more code to write but it'll do exactly what you want for relative access to lists.  Our Survey App Tutorial has some CSOM in it, for example:

    using (ClientContext siteContext = Application.SharePoint.GetAppWebClientContext()) {
        Web appWeb = siteContext.Web;
        ListCollection webLists = appWeb.Lists;
        List appPicList = webLists.GetByTitle("Photos");
        Folder appPicListFolder = appPicList.RootFolder;
        FileCollection appPicListPictures = appPicListFolder.Files;
        Microsoft.SharePoint.Client.File file = appPicListPictures.Add(createPicFileInfo);
        siteContext.Load(file);
        siteContext.ExecuteQuery();
    }

    We'll have a blog post soon that goes more in depth on SharePoint lists and CSOM.

    Thanks,
    Chris Rummel

    • Marked as answer by Angie Xu Friday, March 29, 2013 7:53 AM
    Wednesday, March 13, 2013 1:38 PM
  • Unless I'm misunderstanding you, what you appear to be describing is not a common scenario, & more importantly is NOT what LightSwitch was intended ever to do. It certainly doesn't not make LightSwitch "pretty limited in where it can be considered".

    However, for situations that are beyond LightSwitch's intended scope, a custom RIA service is your "get out of jail free card". You can do just about anything you want in a RIA service.

    For curiosity's sake, what do you mean by "I want to install a single LightSwitch app on multiple sites"? If these are physically disparate sites, the way this would usually be done is to host the web application somewhere on the internet, then install it on whatever machines that you want (anywhere in the world). That doesn't sound "limited" to me.


    Yann - LightSwitch Central - Click here for FREE Themes, Controls, Types and Commands
     
    Please click "Mark as Answer" if a reply answers your question. Please click "Vote as Helpful" , if you find a reply helpful.
     
    By doing this you'll help others to find answers faster.

    • Proposed as answer by Matt Sampson Wednesday, March 13, 2013 12:44 PM
    • Marked as answer by Angie Xu Friday, March 29, 2013 7:53 AM
    Wednesday, March 13, 2013 5:00 AM
    Moderator

All replies

  • Hey Jerry,

    I do not believe we currently expose this ability in LightSwitch.

    I'll pass it along however that you are looking for that ability.

    In the meantime, can you help me understand why you want to override the connection strings?

    The SharePoint lists are more or less OData under the hood (I wouldn't say that they completely conform to the OData Spec, because that is not the case, but they are essentially an OData Service).

    Thx - Matt


    R. Matt Sampson

    Tuesday, March 12, 2013 7:16 PM
  • I want to install a single Lightswitch app on multiple sites. If Lightswitch cannot allow an override to the service URL or the ability to apply relative service URLs, the Lightswitch framework is pretty limited in where it can be considered. 

    While the app has the ability modify the configuration during installation, it has no ability to be used across multiple sites without something like this capability. 

    With your OData reference, are you suggesting that there is an ability to override an OData service URL? I couldn't see how to do that either.

    Wednesday, March 13, 2013 12:20 AM
  • Jerry,

    I suppose one long winded work around (if it really isn't possible) is to create your own RIA service proxy in front of your actual data source and connect to that from the LS app. You can then easily change where the RIA proxy points at run-time.

    Regards


    Xander

    Wednesday, March 13, 2013 4:40 AM
  • Unless I'm misunderstanding you, what you appear to be describing is not a common scenario, & more importantly is NOT what LightSwitch was intended ever to do. It certainly doesn't not make LightSwitch "pretty limited in where it can be considered".

    However, for situations that are beyond LightSwitch's intended scope, a custom RIA service is your "get out of jail free card". You can do just about anything you want in a RIA service.

    For curiosity's sake, what do you mean by "I want to install a single LightSwitch app on multiple sites"? If these are physically disparate sites, the way this would usually be done is to host the web application somewhere on the internet, then install it on whatever machines that you want (anywhere in the world). That doesn't sound "limited" to me.


    Yann - LightSwitch Central - Click here for FREE Themes, Controls, Types and Commands
     
    Please click "Mark as Answer" if a reply answers your question. Please click "Vote as Helpful" , if you find a reply helpful.
     
    By doing this you'll help others to find answers faster.

    • Proposed as answer by Matt Sampson Wednesday, March 13, 2013 12:44 PM
    • Marked as answer by Angie Xu Friday, March 29, 2013 7:53 AM
    Wednesday, March 13, 2013 5:00 AM
    Moderator
  • Another option you could consider is CSOM, the Client SharePoint Object Model.  It'd be a bit more code to write but it'll do exactly what you want for relative access to lists.  Our Survey App Tutorial has some CSOM in it, for example:

    using (ClientContext siteContext = Application.SharePoint.GetAppWebClientContext()) {
        Web appWeb = siteContext.Web;
        ListCollection webLists = appWeb.Lists;
        List appPicList = webLists.GetByTitle("Photos");
        Folder appPicListFolder = appPicList.RootFolder;
        FileCollection appPicListPictures = appPicListFolder.Files;
        Microsoft.SharePoint.Client.File file = appPicListPictures.Add(createPicFileInfo);
        siteContext.Load(file);
        siteContext.ExecuteQuery();
    }

    We'll have a blog post soon that goes more in depth on SharePoint lists and CSOM.

    Thanks,
    Chris Rummel

    • Marked as answer by Angie Xu Friday, March 29, 2013 7:53 AM
    Wednesday, March 13, 2013 1:38 PM
  • While I'm thankful custom RIA service is an option, it's not easy for most LS users.

    I'd say the need to change connection strings at runtime is a common scenario judging by how often it's requested in these forums.

    Sharepoint data sources make this even more needed. Even LS app deployed to sharepoint cannot link to sp lists which are deployed with the app. Now that's silly. We're told it's because the list doesn't yet exist at design time... I suppose that's fair but its surely a limitation. One which could be fixed by allowing change of conn strings at runtime.

    As a result of this limitation, we have to write a ton of js just to show data from such a sp list as seen in a recent LS blog Using SharePoint Documents in LightSwitch HTML Client

    The results of this blog can be done in seconds without code by using sharepoint data source but no can do.

    The bottom line in my view is that LS data sources need to be 'relinkable' at runtime. That way logic can be used at runtime to connect to this database or that....this sharepoint site or that. This is what I think the OP means by 'I want to install a single LS app on multiple sites'

    PS Chris, do you think we'll see a sample that uses sharepoint rest service to upload files from HTML client? I mean pure JavaScript instead of webapi controller method in survey tutorial.

    Thanks, Josh

    • Edited by joshbooker Thursday, September 5, 2013 1:12 PM cuz
    Thursday, September 5, 2013 3:13 AM
  • I'm using VS 2013 Update 3 and would like to know if there is now programmatic access to override the service connection URL to an OData data source.

    I'm connecting to  an OData source that can have optional parameters in the URL.  I'd like to be able to change the URL at run time if necessary depending on what a user enters into some text boxes.

    Thursday, October 9, 2014 12:44 PM
  • Just FYI for anyone still looking for an answer to "Does anyone know how I can override the data service URL at runtime?":

    "In the latest version of LightSwitch (VS 2013 Update 2) a new partial method was introduced on DataService classes that connect to a backing database.

    This method's signature is partial void <DataSourceName>_InitializingConnection(DatabaseConnectionState state). The state object has a ConnectionString property, that allows you to set the connection string to whatever you choose at runtime.

    This allows you to switch database instances, or username/password, etc.

    This should be able to provide as much flexibility you need.  From this method, you can grab the current Application.User, and switch off of it.  Or you can call into SharePoint to get more information."


    Yann Duran
         - Co-Author of Pro Visual Studio LightSwitch 2011
         - Author of the  LightSwitch Central Blog

    FREE Download: Luminous Tools for LightSwitch
    (a Visual Studio productivity extension for LightSwitch)
     
    Click Mark as Answer, if someone's reply answers your question
    Click  Vote as Helpful, if someone's reply is helpful
     
    By doing this you'll help everyone find answers faster.

    Friday, October 10, 2014 12:33 AM
    Moderator
  • True, Yann, for database connections but not so for OData URL Josh
    Friday, October 10, 2014 12:42 AM