Friday, 27 September 2013

Select current user in Lightswitch HTMLClient dropdown?

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? 
 

 

Sunday, 22 September 2013

JsonModelGenerator - Model loading failed

I have a VS2012 Update 3 Lightswitch project with a Silverlight and HTML client. It had been working fine for several months but now I get the latest version form the TFS and I'm getting a "JsonModelGenerator - Sequence contains more than one matching element" in file Microsoft.LightSwitch.Build.Tasks.targets line 208 column 5 where project is the HTMLClient.I am stuck with this error. But After a long search I have solved this solutions.

How to solve this error?

Please follow the following steps.

1. Open your lightswitch HtmlClient Project in visual studio.
2. Create a new Brows screen with selecting any table.
3. Now try to run your application. Your application will works fine.
4. Now remove the newly added screen from your application and try to run this. It will works fine.

I have resolved this error as given above. Please response is it working for you or not?

Thursday, 12 September 2013

Error while Updating project from LightSwitch 2011 to LightSwitch 2012

I have faced an error when I was updating my lightswitch 2011 project to latest version lightswitch 2012. Whenever I was trying to run project in Form Authentication mode then every time project is not loading. It went to in loading  stage and no display appears. When we upgrade our project then one file can be miss from our project. Copy that file and paste at desired location. Now Project works fine.

Steps 

1. Open Bin\Debug\Web\Manifests folder in your project. you will see that Microsoft.LightSwitch.Design.Server.Manifest.dll file is miss.


2. Error in the running project is like.


3. One dll file miss. You have to download or copy and past that file at Bin\Debug\Web\Manifests location. 
     file name: Microsoft.LightSwitch.Design.Server.Manifest.dll


Click here to download missing file. 

Thanks to Ankur Rana