LightSwitch – Current User name – Default and Validation

Background

Microsoft’s LightSwitch is marketed as this easy to use tool, but I am finding it as a great easy to confuse tool.

 

Problem Identification

GUI

I have a simple Data Entry GUI that looks like this:

thisfieldisrequired

I am being asked to enter the “added By” name.

Usually I will check on the database’s stored procedure and set it to SYSTEM_USER when empty.

Data Model

BTW, here is the entity..

listofcontact

We can see that “addedBy” is a String and it is Required.

Unfortunately, I am not able to toggle the required flag as it is greyed out.

 

 

Remediation

Entity – properties – Allow Empty String

Property Changes

Here is our first remediation try …

properties-addedby-default

The “Is Required” flag is disabled.  And, so took the easy route of checking the “Allow Empty String” flag.

properties-addedby-modified

And, so now that we are “Allowing Empty String”, I feel good.

Trial

Tried it out and here is where that got me…

GUI

couldnotresolveallowemptystringtoamodelitem

Textual

Failed to load model: Model Error: Could not resolve id ‘:@AllowEmptyString’ to a model item.

 

Solve the error message

Guide

The rest of the afternoon was spent googling for this incredibly useful error message

Failed to load model: Model Error: Could not resolve is ‘:@AllowEmptyString’ to a model item.

Best idea comes through Dave Baker and it is pasted here:

davebaker

 

Here are the steps:

  1. Launch your favorite editor
  2. Identify the lsml file
    • Syntax
      • [Project]\[Project].HTMLClient\Properties\<datasource>\[entity].lsml
    • Sample
      • EDiag\EDiag.HTMLClient\Properties\DBDiagData\listofContacts.lsml
  3. Comment out the DiscardFromCollection element

 

Guide

Faulty

listofcontacts-xml-20161114-0353pm

 

Revised

listofcontacts-xml-20161114-0353-bpm-repaired

 

Entity – properties – Default to single space

Guide

The alternate route is to revisit our entity and take the following steps:

  1. Supporting Work
    1. Unfortunately, Javascript does not provide a user’s name
    2. We have to get on the Server Side
    3. To get it on the Server side we have to tighten things up and require Windows or Forms Authentication
  2.  Entity
    • For the Entity provide code for setting the front-end default value
    • And, if need be the actual backend value

 

Project Properties

From the Solution Explorer, choose the Project, and right click on it

Default Screen

Here is the default setting

projectproperties-accesscontrol-20161114-0449pm

Explanation
  1. The default choice is not to enable authentication

 

Revised Screen

projectproperties-accesscontrol-20161114-0449pm-b

 

Explanation
  1. The Authentication Type is
    • Use Windows authentication
    • We have two choices
      • Allow only users and Active Directory security groups specified in the Users screen of the application
      • Allow any authenticated Windows user
    • We chose to “Allow any authenticated Windows user” as this is just a mock up

 

Entity

  1. Access the HTMLClient Tab
    • Choose “Write Code
    • For the Created Event
      • And, enter a code that supplies a place holder (space) for our property
  2. Access the Server Tab
    • Choose “Write Code
    • In the Inserting Event
      • Check if field is still at default, if so calculate value and overwrite default

HTMLClient

htmlclient-prepare

 

 

Code


/// <reference path="~/GeneratedArtifacts/viewModel.js" />

myapp.listofContact.created = function (entity) {
 // Write code here.

 entity.active = true;
 entity.addedOn = new Date();

 //Add a single space
 var STRING_SPACE = " ";
 entity.addedBy = STRING_SPACE;


};


 

Server

Entity – ListofContact

inserting-initiate-brushed-up

 

Code

partial void listofContacts_Inserting(listofContact entity)
{
//when inserting, set added By to current windows user
//If added By is still at default
//entity.addedBy = entity.username;
if (entity.addedBy == " ")
{

//If user is authenticated, then get username
if (this.Application.User.IsAuthenticated)
{

entity.addedBy = this.Application.User.Name;

} // if (this.Application.User.IsAuthenticated)

} // if (entity.addedBy == " ")

} // listofContacts_Inserting

 

 

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s