We need to do this to protect the OData service points. Only setting this on the client side code (shown later) is not enough. A user could access the OData service point directly and alter the value. Using the code above prevents this.
However, the UserName is not populated when we run the application.
To fix this, we first switch to File View.
We add a page to the Server project using the following code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace LightSwitchApplication.Web
{
public class GetUserName : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
using (var serverContext = ServerApplicationContext.CreateContext())
{
context.Response.ContentType = "text/plain";
context.Response.Write(serverContext.Application.User.Name);
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
We switch back to Logical View, select the Entity, Client (tab), Write Code, and then the created method.
We add the following code:
myapp.PromiseOrders.created = function (entity) {
// Set the default date for the Order
entity.OrderDate = new Date();
// Using a Promise object we can call the CallGetUserName function
msls.promiseOperation(CallGetUserName).then(function PromiseSuccess(PromiseResult) {
// Set the result of the CallGetUserName function to the
// UserName of the entity
entity.UserName = PromiseResult;
});
};
// This function will be wrapped in a Promise object
function CallGetUserName(operation) {
$.ajax({
type: 'post',
data: {},
url: '../web/GetUserName.ashx',
success: operation.code(function AjaxSuccess(AjaxResult) {
operation.complete(AjaxResult);
})
});
}
The UserName is now retrieved.
Now how to select current user in Lightswitch HTMLClient dropdown?
change the following code with the above code
msls.promiseOperation(CallGetUserName).then(function
PromiseSuccess(PromiseResult) {
myapp.activeDataWorkspace.ApplicationData.SalePersonMasters_SingleOrDefault(PromiseResult).execute().then(function (result) {
entity.SalePersonMaster =
result.results[0];
});
});
};
function CallGetUserName(operation) {
$.ajax({
type: 'post',
date: {},
url: '../web/GetUserName.ashx',
success: operation.code(function
AjaxSuccess(AjaxResult) {
operation.complete(AjaxResult);
})
});
}
Current Sales Person is selected in the sales person drop down.
Whats new in Visual Studio 2013 for lightswitch Applications?