Connecting to a Workflow Service from LightSwitch Part 3



In the third and final post of this topic I’ll show one way of using the results from a workflow service from a LightSwitch application.

The content of this post draws heavily from two other blog posts:

Beth Massi’s How Do I Video: Create a Screen that can Both Edit and Add Records in a LightSwitch Application


Robert Green’s Using Both Remote and Local Data in a LightSwitch Application

Take note of some problems I have in this demo implementation. Look for Issue near the end of this post for more discussion.

As time permits, I plan to investigate further and will be soliciting feedback from the LightSwitch developer’s forum.

If you know of a different implementation to avoid the problems I’ve encountered, please don’t hesitate to send me an email or comment here.


I want to keep the example easy to demonstrate, so I’ll usd only the OrderHeader table from the AdventureWorks sample database.

The OrderHeader table contains fields such as ShipMethod, TotalDue, and Freight. For demo purposes I’m treating the data in Freight as Weight, instead of a freight charge.

Based on its business rules, the workflow service returns a value of a calculated shipping amount. The OrderHeader table does not contain a ShippingAmount. So I thought this a good opportunity to use what Beth Massi and Robert Green explained in their blog to have LightSwitch create a table in its intrinsic database and connect that table’s data to the AdventureWorks OrderHeader table.

In Part 2 of this topic, you created a LightSwitch application and added Silverlight library project to the solution containing an implementation of a proxy to connect to the workflow service. In the LightSwitch project, you should already have a connection to the AdventureWorks database and the SalesOrderHeader table.

Now add a new table to the project. Right click on Data Source node, and select Add a Table in the context menu. Call the table ShippingCost. This new table is created in the LightSwitch intrinsic database.

Add a new field, ShipCost of type Decimal to the table.

Add a relationship to the SalesOrderHeaders table, with a zero to 1 and 1 relationship.


Change the Summary Property for ShippingCost from the default Id to ShipCost.


Now add a search and detail screen for the SalesOrderHeader entity. The default naming of the screens are SearchSalesOrderHeader and SalesOrderHeaderDetail.

I want to use the SalesOrderHeaderDetail as the default screen when a user clicks the Add… button on the search screen.

How to do this is explained in detail in Beth Massi’s How Do I video and Robert Green’s blog.

In the SearchSalesOrderHeader screen designer, Add the Add… button. Right click the Add… button and select Edit Execute Code in the context menu. LightSwitch opens the SearchSalesOrderHeader.cs file. Add the following code to the griddAddAndEditNew_Execute() method for LightSwitch to display the SalesOrderHeaderDetail screen.



Now open the SalesOrderHeaderDetail screen in the designer. Rename the query to SalesOrderHeaderQuery. Add a SalesOrderHeader data item, via the Add Data Item menu on the top of the designer. I’m being brief on the detail steps here because these steps are very well explained in the Beth Massi’s video and Robert Green’s blog.

Your screen designer should appear similar to this:


Click on the Write Code button in the upper left menu in the designer and select SalesOrderHeaderDetail_Loaded under General Methods.

With the SalesOrderHeaderDetail.cs file open, add a private method which this screen code can call for getting the calculated shipping amount.


This method creates an instance of the proxy to connect to the workflow service and passes the Freight (weight), SubTotal, and ShipMethod from the selected SalesOrderHeader row. (Lines 87-90)

It returns the calculated value with a rounding to two decimal places. (Line 91)

When the user selects a SalesOrderHeader item from the search screen, the code in SalesOrderHeaderDetail_Loaded() is called and the SalesOrderID.HasValue will be true. (Line 17)


The _Loaded() method calls the workflow service to get the calculated shipping data. (line 21)

It then checks if the selected row already contains a ShippingCost item. (Line 23).

When you first run the application, the ShippingCost item will be null because no ShippingCost item was created and associated with the SalesOrderHeader item from the AdventureWorks data.

So the implementation creates a new ShippingCosts entity, sets the calculated ShippingCost, associates the new SalesOrderItem entity to the new ShippingCosts entity, and saves the changes to the database. Lines 25-29.

If the SalesOrderHeader does have a ShippingCost, the code gets the ShippingCost entity, updates the ShipCost property with the latest calculated shipping cost and saves the update. (Line 33 – 38)

Lines 19 – 40 handle the case when the user selects an existing SalesOrderHeader item from the search screen. Here the implementation handles the case when the user selects the Add… button and the SalesOrderID.HasValue (Line 17) is false.


For this condition, the implementation creates a new SalesOrderHeader item and a new ShippingCost item. The AdventureWorks database defaults some of the fields when a new row is added. It requires some fields to be initialized, such as the rowguid, to a new unique value. (Line 53)

For this demo, to create a the new SalesOrderHeader without having an exception, I initialized the properties with values shown in Lines 45 – 53. Similarly, I initialized the properties of the new ShippingCost item as shown in lines 56 – 58.

Note the setting of ShipCost to -1 in line 56. This is another ‘code smell.’ I set the property here and check for that value in the SalesOrderHeaderDetail_Saved() method. I found when this new record is saved, in the _Saved() method, it’s necessary to call the GetCalculatedShipping() method to overwrite the uninitialized (-1) value of the Shipping cost.

Issue There must be a better way of doing this implementation and I would love to receive comments in email or to this posting of ways to improve this code. This works for demo purposes. But it doesn’t look right for an application I’d want to use beyond a demo.

Finally, there is the case where the user has clicked the Add… button and saves the changes or where the user has made a change to an existing SalesOrderHeader item and clicks the Save button.


Here, I found you need to check if the SalesOrderHeader.Shipping cost is not defined. (Lines 69 – 76).

If there is not an existing ShippingCost entity associated with the SalesOrderHeader item, a new one is created (line 71) and its properties are set (lines 73 – 75). LightSwitch will update the database appropriately.

And there is that special case mentioned above, where SalesOrderHeader does contain an existing ShippingCost item, but contains a non-initialized ShippingCost item. (Line 77). For this case, the ShipCost is updated.

Issue What I also discovered in this scenario that the user needs to click the Save button twice in order to have the data for both the SalesOrderHeader and the ShipCost entities saved.

Perhaps I haven’t done the implementation here as it should be. Or perhaps I’ve come across a behavior which exists in the Beta 1 version in the scenario of linking two entities and needing to update the data in both of them.

Again, I solicit comments for a better implementation to eliminate the problems I tried to work around.

I think some of the problems in the demo arise from using the intrinsic table ShippingCost table with the external AdventureWorks table and the way LightSwitch does validation when saving associated entities.

If I modified the schema of the AdventureWorks OrderHeader table to include a new column, say for instance, a CalculatedShippingCost column, this code would become less problematic, because it would be working with just the SalesOrderHeader entity, and not two related entities in two databases.

This entry was posted in LightSwitch. Bookmark the permalink.

5 Responses to Connecting to a Workflow Service from LightSwitch Part 3

  1. Pingback: Tweets that mention Connecting to a Workflow Service from LightSwitch Part 3 | Dan Moyer's Blog --

  2. john says:

    This all worked up to part 3 and then I got lost.
    The LightSwitch project that is attached does not load, was it created with an beta version of LightSwitch?
    I can’t follow your instructions.

    • Dan says:

      The project was done with the Beta 1 version of LightSwitch. I haven’t updated the project file. Although it should work in with the RTM version, there may be differences.

  3. Rob Smith says:

    Hi Dan,
    I may be somewhat late (2014) to this series of articles, but after 2 days of searching they are still the only ones that I can find on this subject. So many thanks for the effort.

    I wonder if you have any fresher references for this (Connecting to Workflow services from LightSwitch) or if you are aware of any major changes since you wrote this?

    • Dan says:

      Thanks for the comments Rob!
      I haven’t done work with LightSwitch since those blog postings. So sorry, I can’t help you that.
      Have you tried the LightSwitch forum on MSDN? There was a large user community and it must have certainly grown more since I last visited it a year ago.
      Michael Washington has an extensive online community of followers and his news letter. Perhaps you could reach out to him for help.

      Best of success,

Leave a Reply

Your email address will not be published. Required fields are marked *