Visual Studio LightSwitch 2012 - Springer

14 downloads 248 Views 267KB Size Report
Visual Studio. LightSwitch 2012. Tim Leung. Page 2. Visual Studio LightSwitch 2012. Copyright © 2013 by Tim Leung .... Chapter 1: Introducing LightSwitch. □.
Visual Studio LightSwitch 2012

Tim Leung

Visual Studio LightSwitch 2012 Copyright © 2013 by Tim Leung This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way, and transmission or information storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now known or hereafter developed. Exempted from this legal reservation are brief excerpts in connection with reviews or scholarly analysis or material supplied specifically for the purpose of being entered and executed on a computer system, for exclusive use by the purchaser of the work. Duplication of this publication or parts thereof is permitted only under the provisions of the Copyright Law of the Publisher’s location, in its current version, and permission for use must always be obtained from Springer. Permissions for use may be obtained through RightsLink at the Copyright Clearance Center. Violations are liable to prosecution under the respective Copyright Law. ISBN-13 (pbk): 978-1-4302-5071-5 ISBN-13 (electronic): 978-1-4302-5072-2 Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights. While the advice and information in this book are believed to be true and accurate at the date of publication, neither the authors nor the editors nor the publisher can accept any legal responsibility for any errors or omissions that may be made. The publisher makes no warranty, express or implied, with respect to the material contained herein. President and Publisher: Paul Manning Lead Editor: Ewan Buckingham Developmental Editor: Tom Welsh Technical Reviewers: Stephen Provine and Fabio Claudio Ferracchiati Editorial Board: Steve Anglin, Mark Beckner, Ewan Buckingham, Gary Cornell, Louise Corrigan, Morgan Ertel, Jonathan Gennick, Jonathan Hassell, Robert Hutchinson, Michelle Lowman, James Markham, Matthew Moodie, Jeff Olson, Jeffrey Pepper, Douglas Pundick, Ben Renow-Clarke, Dominic Shakeshaft, Gwenan Spearing, Matt Wade, Tom Welsh Coordinating Editor: Katie Sullivan Copy Editor: Roger LeBlanc Compositor: SPi Global Indexer: SPi Global Artist: SPi Global Cover Designer: Anna Ishchenko Distributed to the book trade worldwide by Springer Science+Business Media New York, 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail [email protected], or visit www.springeronline.com. Apress Media, LLC is a California LLC and the sole member (owner) is Springer Science + Business Media Finance Inc (SSBM Finance Inc). SSBM Finance Inc is a Delaware corporation. For information on translations, please e-mail [email protected], or visit www.apress.com. Apress and friends of ED books may be purchased in bulk for academic, corporate, or promotional use. eBook versions and licenses are also available for most titles. For more information, reference our Special Bulk Sales–eBook Licensing web page at www.apress.com/bulk-sales. Any source code or other supplementary materials referenced by the author in this text is available to readers at www.apress.com. For detailed information about how to locate your book’s source code, go to www.apress.com/source-code/.

Contents at a Glance About the Author��������������������������������������������������������������������������������������������������������������� xxi About the Technical Reviewers��������������������������������������������������������������������������������������� xxiii Acknowledgments������������������������������������������������������������������������������������������������������������ xxv Foreword������������������������������������������������������������������������������������������������������������������������ xxvii Introduction��������������������������������������������������������������������������������������������������������������������� xxix ■■Chapter 1: Introducing LightSwitch�����������������������������������������������������������������������������������1 ■■Chapter 2: Setting Up Your Data��������������������������������������������������������������������������������������11 ■■Chapter 3: Introducing Silverlight Screen Design�����������������������������������������������������������37 ■■Chapter 4: Accessing Data with Code�����������������������������������������������������������������������������63 ■■Chapter 5: Validating Data�����������������������������������������������������������������������������������������������89 ■■Chapter 6: Querying Your Data��������������������������������������������������������������������������������������113 ■■Chapter 7: Mastering Silverlight Screen Design������������������������������������������������������������139 ■■Chapter 8: Creating HTML Interfaces�����������������������������������������������������������������������������189 ■■Chapter 9: Creating and Using RIA Services������������������������������������������������������������������247 ■■Chapter 10: Sharing Data with OData����������������������������������������������������������������������������265 ■■Chapter 11: Creating and Using Custom Controls���������������������������������������������������������281 ■■Chapter 12: Creating Control Extensions�����������������������������������������������������������������������309 ■■Chapter 13: Creating Data and Presentation Extensions�����������������������������������������������365 ■■Chapter 14: Creating Reports����������������������������������������������������������������������������������������435 ■■Chapter 15: Sending E-mail�������������������������������������������������������������������������������������������481 iii

■ Contents at a Glance

■■Chapter 16: Authenticating Your Users�������������������������������������������������������������������������523 ■■Chapter 17: Authorizing Your Users������������������������������������������������������������������������������537 ■■Chapter 18: Deploying Your Application������������������������������������������������������������������������557 ■■Appendix A: Culture Names�������������������������������������������������������������������������������������������597 ■■Appendix B: Data Type Identifiers���������������������������������������������������������������������������������603 ■■Appendix C: Using Properties in Custom Controls���������������������������������������������������������605 ■■Appendix D: Custom Screen Template View IDs������������������������������������������������������������609 ■■Appendix E: HelpDesk Tables�����������������������������������������������������������������������������������������613 Index���������������������������������������������������������������������������������������������������������������������������������627

iv

Contents About the Author��������������������������������������������������������������������������������������������������������������� xxi About the Technical Reviewers��������������������������������������������������������������������������������������� xxiii Acknowledgments������������������������������������������������������������������������������������������������������������ xxv Foreword������������������������������������������������������������������������������������������������������������������������ xxvii Introduction��������������������������������������������������������������������������������������������������������������������� xxix ■■Chapter 1: Introducing LightSwitch�����������������������������������������������������������������������������������1 Understanding LightSwitch’s Architecture������������������������������������������������������������������������������������1 The Data Layer������������������������������������������������������������������������������������������������������������������������������������������������������� 2 The Logic Layer����������������������������������������������������������������������������������������������������������������������������������������������������� 3 The Presentation Layer������������������������������������������������������������������������������������������������������������������������������������������ 4

Introducing the Model-Centric Architecture����������������������������������������������������������������������������������6 Building Your Application���������������������������������������������������������������������������������������������������������������6 Understanding the Model-View-ViewModel Pattern���������������������������������������������������������������������8 Examining LightSwitch Projects����������������������������������������������������������������������������������������������������8 Reducing Project Sizes����������������������������������������������������������������������������������������������������������������10 Summary�������������������������������������������������������������������������������������������������������������������������������������10 ■■Chapter 2: Setting Up Your Data��������������������������������������������������������������������������������������11 Choosing Where to Store Your Data���������������������������������������������������������������������������������������������11 What Are Entities and Properties?�����������������������������������������������������������������������������������������������12 Creating Tables (Entities)�������������������������������������������������������������������������������������������������������������12

v

■ Contents

Creating Fields (Properties)���������������������������������������������������������������������������������������������������������13 Storing String Data���������������������������������������������������������������������������������������������������������������������������������������������� 13 Storing Numbers (Double and Decimal Types)���������������������������������������������������������������������������������������������������� 14 Formatting Numeric Fields���������������������������������������������������������������������������������������������������������������������������������� 14 Storing Images���������������������������������������������������������������������������������������������������������������������������������������������������� 16 Storing Binary Data��������������������������������������������������������������������������������������������������������������������������������������������� 16 Ensuring Unique Values��������������������������������������������������������������������������������������������������������������������������������������� 16 Changing Data Types������������������������������������������������������������������������������������������������������������������������������������������� 16

Using LightSwitch Business Types����������������������������������������������������������������������������������������������16 Storing Email Addresses�������������������������������������������������������������������������������������������������������������������������������������� 17 Storing Money Values������������������������������������������������������������������������������������������������������������������������������������������ 17 Storing Phone Numbers��������������������������������������������������������������������������������������������������������������������������������������� 18 Storing Web Addresses and Percentage Values�������������������������������������������������������������������������������������������������� 19

Examining What Happens in SQL Server�������������������������������������������������������������������������������������19 Creating Choice Lists�������������������������������������������������������������������������������������������������������������������20 Choice Lists vs. Related Tables���������������������������������������������������������������������������������������������������������������������������� 21

Defining Relationships����������������������������������������������������������������������������������������������������������������22 Defining One-to-Many Type Relationships����������������������������������������������������������������������������������������������������������� 22 How Relationships Are Defined in SQL Server����������������������������������������������������������������������������������������������������� 24 Defining Self-Referencing Relationships������������������������������������������������������������������������������������������������������������� 24 Defining Many-to-Many Relationships���������������������������������������������������������������������������������������������������������������� 25 Determining How Child Records Are Deleted������������������������������������������������������������������������������������������������������ 26

Attaching Existing Data���������������������������������������������������������������������������������������������������������������27 Attaching to an Existing Database����������������������������������������������������������������������������������������������������������������������� 27 Attaching to SharePoint Data������������������������������������������������������������������������������������������������������������������������������ 30 Deleting a Data Source���������������������������������������������������������������������������������������������������������������������������������������� 30

Creating Computed Properties����������������������������������������������������������������������������������������������������30 Creating Summary Properties����������������������������������������������������������������������������������������������������������������������������� 31 Calculating Dates with Computed Properties������������������������������������������������������������������������������������������������������ 32

vi

■ Contents

Summarizing Child Records with Computed Properties�������������������������������������������������������������������������������������� 33 Returning Images with Computed Properties������������������������������������������������������������������������������������������������������ 33 Sorting and Filtering by Computed Properties����������������������������������������������������������������������������������������������������� 34

Summary�������������������������������������������������������������������������������������������������������������������������������������35 ■■Chapter 3: Introducing Silverlight Screen Design�����������������������������������������������������������37 Choosing a Screen Template�������������������������������������������������������������������������������������������������������37 Showing a Single Record������������������������������������������������������������������������������������������������������������38 Organizing Your Screen���������������������������������������������������������������������������������������������������������������39 Introducing the Screen Designer Toolbar������������������������������������������������������������������������������������������������������������ 39 Setting Screen Properties������������������������������������������������������������������������������������������������������������������������������������ 40 Grouping and Laying Out Your Screen����������������������������������������������������������������������������������������������������������������� 40 Displaying Static Text and Images����������������������������������������������������������������������������������������������������������������������� 42 Choosing Data Controls��������������������������������������������������������������������������������������������������������������������������������������� 42 Displaying Data Using Data Item Containers������������������������������������������������������������������������������������������������������� 44

Including Related Data Items������������������������������������������������������������������������������������������������������46 Showing Collections of Records��������������������������������������������������������������������������������������������������47 Configuring Data Grid Settings���������������������������������������������������������������������������������������������������������������������������� 48 Setting Data Grid Header Buttons������������������������������������������������������������������������������������������������������������������������ 50

Using Data-Selection Controls����������������������������������������������������������������������������������������������������50 Using the Auto Complete Box Control������������������������������������������������������������������������������������������������������������������ 51 Using the Modal Window Picker Control�������������������������������������������������������������������������������������������������������������� 51

Setting Control Appearances�������������������������������������������������������������������������������������������������������52 Positioning Control Labels����������������������������������������������������������������������������������������������������������������������������������� 53 Styling Labels and Static Text Controls���������������������������������������������������������������������������������������������������������������� 54 Making Controls Read-Only��������������������������������������������������������������������������������������������������������������������������������� 54

Creating a Search Screen������������������������������������������������������������������������������������������������������������55 Launching Child Screens�������������������������������������������������������������������������������������������������������������56 Setting Application Properties�����������������������������������������������������������������������������������������������������57 Changing the Language and Culture Settings����������������������������������������������������������������������������������������������������� 57

Configuring Screen Navigation����������������������������������������������������������������������������������������������������59 vii

■ Contents

Designing Screens at Run Time��������������������������������������������������������������������������������������������������60 Summary�������������������������������������������������������������������������������������������������������������������������������������61 ■■Chapter 4: Accessing Data with Code�����������������������������������������������������������������������������63 Performing Application Tasks in Code�����������������������������������������������������������������������������������������63 Working with Data�����������������������������������������������������������������������������������������������������������������������64 Creating Records������������������������������������������������������������������������������������������������������������������������������������������������� 65 Finding Single Records by ID Value��������������������������������������������������������������������������������������������������������������������� 66 Updating Records������������������������������������������������������������������������������������������������������������������������������������������������ 67 Deleting Records������������������������������������������������������������������������������������������������������������������������������������������������� 68 Working with Properties and Nullable Data Types����������������������������������������������������������������������������������������������� 68

Working with User Details�����������������������������������������������������������������������������������������������������������69 Working with Screen Data�����������������������������������������������������������������������������������������������������������70 Working with Screen Events�������������������������������������������������������������������������������������������������������������������������������� 71

Working with the Save Pipeline���������������������������������������������������������������������������������������������������73 Calling Server-Side Code from the Client������������������������������������������������������������������������������������������������������������ 75

Working with Change Sets����������������������������������������������������������������������������������������������������������75 Retrieving Original Values������������������������������������������������������������������������������������������������������������������������������������ 75 Discarding Items That Have Changed������������������������������������������������������������������������������������������������������������������ 77

Managing Transactions in LightSwitch����������������������������������������������������������������������������������������79 Applying Transactions on Screens����������������������������������������������������������������������������������������������������������������������� 79 Creating Transactions in the Save Pipeline���������������������������������������������������������������������������������������������������������� 80

Understanding Concurrency��������������������������������������������������������������������������������������������������������83 Displaying a Data-Conflict Screen����������������������������������������������������������������������������������������������������������������������� 83 Handling Conflicts in Code����������������������������������������������������������������������������������������������������������������������������������� 85

Summary�������������������������������������������������������������������������������������������������������������������������������������87 ■■Chapter 5: Validating Data�����������������������������������������������������������������������������������������������89 Understanding Validation in LightSwitch�������������������������������������������������������������������������������������89 How Client Validation Works��������������������������������������������������������������������������������������������������������90 How MVVM Applies to Validation������������������������������������������������������������������������������������������������������������������������� 91

Defining Validation Rules�������������������������������������������������������������������������������������������������������������91 viii

■ Contents

Writing Custom Validation�����������������������������������������������������������������������������������������������������������93 Identifying Where Validation Runs����������������������������������������������������������������������������������������������������������������������� 93 Creating Validation Rules and Warnings�������������������������������������������������������������������������������������������������������������� 94

Custom Validation Examples�������������������������������������������������������������������������������������������������������96 Comparing Against Other Properties������������������������������������������������������������������������������������������������������������������� 96 Mandating Data Using Conditions����������������������������������������������������������������������������������������������������������������������� 97 Validating Patterns with Regular Expressions����������������������������������������������������������������������������������������������������� 98 Validating File Sizes������������������������������������������������������������������������������������������������������������������������������������������� 100 Checking Against Child Collections������������������������������������������������������������������������������������������������������������������� 101 Enforcing Uniqueness and Preventing Duplicates��������������������������������������������������������������������������������������������� 102

Performing Screen Validation����������������������������������������������������������������������������������������������������104 Validating Deletions�������������������������������������������������������������������������������������������������������������������105 Validating Deletions on the Server�������������������������������������������������������������������������������������������������������������������� 107

Accessing Validation Results in Code����������������������������������������������������������������������������������������110 Database Validation�������������������������������������������������������������������������������������������������������������������111 Summary�����������������������������������������������������������������������������������������������������������������������������������112 ■■Chapter 6: Querying Your Data��������������������������������������������������������������������������������������113 Introduction to Data Retrieval����������������������������������������������������������������������������������������������������113 LightSwitch Queries Always Return Entities������������������������������������������������������������������������������������������������������ 113

Understanding the Query Pipeline���������������������������������������������������������������������������������������������114 Using LightSwitch’s Default Queries�����������������������������������������������������������������������������������������115 Filtering and Sorting Data���������������������������������������������������������������������������������������������������������116 Creating Queries Based on Entities������������������������������������������������������������������������������������������������������������������� 116 Applying Filters�������������������������������������������������������������������������������������������������������������������������������������������������� 117 Comparing Against Other Fields/Properties������������������������������������������������������������������������������������������������������� 119 Passing Arguments into a Query����������������������������������������������������������������������������������������������������������������������� 119 Filtering by Global Values���������������������������������������������������������������������������������������������������������������������������������� 119

Sorting Data������������������������������������������������������������������������������������������������������������������������������120 Sorting Data Grids��������������������������������������������������������������������������������������������������������������������������������������������� 121 Examining User Setting Files����������������������������������������������������������������������������������������������������������������������������� 122 ix

■ Contents

Modifying Screen Queries���������������������������������������������������������������������������������������������������������122 Writing Queries in Code�������������������������������������������������������������������������������������������������������������123 Understanding LINQ Syntax������������������������������������������������������������������������������������������������������������������������������� 124 Understanding Method (Lambda) Syntax Code������������������������������������������������������������������������������������������������� 124 Using Standard Query Operators����������������������������������������������������������������������������������������������������������������������� 125 Filtering Data with Lambda Expressions����������������������������������������������������������������������������������������������������������� 125 Using the Any and All Operators������������������������������������������������������������������������������������������������������������������������ 126 Performing For Each Logic�������������������������������������������������������������������������������������������������������������������������������� 127 Querying Data Remotely������������������������������������������������������������������������������������������������������������������������������������ 128 Querying Data Locally���������������������������������������������������������������������������������������������������������������������������������������� 129

Performing Advanced Filtering��������������������������������������������������������������������������������������������������131 Filtering by Related Child Items������������������������������������������������������������������������������������������������������������������������� 131 Exists, In������������������������������������������������������������������������������������������������������������������������������������������������������������ 132 Not Exists, Not In����������������������������������������������������������������������������������������������������������������������������������������������� 133 Filtering by Date Elements��������������������������������������������������������������������������������������������������������������������������������� 134 Top N Records���������������������������������������������������������������������������������������������������������������������������������������������������� 135

Creating Global Values���������������������������������������������������������������������������������������������������������������135 Adding Your Global Value to Your LSML File������������������������������������������������������������������������������������������������������ 136 Creating the Global Value Class������������������������������������������������������������������������������������������������������������������������� 137 Using Global Values in Upgraded Projects��������������������������������������������������������������������������������������������������������� 138

Summary�����������������������������������������������������������������������������������������������������������������������������������138 ■■Chapter 7: Mastering Silverlight Screen Design������������������������������������������������������������139 Working with Screen Data���������������������������������������������������������������������������������������������������������139 Displaying Custom Text and Information����������������������������������������������������������������������������������������������������������� 140 Adding Data Controls to a Screen���������������������������������������������������������������������������������������������������������������������� 141 Setting Control Values and Default Values��������������������������������������������������������������������������������������������������������� 142 Setting the Screen Title in Code������������������������������������������������������������������������������������������������������������������������ 143 Creating an Advanced Search Screen��������������������������������������������������������������������������������������������������������������� 144

Managing Screens��������������������������������������������������������������������������������������������������������������������146 Opening Screens from Buttons or Links������������������������������������������������������������������������������������������������������������ 146 Refreshing All Open Screens����������������������������������������������������������������������������������������������������������������������������� 148 x

■ Contents

Passing Arguments into Screens����������������������������������������������������������������������������������������������������������������������� 149 Creating a Continuous New Data Screen����������������������������������������������������������������������������������������������������������� 150

Working with Controls���������������������������������������������������������������������������������������������������������������151 Finding Controls Using FindControl������������������������������������������������������������������������������������������������������������������� 151 Reference the Underlying Silverlight Control����������������������������������������������������������������������������������������������������� 155 Handling Silverlight Control Events������������������������������������������������������������������������������������������������������������������� 156

Custom Examples����������������������������������������������������������������������������������������������������������������������159 Designing an Add/Edit Screen��������������������������������������������������������������������������������������������������������������������������� 159 Customizing Data Grid Dialogs�������������������������������������������������������������������������������������������������������������������������� 161 Nesting Autocomplete Boxes����������������������������������������������������������������������������������������������������������������������������� 164 Bulk Updating Records by Using a Multiselect Grid������������������������������������������������������������������������������������������ 165 Assigning and Unassigning Self-Joined Data���������������������������������������������������������������������������������������������������� 168 Creating Screens to Work with Single Row Tables�������������������������������������������������������������������������������������������� 170

Working with Threads���������������������������������������������������������������������������������������������������������������171 Finding Out Which Thread Your Code Is Executing On��������������������������������������������������������������������������������������� 173 Understanding When to Execute Code on a Different Thread���������������������������������������������������������������������������� 173

Reacting to Data Changes���������������������������������������������������������������������������������������������������������175 Using PropertyChanged on a New Data Screen Template��������������������������������������������������������������������������������� 176 Using PropertyChanged on a Details Screen����������������������������������������������������������������������������������������������������� 178

Working with Files���������������������������������������������������������������������������������������������������������������������180 Uploading Files�������������������������������������������������������������������������������������������������������������������������������������������������� 180 Downloading and Saving Files��������������������������������������������������������������������������������������������������������������������������� 183 Opening Files in Their Application��������������������������������������������������������������������������������������������������������������������� 184

Summary�����������������������������������������������������������������������������������������������������������������������������������186 ■■Chapter 8: Creating HTML Interfaces�����������������������������������������������������������������������������189 Introducing the HTML Client������������������������������������������������������������������������������������������������������189 Setting Up Visual Studio for HTML Development�����������������������������������������������������������������������190 Adding an HTML Client��������������������������������������������������������������������������������������������������������������190 Adding Screens to Your Application�������������������������������������������������������������������������������������������191

xi

■ Contents

Designing Screens��������������������������������������������������������������������������������������������������������������������192 Understanding the Top-Level Screen Layout����������������������������������������������������������������������������������������������������� 193 Using Data List Controls������������������������������������������������������������������������������������������������������������������������������������ 193 Using Data Controls������������������������������������������������������������������������������������������������������������������������������������������� 194 Using Data-Picker Controls�������������������������������������������������������������������������������������������������������������������������������� 195 Setting Screen Properties���������������������������������������������������������������������������������������������������������������������������������� 195

Creating a Navigation Structure������������������������������������������������������������������������������������������������196 Creating a Startup Screen��������������������������������������������������������������������������������������������������������������������������������� 196 Navigating to Different Screens������������������������������������������������������������������������������������������������������������������������� 197

Adding New Records�����������������������������������������������������������������������������������������������������������������198 Using Dialogs����������������������������������������������������������������������������������������������������������������������������199 Creating Child Records in a Dialog�������������������������������������������������������������������������������������������������������������������� 199 Editing Child Records in a Dialog����������������������������������������������������������������������������������������������������������������������� 200

Using Popups����������������������������������������������������������������������������������������������������������������������������201 Using Compact Margins������������������������������������������������������������������������������������������������������������������������������������� 203

Creating a Search Screen����������������������������������������������������������������������������������������������������������204 Filtering the List Control������������������������������������������������������������������������������������������������������������204 Extending Your Application with JavaScript������������������������������������������������������������������������������207 Using the LightSwitch JavaScript API���������������������������������������������������������������������������������������������������������������� 207 Attaching JavaScript Code�������������������������������������������������������������������������������������������������������������������������������� 208 Setting Default Screen Values��������������������������������������������������������������������������������������������������������������������������� 209 Setting the Screen Title������������������������������������������������������������������������������������������������������������������������������������� 209 Hiding Controls�������������������������������������������������������������������������������������������������������������������������������������������������� 210 Accessing Data in Code������������������������������������������������������������������������������������������������������������������������������������� 211 Deleting Records����������������������������������������������������������������������������������������������������������������������������������������������� 211 Setting a Custom Command Icon���������������������������������������������������������������������������������������������������������������������� 214 Accessing Screen Data Collections������������������������������������������������������������������������������������������������������������������� 214 Validating Data�������������������������������������������������������������������������������������������������������������������������������������������������� 216

Adding Custom HTML to Screens����������������������������������������������������������������������������������������������217

xii

■ Contents

Using Custom Controls��������������������������������������������������������������������������������������������������������������218 Showing Data with Custom Controls����������������������������������������������������������������������������������������������������������������� 219 Running Code When Data Changes������������������������������������������������������������������������������������������������������������������� 223 Replacing Default Controls with Custom Controls��������������������������������������������������������������������������������������������� 224

Executing Queries���������������������������������������������������������������������������������������������������������������������226 Working with Documents����������������������������������������������������������������������������������������������������������229 Uploading Files�������������������������������������������������������������������������������������������������������������������������������������������������� 229 Downloading Files��������������������������������������������������������������������������������������������������������������������������������������������� 237

Customizing the Appearance of Your Application����������������������������������������������������������������������242 Applying Themes����������������������������������������������������������������������������������������������������������������������������������������������� 242 Creating Your Own Themes�������������������������������������������������������������������������������������������������������������������������������� 242 Changing the Application Icon and Title������������������������������������������������������������������������������������������������������������ 243

Securing Your Application���������������������������������������������������������������������������������������������������������244 Securing Methods��������������������������������������������������������������������������������������������������������������������������������������������� 244

Summary�����������������������������������������������������������������������������������������������������������������������������������245 ■■Chapter 9: Creating and Using RIA Services������������������������������������������������������������������247 Creating an RIA Services Project�����������������������������������������������������������������������������������������������247 Creating an Entity Class������������������������������������������������������������������������������������������������������������������������������������� 248 Creating a Domain Service Class���������������������������������������������������������������������������������������������������������������������� 250

Retrieving Data��������������������������������������������������������������������������������������������������������������������������252 Retrieving a Connection String from web.config�����������������������������������������������������������������������255 Updating Data����������������������������������������������������������������������������������������������������������������������������256 Calling SQL Stored Procedures�������������������������������������������������������������������������������������������������258 Consuming Your RIA Service�����������������������������������������������������������������������������������������������������261 Summary�����������������������������������������������������������������������������������������������������������������������������������263 ■■Chapter 10: Sharing Data with OData����������������������������������������������������������������������������265 What Is OData?��������������������������������������������������������������������������������������������������������������������������265 Using External Data Sources�����������������������������������������������������������������������������������������������������265 Finding Third-Party Data Sources to Use����������������������������������������������������������������������������������������������������������� 266 Determining Connection Settings���������������������������������������������������������������������������������������������������������������������� 266 xiii

■ Contents

Connecting to an OData Data Source���������������������������������������������������������������������������������������������������������������� 267 Understanding Default Data Source Options����������������������������������������������������������������������������������������������������� 269

Exposing Your LightSwitch Data via OData�������������������������������������������������������������������������������269 Finding Your Application’s End Point URL���������������������������������������������������������������������������������������������������������� 270 Querying OData�������������������������������������������������������������������������������������������������������������������������������������������������� 271 Securing OData Feeds��������������������������������������������������������������������������������������������������������������������������������������� 271

Consuming LightSwitch Data����������������������������������������������������������������������������������������������������272 Connecting to LightSwitch Data from Excel������������������������������������������������������������������������������������������������������ 272 Connecting to LightSwitch Data from .NET������������������������������������������������������������������������������������������������������� 272 Connecting to Use LightSwitch from Other Platforms��������������������������������������������������������������������������������������� 278

Summary�����������������������������������������������������������������������������������������������������������������������������������279 ■■Chapter 11: Creating and Using Custom Controls���������������������������������������������������������281 Using Custom Controls��������������������������������������������������������������������������������������������������������������281 Specifying a Custom Control����������������������������������������������������������������������������������������������������������������������������� 282 Binding Data to Custom Controls����������������������������������������������������������������������������������������������������������������������� 284 Binding Data Collections to Custom Controls���������������������������������������������������������������������������������������������������� 286 Converting Values When Data Binding��������������������������������������������������������������������������������������������������������������� 288

Creating a Custom Silverlight Control���������������������������������������������������������������������������������������291 Understanding Dependency Properties������������������������������������������������������������������������������������������������������������� 292 Creating a New Control and Dependency Property������������������������������������������������������������������������������������������� 293 Binding Dependency Properties to the Data Context����������������������������������������������������������������������������������������� 298 Applying the Duration Control on a Screen�������������������������������������������������������������������������������������������������������� 299 Calling Custom Control Methods via Dependency Properties���������������������������������������������������������������������������� 300

Calling Screen Code from a Custom Control�����������������������������������������������������������������������������303 Summary�����������������������������������������������������������������������������������������������������������������������������������308 ■■Chapter 12: Creating Control Extensions�����������������������������������������������������������������������309 Using LightSwitch Extensions���������������������������������������������������������������������������������������������������309 Installing LightSwitch Extensions���������������������������������������������������������������������������������������������������������������������� 309 Using the Many-to-Many Data Control�������������������������������������������������������������������������������������������������������������� 310

Preparing Your Computer to Develop Extensions����������������������������������������������������������������������312 xiv

■ Contents

Understanding Custom Control Types���������������������������������������������������������������������������������������313 Creating Value Controls�������������������������������������������������������������������������������������������������������������314 Specifying Which Data Types to Support����������������������������������������������������������������������������������������������������������� 316 Supporting the FindControl Method������������������������������������������������������������������������������������������������������������������� 316 Setting the Control Icon������������������������������������������������������������������������������������������������������������������������������������� 318

Running and Deploying Your Extension�������������������������������������������������������������������������������������323 Setting Product/Installation Attributes��������������������������������������������������������������������������������������������������������������� 324

Creating a Detail Control (ComboBox)���������������������������������������������������������������������������������������325 Setting the Control’s Metadata�������������������������������������������������������������������������������������������������������������������������� 326 Finding the Summary Property for an Entity����������������������������������������������������������������������������������������������������� 334 Creating a Helper Class������������������������������������������������������������������������������������������������������������������������������������� 334

Creating Custom Property Editors���������������������������������������������������������������������������������������������341 Customizing Visual Studio’s Property Editor������������������������������������������������������������������������������������������������������ 341 Customizing the Runtime Designer������������������������������������������������������������������������������������������������������������������� 352

Creating a Group Control Extension������������������������������������������������������������������������������������������357 Setting the Visibility of Labels��������������������������������������������������������������������������������������������������������������������������� 359

Creating a Command Control Extension������������������������������������������������������������������������������������360 Summary�����������������������������������������������������������������������������������������������������������������������������������363 ■■Chapter 13: Creating Data and Presentation Extensions�����������������������������������������������365 Creating a Business Type Extension������������������������������������������������������������������������������������������365 Associating Custom Controls with Business Types������������������������������������������������������������������������������������������� 366 Enforcing Validation������������������������������������������������������������������������������������������������������������������������������������������� 367 Creating Custom Property Editor Windows�������������������������������������������������������������������������������������������������������� 374 Using Your Business Type���������������������������������������������������������������������������������������������������������������������������������� 382

Creating a Custom Shell Extension�������������������������������������������������������������������������������������������383 Preparing Your Project��������������������������������������������������������������������������������������������������������������������������������������� 384 Defining the Look of Your Shell�������������������������������������������������������������������������������������������������������������������������� 384 Binding Data to Your Shell��������������������������������������������������������������������������������������������������������������������������������� 388 Displaying Your Application’s Logo�������������������������������������������������������������������������������������������������������������������� 389 Adding Code That Supports our Custom Shell��������������������������������������������������������������������������������������������������� 390 xv

■ Contents

Managing Screens��������������������������������������������������������������������������������������������������������������������������������������������� 408 Persisting User Settings������������������������������������������������������������������������������������������������������������������������������������ 410 Setting the Name and Description��������������������������������������������������������������������������������������������������������������������� 412 Using Your Custom Shell������������������������������������������������������������������������������������������������������������������������������������ 412

Creating a Custom Theme Extension�����������������������������������������������������������������������������������������413 Applying a Different Font����������������������������������������������������������������������������������������������������������������������������������� 413 Setting Different Colors������������������������������������������������������������������������������������������������������������������������������������� 414

Creating a Screen Template Extension��������������������������������������������������������������������������������������416 Setting Template Properties������������������������������������������������������������������������������������������������������������������������������ 416 Defining the Data Source Type�������������������������������������������������������������������������������������������������������������������������� 418 Generating Screen Controls������������������������������������������������������������������������������������������������������������������������������� 418 Generating Screen Code������������������������������������������������������������������������������������������������������������������������������������ 423 Creating More Complex Screen Templates�������������������������������������������������������������������������������������������������������� 425

Creating a Data Source Extension���������������������������������������������������������������������������������������������425 Creating an Entity Class������������������������������������������������������������������������������������������������������������������������������������� 425 Creating the Data Service Class������������������������������������������������������������������������������������������������������������������������ 428 Using the Data Source Extension���������������������������������������������������������������������������������������������������������������������� 433

Summary�����������������������������������������������������������������������������������������������������������������������������������433 ■■Chapter 14: Creating Reports����������������������������������������������������������������������������������������435 Choosing a Reporting Option�����������������������������������������������������������������������������������������������������435 Using ASP.NET to Create Reports����������������������������������������������������������������������������������������������436 Creating an ASP.NET Project������������������������������������������������������������������������������������������������������������������������������ 436 Adding an ASP.NET Page������������������������������������������������������������������������������������������������������������������������������������ 437 Creating Charts�������������������������������������������������������������������������������������������������������������������������������������������������� 441 Securing Reports����������������������������������������������������������������������������������������������������������������������������������������������� 442 Deploying Your ASP.NET Application������������������������������������������������������������������������������������������������������������������ 444

Using Microsoft SQL Server Reporting Services�����������������������������������������������������������������������445 Installing Reporting Services����������������������������������������������������������������������������������������������������������������������������� 445 Creating Reports������������������������������������������������������������������������������������������������������������������������������������������������ 445

xvi

■ Contents

Linking Reports to LightSwitch�������������������������������������������������������������������������������������������������452 Opening Reports in a New Browser Window����������������������������������������������������������������������������������������������������� 452 Displaying Reports Inside a LightSwitch Screen����������������������������������������������������������������������������������������������� 453 Printing Reports������������������������������������������������������������������������������������������������������������������������������������������������� 458 Opening Reports in an HTML Client Application������������������������������������������������������������������������������������������������ 459

Creating Reports with Microsoft Word��������������������������������������������������������������������������������������459 Performing Simple Automation�������������������������������������������������������������������������������������������������������������������������� 459 Performing Mail Merges with Word������������������������������������������������������������������������������������������������������������������� 466

Creating Adobe PDF Documents������������������������������������������������������������������������������������������������475 Using Other Third-Party Solutions���������������������������������������������������������������������������������������������479 Summary�����������������������������������������������������������������������������������������������������������������������������������479 ■■Chapter 15: Sending E-mail�������������������������������������������������������������������������������������������481 Choosing How to Send E-mail���������������������������������������������������������������������������������������������������481 Sending E-mail by Using Server-Side Code������������������������������������������������������������������������������482 Sending E-mail When Data Changes����������������������������������������������������������������������������������������������������������������� 483 Making Your E-mail Code Reusable������������������������������������������������������������������������������������������������������������������� 485 Using Configuration Settings to Save Credentials��������������������������������������������������������������������������������������������� 488 Triggering Server E-mail from an Onscreen Button on the Client��������������������������������������������������������������������� 489 Sending E-mails with File Attachments������������������������������������������������������������������������������������������������������������� 496

Creating Mail in a Pickup Folder�����������������������������������������������������������������������������������������������499 Sending Mail via Outlook by Using COM Automation����������������������������������������������������������������500 Opening the Default Mail Program��������������������������������������������������������������������������������������������504 Using mailto in a Silverlight Client��������������������������������������������������������������������������������������������������������������������� 505 Using mailto in the HTML Client������������������������������������������������������������������������������������������������������������������������ 507

Calling External Resources from LightSwitch Clients���������������������������������������������������������������508 Using AJAX from the HTML Client���������������������������������������������������������������������������������������������������������������������� 509 Calling Web Services from the Silverlight Client����������������������������������������������������������������������������������������������� 512

Summary�����������������������������������������������������������������������������������������������������������������������������������521

xvii

■ Contents

■■Chapter 16: Authenticating Your Users�������������������������������������������������������������������������523 Choosing an Authentication Method������������������������������������������������������������������������������������������523 Disabling Authentication������������������������������������������������������������������������������������������������������������������������������������ 524 Enabling Windows Authentication��������������������������������������������������������������������������������������������������������������������� 525 Enabling Forms Authentication�������������������������������������������������������������������������������������������������������������������������� 526

Understanding Where User Details Are Stored��������������������������������������������������������������������������527 Changing the Password Complexity Rules�������������������������������������������������������������������������������������������������������� 529 Changing Password-Encryption Settings���������������������������������������������������������������������������������������������������������� 530 Sharing Forms Authentication Data with ASP.NET��������������������������������������������������������������������������������������������� 530 Allowing Users to Log Out of Web Applications������������������������������������������������������������������������������������������������� 532

Summary�����������������������������������������������������������������������������������������������������������������������������������535 ■■Chapter 17: Authorizing Your Users������������������������������������������������������������������������������537 Applying Permissions����������������������������������������������������������������������������������������������������������������537 Defining Permissions����������������������������������������������������������������������������������������������������������������539 Setting Up Administrators Who Can Manage Users������������������������������������������������������������������������������������������� 539 Debugging Permissions������������������������������������������������������������������������������������������������������������������������������������� 540 Applying Table-Level Access Control����������������������������������������������������������������������������������������������������������������� 540 Applying Screen-Level Access Control�������������������������������������������������������������������������������������������������������������� 544 Applying Query-Level Permissions�������������������������������������������������������������������������������������������������������������������� 547

Specifying the Application Administrator����������������������������������������������������������������������������������547 Creating and Managing Roles���������������������������������������������������������������������������������������������������548 Custom Authorization Examples������������������������������������������������������������������������������������������������548 Associating Logins with Your Data�������������������������������������������������������������������������������������������������������������������� 548 Opening Screens Conditionally at Login������������������������������������������������������������������������������������������������������������ 552 Restricting Row-Level Access to Data��������������������������������������������������������������������������������������������������������������� 553 Setting Screen Control Properties by Permission���������������������������������������������������������������������������������������������� 554 Allowing Users to Bypass Validation������������������������������������������������������������������������������������������������������������������ 555

Summary�����������������������������������������������������������������������������������������������������������������������������������556

xviii

■ Contents

■■Chapter 18: Deploying Your Application������������������������������������������������������������������������557 Getting Started with Deployment ���������������������������������������������������������������������������������������������557 Choosing an Application Topology��������������������������������������������������������������������������������������������������������������������� 559 Choosing Whether to Publish or Package���������������������������������������������������������������������������������������������������������� 559 Setting Up Your Workstations���������������������������������������������������������������������������������������������������������������������������� 560 Setting Up SQL Server��������������������������������������������������������������������������������������������������������������������������������������� 560

Using the Publish Wizard�����������������������������������������������������������������������������������������������������������560 Data Connections Page�������������������������������������������������������������������������������������������������������������������������������������� 562 Prerequisites Page��������������������������������������������������������������������������������������������������������������������������������������������� 564 Security Page���������������������������������������������������������������������������������������������������������������������������������������������������� 565 Digital Signature/Certificate Page��������������������������������������������������������������������������������������������������������������������� 566

Setting Up Your Web Server������������������������������������������������������������������������������������������������������568 Setting Up IIS on Windows Server 2008/2012��������������������������������������������������������������������������������������������������� 569 Setting Up IIS on Windows 8, 7, or Vista������������������������������������������������������������������������������������������������������������ 570 Setting Up IIS 6 on Windows 2003��������������������������������������������������������������������������������������������������������������������� 571 Setting Up IIS Application Pools ������������������������������������������������������������������������������������������������������������������������ 573 Configuring SQL Server Windows Authentication���������������������������������������������������������������������������������������������� 575 Configuring Secure Sockets Layer (SSL) ���������������������������������������������������������������������������������������������������������� 576 ASP.NET Configuration �������������������������������������������������������������������������������������������������������������������������������������� 578 Installing the LightSwitch Server Components������������������������������������������������������������������������������������������������� 578

Installing the Packages�������������������������������������������������������������������������������������������������������������579 Installing a Two-Tier Desktop Application���������������������������������������������������������������������������������������������������������� 579 Installing a Three-Tier Browser and Desktop Applications�������������������������������������������������������������������������������� 580

Three-Tier Manual Deployment ������������������������������������������������������������������������������������������������583 Deploying Data��������������������������������������������������������������������������������������������������������������������������583 Updating an Application������������������������������������������������������������������������������������������������������������584 Troubleshooting Deployment Errors������������������������������������������������������������������������������������������584 Tracing Your Application ����������������������������������������������������������������������������������������������������������������������������������� 585

xix

■ Contents

Deploying to Azure��������������������������������������������������������������������������������������������������������������������587 Connecting Visual Studio to Azure��������������������������������������������������������������������������������������������������������������������� 587 Choosing an Azure Service Type������������������������������������������������������������������������������������������������������������������������ 589 Creating a Database������������������������������������������������������������������������������������������������������������������������������������������ 591 Completing the Publishing Process������������������������������������������������������������������������������������������������������������������� 592

Publishing to SharePoint�����������������������������������������������������������������������������������������������������������593 Summary�����������������������������������������������������������������������������������������������������������������������������������595 ■■Appendix A: Culture Names�������������������������������������������������������������������������������������������597 ■■Appendix B: Data Type Identifiers���������������������������������������������������������������������������������603 ■■Appendix C: Using Properties in Custom Controls���������������������������������������������������������605 ■■Appendix D: Custom Screen Template View IDs������������������������������������������������������������609 ■■Appendix E: HelpDesk Tables�����������������������������������������������������������������������������������������613 HelpDesk Tables������������������������������������������������������������������������������������������������������������������������613 External Tables��������������������������������������������������������������������������������������������������������������������������624 Database Diagram���������������������������������������������������������������������������������������������������������������������625 Index���������������������������������������������������������������������������������������������������������������������������������627

xx

About the Author Tim Leung is a professional software developer based in England. For the past 15 years, he has specialized in enterprise application development using products from the Microsoft technology stack. In particular, he possesses deep knowledge of Microsoft LightSwitch, the Microsoft .NET Framework and SQL Server. He is an active member of the U.K. developer community and often delivers presentations on technical topics. He is also a chartered member of the British Computer Society. He is passionate about the concept of rapid application development and was awarded the Microsoft Community Contributor Award in 2011 for contributions to the LightSwitch community.

xxi

About the Technical Reviewers Stephen Provine is a principal software design engineer on the Microsoft Visual Studio LightSwitch team. He has 12 years of experience working across many aspects of the Visual Studio rapid-application-development (RAD) experiences, including the Server Explorer, SQL database tooling and overall data designer extensibility in the Visual Studio IDE. Most recently, he has focused on the architecture and design for the LightSwitch HTML client introduced in Visual Studio 2012 Update 2. Prior to working at Microsoft, Stephen received a bachelor’s degree in Computer Science from Harvard University.

Fabio Claudio Ferracchiati is a senior consultant and a senior analyst/developer using Microsoft technologies. He works for Brain Force (www.brainforce.com) in its Italian branch (www.brainforce.it). He is a Microsoft Certified Solution Developer for .NET, a Microsoft Certified Application Developer for .NET, a Microsoft Certified Professional, and a prolific author and technical reviewer. Over the past ten years, he’s written articles for Italian and international magazines and coauthored more than ten books on a variety of computer topics.

xxiii

Acknowledgments I’d like to thank everyone who supported me during the writing of this book. In particular, I’d like to thank my development editor Tom Welsh for all the help he’s given me throughout this project. Stephen Provine spent a lot of time reviewing this book and went out of his way to answer my technical questions. I highly appreciate all the help that he gave me. Beth Massi at Microsoft has helped me countless times in the past, and I’m very grateful for her support. Last but not least, I’d like to thank Katie Sullivan, Ewan Buckingham, and everybody at Apress for making this book possible.

xxv

Foreword Every business uses software to automate their business functions. At their heart, such applications are about gathering, storing, and processing data, so such applications could involve what we typically think of under the rubric of “business applications”—for example, tracking finances or assets (as in ERP software). But it also includes the process of software development, systems management, or anything else involving data. There is a lot of this business software out there, a lot more being written, and even more that people wish they had the time, budget, and skill to write. Building such software involves two concerns: the business problem to be solved (the domain) and the technology into which a solution is rendered. First you have to have one person who understands both the domain and the technology necessary to render it into software, or you have to have a team with a mix of skills. That’s enough to kill many small projects. Assuming you can get the right people together, the technologist then spends a great deal of time on issues that have nothing to do with the business problem being solved, including UI, protocols, business logic mechanism, security, integration with Microsoft Office, and much, much more. One needs a good deal of skill, time, inclination, and budget to get a project accomplished. To help people write business applications faster, we created Microsoft Visual Studio LightSwitch, the simplest way for developers of all skill levels to develop business applications for the desktop and cloud. Using LightSwitch, the core technical problems are solved and a lot of projects—that without LightSwitch would have never seen the light of day—are now in production. Visual Studio LightSwitch 2012 provides a conceptual and practical introduction to many core LightSwitch building blocks, including importing and defining data schema, designing queries and screens, validating data, authenticating and authorizing application users, and deploying the final product. However, the challenge with rapid application development environments is that they’re great at solving the problems they anticipated, but what if you need to do more? Happily, LightSwitch was designed without the glass ceiling that constrains the tools of the 4GL generation, so the full power of Visual Studio is available to you if you want it—the limit is your imagination. Tim has a lot of imagination and has explored many ways to supplement LightSwitch in this book. He offers solutions for a number of problems that LightSwitch does not address but that you may encounter as you write your applications. His is a cookbook. Some recipes won’t have ingredients you like, some you’ll season to fit your taste, some will open possibilities you hadn’t even considered, and some you’ll use as is. A sampling includes sending e-mail, creating reports, and implementing auditing. He shares a passion with the LightSwitch team and with their readers: to build great business applications, fast. Together we can make businesses more successful through software. Steve Anonsen, Distinguished Engineer, Microsoft Visual Studio

xxvii

Introduction It’s possible to see many things in life as a journey, and writing a business application is no exception. On this particular journey, your goal is to build an application that fully works and meets the expectations of your users. Let’s imagine, for a moment, that the tool that builds your application represents your journey’s mode of transport. Using this analogy, I like to think of LightSwitch as a high-speed train because it allows you to reach your destination a lot faster than usual. Speaking of trains, journeys are much more comfortable today than they were in the past. Modern carriages are Wi-Fi enabled and include tables and electrical sockets that you can plug your laptop into. There are even touch-screen displays that you can use to watch television or to view a map that shows you exactly where you are, and how much further there is to go. The ride itself is smooth enough for you to leave your hot coffee on the table without fear of it spilling. Everything is much cleaner than in the age of steam; and you never risk getting a hot cinder in your eye if you open a window! The fascinating thing is that your railway journey might follow exactly the same route as it would have 150 years ago. However, the experience itself is quicker, cleaner, and more convenient. Just like the railways, LightSwitch has evolved during its short lifespan. The improvements in the latest version help to keep it fresh, relevant, and purposeful. When LightSwitch first came out, it created applications based on Microsoft Silverlight. Today, you can support mobile and touch-screen tablet devices by extending your application to include an HTML front end. A big benefit of this approach is that you can reuse all of your existing data and business logic. This technical change is the railway equivalent of swapping all the carriages on a train for modern replacements. Likewise, the first version of LightSwitch employed RIA services as the communication channel between the client and server. Today’s LightSwitch uses the OData protocol instead. Applying the railway analogy once more, this is like replacing the railway tracks with smoother rails. If LightSwitch doesn’t totally meet your requirements, you can easily integrate your application with other systems and platforms—like completing the final leg of the journey by car, bus, or foot. The point of this analogy is to highlight one of the beauties of LightSwitch. It’s possible for LightSwitch to evolve and improve because it’s built in a modular way that allows individual pieces to be replaced. This modular architecture ensures that LightSwitch will remain relevant for a long time to come. You can also consider this book as a journey—a tour that shows you how to build a LightSwitch application from start to finish. As with most journeys, there’ll be highlights and features along the way. I’m really pleased that you’ve chosen to take the LightSwitch journey. It’s sure to save you time and make your life easier; so I’m certain it’ll be a journey that you won’t regret taking!

Who This Book Is For This book is designed for readers who want to build data-driven business applications quickly. You don’t need to have any prior knowledge of LightSwitch, but it helps if you know a little about .NET and database development in general.

xxix

■ Introduction

How This Book Is Organized If learning LightSwitch seems like an enormous task, don’t worry! You might have heard of the Pareto Principle, which also goes by the name of the 80/20 rule. This rule states that in most situations, 20% of something causes 80% of the results. If you apply the Pareto Principle to this book, it suggests that you can accomplish 80% of what you want to accomplish by reading less than four chapters! And, in fact, the first three chapters highlight the key topics that are all you need to build an application that’s almost 80% complete. By the end of Chapter 3, you’ll understand how to create data tables and attach to external data. You’ll know how to build a Silverlight web or desktop application that includes screens and navigation controls. You’ll see how to open child records from a parent record and how to assign parent records to child records by using controls like the autocomplete box (a picker control that’s similar to a drop-down box). In short, you’ll know how to build a working business application with enough functionality to perform most basic tasks. The next part of the book shows you how to write code and queries. After that, you’ll learn how to create screens that contain advanced features and support mobile devices by adding an HTML client. Unless you want to create an application that’s completely isolated from other systems, it’s useful to know how you can attach to unusual data sources and how to share your LightSwitch data with other applications. You’ll discover how to do this through RIA services and OData. You can extend your Silverlight applications through custom controls and extensions, and you’ll find an entire section of the book devoted to this topic. The remaining chapters of the book show you how to build reports, integrate with e-mail systems, and deploy your application.

The Sample Application Most of the chapters include code samples that refer to a HelpDesk application. The purpose of this application is to relate the technical content in the book to a real-life scenario that you can understand. The HelpDesk application is typical of the applications that you can build with LightSwitch. Its features are simple enough to understand, yet complex enough to show off the more advanced features of LightSwitch. It is designed to help companies manage problems, and it’s especially suitable for departments that deal with technical support issues. It allows users to record the actions they carry out to resolve a problem, while giving managers an overview of all current issues. Figure 1 shows a screen from this application and illustrates some of the features that you’ll learn about in this book.

xxx

■ Introduction

Figure 1.  A typical screen from the HelpDesk manager Figure 1 shows the data-entry screen through which users can enter help desk issues. They can set the user that the issue relates to and allocate an engineer to the record. The data-entry screen allows users to respond to the issue, attach documents, register feedback, and record the time that engineers spend on the task. To help you re-create this application, you can find a summary of the application’s tables in Appendix E.

Code Samples LightSwitch supports C#, VB.NET, and JavaScript. This book includes code samples in all three languages. To make life simple, LightSwitch hides much of the complexity that’s associated with application design. When you want to write some code, you do this by clicking on a write code button that LightSwitch shows in its graphical designers. The write code button shows a list of events that you can handle. When you select one of the options in the list, LightSwitch opens a code editor window that you can use to author your code. When you’re starting out with LightSwitch, it isn’t always obvious where your code is stored. To add some clarity, each code listing includes a file location, as shown in Listing 1.

xxxi

■ Introduction

Listing 1.  Hello World Example VB: File: HelpDeskVB\Server\UserCode\ApplicationData.vb Imports System.Text.RegularExpressions 'REM VB Code appears here Dim message = "Hello World"

 

C#: File: HelpDeskCS\Server\UserCode\ApplicationData.cs using System.Text.RegularExpressions; //REM C# Code appears here var message = "Hello World";

 

For both the VB and C# examples in Listing 1, the File heading specifies the file name and path. In this example, HelpDeskVB and HelpDeskCS refer to the name of your LightSwitch application. The name Server refers to the Server project. This piece of information is useful because it informs you that the code runs on the server rather than the client. (You’ll learn all about this in Chapter 1.) The next part of the file heading, UserCode, indicates a subfolder in the Server project. Finally, ApplicationData.vb and ApplicationData.cs refer to the name of the VB or C# file that the listing shows. If a piece of code requires you to reference a .NET namespace, the code listing will show the necessary Imports (VB.NET) or using (C#) statement. Be sure to add these statements to the top of your code file. Many of the code samples include numeric symbols to help you locate specific lines in a code listing. For example, line  defines a comment, whereas line  declares a variable. There’s obviously no need to enter these numeric symbols when you re-create this code for real! As in all books, the length of a code line can exceed the width of this book. In most cases, I’ve put line breaks in places that still allow the code to compile. But in some circumstances, this isn’t always possible. Notable examples include namespace declarations in XAML files and VB.NET keywords like inherits and implements. If your application doesn’t compile, it’s worth checking your code for extraneous line breaks.

Tips/Notes/Cautions This book includes callouts with tips, notes, and cautions. Tips are helpful hints or pieces of information that may be particularly interesting. Notes provide you with nonessential, yet interesting additional information about a particular topic. Cautions alert you to anything that might be detrimental to your work or could cause damage.

Exercises Some readers prefer to learn kinesthetically (that is, in a touchy-feely way). If you fall into this category, you can try the exercises that you’ll find throughout this book. These exercises provide you with ideas to try out in LightSwitch to help you further understand the content that’s described in the book.

xxxii

■ Introduction

Comments and Errata Although my editors and I have tried to be as accurate as possible, mistakes do sometimes happen (particularly with a book of this length). If you have any feedback or want to report any bugs, please visit the official page for this book at the Apress website: http://www.apress.com/9781430250715 This page also shows any mistakes that we’ve found following the publication of this book.

xxxiii