locked
HTML Client Screen and/or entity- how do I bring back multiple screen fields at once from a generic handler? RRS feed

  • Question

  • Hi,

    I am using generic handlers, and the 'created' method to bring back defaults to an entity and eventually screen. So far that is working for one field at a time. How do you bring back multiple fields in one call, and parse out on the html screen defaults?

    I am assuming that json serialization is needed but I am getting a circular reference error.

    Here is a mockup of the code  I am using:

    The Generic Handler:

    Public Class GetDefStatAndADInfo
        Implements System.Web.IHttpHandler
    
        Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
            Using serverContext = ServerApplicationContext.CreateContext()
                Dim RequestorID = serverContext.Application.User.Name
                Dim RequestorFirstName As String = ""
                Dim RequestorLastName As String = ""
                'Dim defStatus = serverContext.DataWorkspace.ApplicationData.StatusSet.Where(Function(StatusSet) StatusSet.StatusDescription = "Not Started").FirstOrDefault
                Dim adinfo As ADInformation = serverContext.DataWorkspace.ApplicationData.ADInformations.Where(Function(adinf) adinf.APPUSERNAME = RequestorID).FirstOrDefault
                If RequestorID <> "" AndAlso RequestorID IsNot Nothing Then
                    RequestorFirstName = adinfo.givenName
                    RequestorLastName = adinfo.sn
                    Dim RequestorEmail = adinfo.mail
                    Dim RequestorPhone = adinfo.telephoneNumber
                End If
    
                context.Response.ContentType = "text/plain"
                Dim jsonSerializer = New System.Web.Script.Serialization.JavaScriptSerializer()
                context.Response.Write(jsonSerializer.Serialize(adinfo))
                'context.Response.Write(RequestorFirstName)
            End Using
        End Sub

    I want to bring back in one call to this handler the fields for givenName, sn, mail and telephoneNumber. If I attempt to serialize adinfo as is, i get the circular reference error. If I instead send just the requestor firstname above that works.

    On the lightswitch entity side for html client I have the following code to get the requestor first name by itself ( which works):

     msls.promiseOperation(CallGetUserFirstName).then(function PromiseSuccess(FirstName) {
    
            // Set the result of the CallGetUserName function to the 
    
            // UserName of the entity
    
            entity.RequestorFirstName =FirstName;
        });

    For trying to bring back mutiple fields of adinfo I have:

    msls.promiseOperation(CallGetAdInfo).then(function PromiseSuccess(Result) {
    
            
        
           var objProfile = jQuery.parseJSON(result);
    
           entity.RequestorFirstName = objProfile.givenName;
            entity.RequestorLastName = objProfile.sn;
    
        });

    Three questions:

    1.Am I correct in assuming I need to use json serialization to bring back multiple fields in one call?

    2.IF so, what causes the circular reference error?

    3. Is the above promise object code correct type coding/approach for parsing out the field defaults I am looking for?

    Thanks for any help provided!

    Monday, August 26, 2013 9:30 PM

Answers

  • I think the problem is you are trying to serialize the object that comes out of LightSwitch. In my example I am serializing a special class I created:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    namespace LightSwitchApplication.Web
    {
        [Serializable]
        public class userProfile
        {
            public string UserName { get; set; }
            public string FirstName { get; set; }
            public string LastName { get; set; }
            public string EmailAddress { get; set; }
            public string TimeZone { get; set; }
            public string LightBulbStatus { get; set; }
        }
    
        public class GetProfile : IHttpHandler
        {
            public void ProcessRequest(HttpContext context)
            {
                using (var serverContext = ServerApplicationContext.CreateContext())
                {
                    // Get the current user
                    string strCurrentUserName = serverContext.Application.User.Name;
    
                    // Instantiate userProfile class
                    userProfile objUserProfile = new userProfile();
    
                    // Try to get the Profile
                    objUserProfile = (from User_Profile in serverContext.DataWorkspace.ApplicationData
                                              .UserProfiles.GetQuery().Execute()
                                          where User_Profile.UserName == strCurrentUserName
                                          select new userProfile
                                          {
                                              UserName = User_Profile.UserName,
                                              FirstName = User_Profile.FirstName,
                                              LastName = User_Profile.LastName,
                                              EmailAddress = User_Profile.EmailAddress,
                                              TimeZone = User_Profile.TimeZone
                                          }).FirstOrDefault();
    
                    if (objUserProfile == null) // No Profile found
                    {
                        // Create a Default Profile
                        objUserProfile = new userProfile();
    
                        objUserProfile.UserName = strCurrentUserName;
                        objUserProfile.FirstName = "";
                        objUserProfile.LastName = "";
                        objUserProfile.EmailAddress = "";
                        objUserProfile.TimeZone = TimeZoneInfo.Local.StandardName;
                        objUserProfile.LightBulbStatus = "Off";
                    }
    
                    // Create JavaScriptSerializer
                    System.Web.Script.Serialization.JavaScriptSerializer jsonSerializer =
                       new System.Web.Script.Serialization.JavaScriptSerializer();
    
                   // Output as JSON
                    context.Response.Write(jsonSerializer.Serialize(objUserProfile));
               }
           }
           public bool IsReusable
           {
               get
               {
                   return false;
                }
            }
        }
    }


    Unleash the Power - Get the LightSwitch HTML Client book

    http://LightSwitchHelpWebsite.com


    • Marked as answer by lvsund Tuesday, August 27, 2013 1:28 PM
    • Edited by ADefwebserver Tuesday, August 27, 2013 1:41 PM formatting
    Tuesday, August 27, 2013 4:08 AM

All replies

  • oh and I also have the following code in the entity,.js

    function CallGetUserFirstName(operation) {
    
        $.ajax({
    
            type: 'post',
    
            data: {},
    
            url: '../web/GetDefStatAndADInfo.ashx',
    
            success: operation.code(function AjaxSuccess(AjaxResult) {
    
                operation.complete(AjaxResult);
    
            })
    
        });
    };
    for the firstname file example above
    Monday, August 26, 2013 9:37 PM
  • See:

    Full Control LightSwitch (ServerApplicationContext And Generic File Handlers And Ajax Calls)

    myapp.ProfilePage.created = function (screen) {
    
    
        // Call GetProfileUpdateProfile.ashx .ashx 
    
        // to get the Users Profile
    
        $.ajax({
    
            type: 'post',
    
            data: {},
    
            url: '../web/GetProfile.ashx',
    
            success: function success(result) {
    
    
                // Parse the JSON returned
    
                var objProfile = jQuery.parseJSON(result);
    
    
                // Fill in the values on the Screen
    
                screen.UserName = objProfile.UserName;
    
                screen.FirstName = objProfile.FirstName;
    
                screen.LastName = objProfile.LastName;
    
                screen.EmailAddress = objProfile.EmailAddress;
    
                screen.TimeZone = objProfile.TimeZone;
    
            }
    
        });
    
    };
    


    Unleash the Power - Get the LightSwitch HTML Client book

    http://LightSwitchHelpWebsite.com

    Monday, August 26, 2013 11:46 PM
  • Thanks Michael.

    I did pattern the equivalent  code for my application similar to your example above. Your example makes sense.

    My code seems to hang on the following line in the generic handler file first code sample above:

                Dim jsonSerializer = New System.Web.Script.Serialization.JavaScriptSerializer()
                 -> context.Response.Write(jsonSerializer.Serialize(adinfo))
    

    with the following error:

    Additional information: A circular reference was detected while serializing an object of type 'LightSwitchApplication.ADInformation'.

    googling that error hasn't illuminated much in terms of a solution so far

    That is the part I can't seem to get around.

    When the generic handler is called, my code does recognize the proper values for each of the given name, surname, mail and telephone number. The error seems to occur when that object is serialized.

    Thanks so much for your ongoing effort, time and examples!

    Tuesday, August 27, 2013 2:59 AM
  • I think the problem is you are trying to serialize the object that comes out of LightSwitch. In my example I am serializing a special class I created:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    namespace LightSwitchApplication.Web
    {
        [Serializable]
        public class userProfile
        {
            public string UserName { get; set; }
            public string FirstName { get; set; }
            public string LastName { get; set; }
            public string EmailAddress { get; set; }
            public string TimeZone { get; set; }
            public string LightBulbStatus { get; set; }
        }
    
        public class GetProfile : IHttpHandler
        {
            public void ProcessRequest(HttpContext context)
            {
                using (var serverContext = ServerApplicationContext.CreateContext())
                {
                    // Get the current user
                    string strCurrentUserName = serverContext.Application.User.Name;
    
                    // Instantiate userProfile class
                    userProfile objUserProfile = new userProfile();
    
                    // Try to get the Profile
                    objUserProfile = (from User_Profile in serverContext.DataWorkspace.ApplicationData
                                              .UserProfiles.GetQuery().Execute()
                                          where User_Profile.UserName == strCurrentUserName
                                          select new userProfile
                                          {
                                              UserName = User_Profile.UserName,
                                              FirstName = User_Profile.FirstName,
                                              LastName = User_Profile.LastName,
                                              EmailAddress = User_Profile.EmailAddress,
                                              TimeZone = User_Profile.TimeZone
                                          }).FirstOrDefault();
    
                    if (objUserProfile == null) // No Profile found
                    {
                        // Create a Default Profile
                        objUserProfile = new userProfile();
    
                        objUserProfile.UserName = strCurrentUserName;
                        objUserProfile.FirstName = "";
                        objUserProfile.LastName = "";
                        objUserProfile.EmailAddress = "";
                        objUserProfile.TimeZone = TimeZoneInfo.Local.StandardName;
                        objUserProfile.LightBulbStatus = "Off";
                    }
    
                    // Create JavaScriptSerializer
                    System.Web.Script.Serialization.JavaScriptSerializer jsonSerializer =
                       new System.Web.Script.Serialization.JavaScriptSerializer();
    
                   // Output as JSON
                    context.Response.Write(jsonSerializer.Serialize(objUserProfile));
               }
           }
           public bool IsReusable
           {
               get
               {
                   return false;
                }
            }
        }
    }


    Unleash the Power - Get the LightSwitch HTML Client book

    http://LightSwitchHelpWebsite.com


    • Marked as answer by lvsund Tuesday, August 27, 2013 1:28 PM
    • Edited by ADefwebserver Tuesday, August 27, 2013 1:41 PM formatting
    Tuesday, August 27, 2013 4:08 AM
  • ah! -ok

    I will need to relook at how I am coding in the generic handler file then.

    I will mark as answer.

    Thanks

    Tuesday, August 27, 2013 1:28 PM
  • That was it Michael.

    I added the code for serializing a class and it worked!

    Tuesday, August 27, 2013 2:46 PM
  • That was it Michael.

    I added the code for serializing a class and it worked!

    Cool :)

    There may be a way to just use the LightSwitch collection but I have no examples :(


    Unleash the Power - Get the LightSwitch HTML Client book

    http://LightSwitchHelpWebsite.com

    Tuesday, August 27, 2013 3:34 PM