Getting the Most out of LightSwitch Summary Properties

Getting the Most out of LightSwitch Summary Properties

Rate This
  • Comments 14

Note: This article has been updated for Beta 2 on 3/17/2011

Summary Properties in LightSwitch are properties on your entities (tables) that “describe” them (kind of like the .ToString() method on objects in .NET). A summary property is used by LightSwitch to determine what to display when a row of data is represented on a screen. Therefore, it’s important to get them right so that your data is represented to users in a clear way. In this post I’ll explain how to specify these properties on entities as well as how they work in LightSwitch applications.

Specifying Summary Properties on Entities (Tables)

Regardless of whether you are creating new database tables or attaching to an existing data source, summary properties are used to describe your entities as you model them in the designer. By default, LightSwitch will choose the first string property on your entity as the summary property. For instance, say we have a Customer entity with the following properties pictured below. If you select the Customer entity itself (by clicking on the name of it) you will see that the summary property is automatically set to LastName in the properties window. This is because the LastName property is the first property listed as type string.

image

In most cases this is the correct behavior you will want, however, for a Customer it makes more sense to display their full name instead. We can easily do this using a computed property. To add one, click on the “Computed Property” button at the top of the designer. This sets the Is Computed property in the properties window for you. Name the property FullName and then click on the “Edit Method” link in the properties window. This method is where you return the value of the computed field.

image

Computed fields are not stored in the underlying database, they are computed on the entity and only live in the data model. For FullName, I will return “LastName, FirstName” by writing the following code (in bold).

Public Class Customer

    Private Sub FullName_Compute(ByRef result As String)
        ' Set result to the desired field value
        result = Me.LastName + ", " + Me.FirstName
    End Sub

End Class

To set the FullName as the summary property, go back and select the entity’s name and you will now see FullName in the dropdown on the properties window as an available choice to use as the entity’s summary property.

Summary Properties in Action

Summary properties are displayed anytime you use the Summary control on a screen or when LightSwitch generates a layout for you that needs to display a description of the entity. For instance, to see this summary property in action create a search screen for the customer. Click on the “Screen…” button at the top of the designer, select the Search Screen template and select Customer as the screen data:

image

(Note, if you don’t have any records in your development database then also add a New Data Screen to enter some data first).Take a look at the Full Name label control in the screen designer of the search screen. Because this is a summary property it will display as a hyperlink to allow the user to open the specific record as indicated by the “Show as Link” in the Appearance section of the properties window. You can also choose which screen it should open. By default, the default edit screen is opened.

image

Hit F5 to run the application and open the customer search screen. Notice that the Full Name field is displaying as a clickable hyperlink. 

image

If we click it, then then LightSwitch will generate an edit screen for us to access the record. Notice that the titleof the generated Edit screen also displays the summary property:

image

Summary properties also show up in child grids and modal window pickers as well. 

More Examples of Summary Properties

Summary properties are not required to be strings, they just have to be able to be represented as strings. For instance, maybe you have an entity that captures date ranges or some item that would be better represented as a date. For our example say we have an Appointment entity and we’ve set a relation where a Customer can have many Appointments. The Appointment table has properties Subject, StartTime, EndTime and some Notes. We can select any of these properties as summary fields. You can even mix+match them by creating a computed property just like before, you just need to make sure the properties can be converted to a string.

Private Sub Summary_Compute(ByRef result As String)
    ' Set result to the desired field value
    result = Me.StartTime + ": " + Me.Subject
End Sub

Another thing that you can do is show data from a related entity. For instance if you have an OrderDetail line item that has a reference lookup value to a Product table, you may want to display the Product Name on the OrderDetail. Create a computed property and then just walk the association to get the property you want on the related entity. Just make sure you perform a null check on the related property first:

Private Sub Summary_Compute(ByRef result As String)
    ' Set result to the desired field value
    If Me.Product Is Nothing Then
        result = "<New Product> - Quantity: " + Me.Quantity
    Else
        result = Me.Product.Name + " - Quantity: " + Me.Quantity
    End If
End Sub

Keep in mind that summary properties are meant to be displayed throughout the application and should not be too lengthy or complicated. Keeping them down to 50 characters or less will make the application look less cluttered.

Enjoy!

Leave a Comment
  • Please add 5 and 1 and type the answer here:
  • Post
  • Beth,

    What happens when someone changes LastName and/or FirstName? Does the Summary get updated (computed again) on the fly?

  • @Luciano, yes that's correct.

  • i tried to leave a comment but it didnt work

  • 1. What about sorting on the computed property?

    2. What about search on the conputed property?

    Neither seem to work, nor can I find any examples.

  • Hi Beth,

    The computed property doesn't support the fuzzy search: search the full name directly.

    Is there any tips to handle this?

    Thanks!

  • Very cool. I'm using the Contacts Walk Thru and this was a link in that app so I thought I'd check it out.. Very good.. Beth, write a good lightswitch book.. Get MS to certify one in this app. I love it..

    Tim

  • Hi Beth

    How can we do this in the HTML Client? It doesn't let you use a computed property as the summary property?

  • Beth, I came a little bit late to new LightSwitch new version. After upgrade, I cannot see "summary property" on the entity! Where did it go? Do I have to override ToString()?

  • OK, never mind, I found it! I just had to click on "Client" Perspective.

  • How can i do it with an register screen? if i have a field char(1) with W (week) or M (Month) values

  • Something weird is happening. I added a computed property in server perspective, then switch to client perspective and select my entity name and in the properties window I see summary property but I don't see the computed property that I added. I have saved, closed and opened the lsml file but I don't see it in the drop down.

  • I have the same problem that Mahesh has.

  • @Mahesh / @Guido -- Calculated properties only apply to the Server and Silverlight desktop client, not HTML client. This is because you need to write JavaScript on the HTML client. You can still set multiple columns to display on the HTML client screens though (i.e. to show a customers' full name for instance). If you need calculated properties on the HTML client see -

    lightswitchhelpwebsite.com/.../Computed-Properties-With-the-LightSwitch-HTML-Client.aspx

    and

    msdn.microsoft.com/.../jj733571.aspx

    Cheers,

    -Beth

  • Hi,

    thanks for the tutorial. I set the date format as "d" (without time). It works using a data grid but when I use the list to show the summary (date), the standard format with the time appears. How can I disable the time from the summary?

    Thanks in advance.

Page 1 of 1 (14 items)