An extremely useful method that I consider to be a “must-have” in almost any type of LightSwitch application is the Created method. The Created method allows us to easily set the default values for an entity’s properties (e.g. fields in a table). To understand this further, let’s dive into an example that shows how to use the Created method.

If you read my first blog post on how to Prevent a Hole with Security Access Control, then you may remember in that scenario that I created a simple application used to track expense reports (which I creatively called the “Expense Report Tracker”). This application consists of two entities sharing a one-to-many relationship, called ExpenseReport and ExpenseItems. There are two types of users that use the application:

  • Employees that create and save expense reports for approval
  • Managers that approve or reject employees’ expense reports

In addition, we have a couple of business rules for when an employee creates an expense report – specifically, when an employee creates an expense report:

  • The ExpenseReport’s Name and UserAlias fields should be automatically set to the employee’s user information
  • The ExpenseReport’s Status field should be automatically set to a “Pending” status


The Created method allows us to implement both of the above business rules by using it to set the default values for the Name, UserAlias, and Status properties when the ExpenseReport entity is created. It’s important to note that the Created method is executed on both the Client and Server tier. As a result, the Created method is called no matter what mechanism is used to create an entity, regardless if it’s by clicking the “Add” button on a screen or by constructing an entity through code.

To add the ExpenseReport’s Created method to our application, open the ExpenseReport entity in the designer and select the “ExpenseReport_Created” method from the “Write Code” drop-down menu


Once the “ExpenseReport_Created” method’s code is generated, we can access the entity and set the properties’ values using the following code:

partial void ExpenseReport_Created()
   this.Status = "Pending";

this.Name = this.Application.User.FullName;

   this.UserAlias = this.Application.User.Name.Substring(
   this.Application.User.Name.LastIndexOf("\\") + 1);

Private Sub ExpenseReport_Created()
    Me.Status = "Pending"
    Me.Name = Me.Application.User.FullName

    Me.UserAlias = Me.Application.User.Name.Substring(
        Me.Application.User.Name.LastIndexOf("\\") + 1 
End Sub

Next, let’s add a screen, called CreateNewExpenseReport, The screen is used for creating Expense Reports that display the values of the Name, UserAlias, and Status properties. Since these values are set automatically for the user in the Created method, we don’t want the user to have to enter these values. Essentially, we want these values to be non-editable through the screen.


The easiest way to get this behavior is to use Label controls to display the Name, UserAlias, and Status properties. Label controls allow the user to view a value, but not edit it. To set the Control Type to Label, select the control in the screen designer and change the “Control Type” property to “Label”.


It’s important to note that even though our screen prevents the user from entering values for the Name, UserAlias, and Status properties, these values aren’t protected from modification. To properly secure these values and ensure that an employee cannot modify them, we need to implement security access control checks in the save pipeline methods. Refer to my previous blog post for information on how to do this.


As you can see from the above scenario, the Created method is extremely useful for setting default values for entity properties. In particular, setting default values in this manner helps save the end-user time so that they don’t have to enter data that the application can automatically detect and set for them.

For a video demonstration of this technique see - How Do I: Set Default Values on Fields when Entering New Data?