Skip to content

Passing Objects Using JSON.NET

In my current project (which is framework 2.0 based :( ), there is lot of Ajax & one challenge came in my way to pass bulky object having lots of properties from Ajax to BL & then DL.

Method 1

I adopted a method where I created entity class in BL, created an object in javascript having same set of properties & passed the object which got mapped to my BL entity class. But I faced a problem which is, once BL gets its entity filled in from Ajax call, I could not pass the object to DL because DL does not know that entity. One option was to pass entity data as parameters to DL Save method.

[Please open below image in new window(Right click the image-> Open Image in new tab/window) to view it clearly.]

AjaxSaveFlow

Method 2 (Using System.Net.Json)

System.Net.Json.dll is open source assembly available at http://sourceforge.net/projects/csjson/

You can download sample application from below link to understand how we can pass serialized Json object from BL to DL without having to create list of parameter in DL method.

sites.google.com/site/sanjivanipant/Home/JSONExample.zip?attredirects=0&d=1

My application restriction was, I had to find solution for framework 2.0. In recent 4.0 framework, there would be easy ways. You are welcome to share your knowledge if you know better way to deal with JSON data in .NET.

ShareThis

Basic Example of JQuery Template

Recently I started using JQuery template & found it much useful & powerful. I want to depict basic steps to use JQuery template. You can later go through more functions on JQuery website to achieve complex functionality through JQuery template. Here I just want to explain how one can use JQuery template, the needed JQuery references & its structure.

Following sample code displays a dynamic table using JQuery template.


<html>
<head runat="server">
<title></title>
<!-- Step 1-->
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
<script type="text/javascript" src="http://ajax.microsoft.com/ajax/jquery.templates/beta1/jquery.tmpl.min.js"></script>
<!-- Step 3-->
<script id="templateData" type="text/x-jquery-tmpl">
<tr>
<td>  ${ID}  </td>
<td>   ${Description}   </td>
</tr>
</script>

<!-- Step 4-->
<script type="text/javascript" language="javascript" id="ScrId">
$(document).ready(function () {
 var response = [{ ID: "1", Description: "Description 1" },
{ ID: "2", Description: "Description 2"}];
 /* Render the template with the data */
$("#templateData").tmpl(response).appendTo("#tableDataContainer");
 });
</script>
</head>
<body>
<form id="form1" runat="server">
<!-- Step 2-->
<table id="tableDataContainer" border="1">
<tr>
<th>
ID
</th>
<th>
Description
</th>
</tr>
</table>
</form>
</body>
</html>

Ok, lets understand the above code.

<!– Step 1–>
The very first step is to add references to required JQuery files in order to use templates. So, to use templates, we need to have references to jquery.js & jquery.tmpl.js. In the sample code I have used mini version of these files.

<!– Step 2–>
Second step is, defining container for your dynamic data. In sample code, I have created a table with its header row. This will act as a container that means my dynamic rows will get added to this table.

<!– Step 3–>
Step 3 defines the template for dynamic data. It is a skeleton for dynamic data & all the row’s data will get populated in the format defined in the template. Notice type=”text/x-jquery-tmpl”. This script type tells compiler that this script block is a template block. In our template block, we have ${ID} & ${Description} embedded in TR & TD block. ${ID} & ${Description} are nothing but placeholders for data. ID & Descriptions are field names available in data source.

<!– Step 4–>
In Step 4, we are making data for the template & providing it to template block & ultimately to our container table. I have used a static data, but you can make an Ajax call & get the data from server side and provide it to template.
Hope the explanation will be useful.

ShareThis

Getting K2 WorkItem By Folio No

There are many occasions we come across while developing K2 based application where we need to get an instance of K2 WorkItem for further manual manipulation or doing the work through code. We can get an instance of a K2 WorkItem by its folio no. Create WorkItem giving predictable folio then it will be easy to track WorkItems by folio nos in the code.

We need to add reference to SourceCode.Workflow.Client & SourceCode.Workflow.HostAPI libraries.

Image

Code to access K2 WorkItem:

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SourceCode.Workflow.Client;
using SourceCode.Hosting.Client.BaseAPI;
using System.Configuration;
public partial class _Default : System.Web.UI.Page
{
private static string _processName = ConfigurationManager.AppSettings["K2Process"];
private static string _serverName = ConfigurationManager.AppSettings["K2Server"];
private static uint _serverPort = Convert.ToUInt32(ConfigurationManager.AppSettings["K2Port"]);
private static string _user = ConfigurationManager.AppSettings["K2User"];
private static string _domain = ConfigurationManager.AppSettings["K2Domain"];
private static string _password = ConfigurationManager.AppSettings["K2Passoword"];
private static string _userName = ConfigurationManager.AppSettings["UserName"];
protected void Page_Load(object sender, EventArgs e)
{
if (GetK2WorkflowItem("FolioNo") ==null)
Response.Write("Workitem not found");
else
Response.Write("Workitem found");
}
public static WorklistItem GetK2WorkflowItem(string folio)
{
WorklistCriteria criteria = new WorklistCriteria();
criteria.AddFilterField(WCField.ProcessFullName, WCCompare.Equal, _processName);
criteria.AddFilterField(WCField.ProcessFolio, WCCompare.Equal, folio);
using (Connection connection = GetK2Connection())
{
connection.ImpersonateUser(_userName);
Worklist workList = connection.OpenWorklist(criteria);
if (workList.Count == 1)
{
WorklistItem worklistItem = workList[0];
return worklistItem;
}
connection.RevertUser();
}
return null;
}
public static Connection GetK2Connection()
{
SCConnectionStringBuilder connectionString = new SCConnectionStringBuilder();
connectionString.Authenticate = true;
connectionString.Host = _serverName;
connectionString.Integrated = true;
connectionString.IsPrimaryLogin = true;
connectionString.Port = _serverPort;
connectionString.UserID = _user;
connectionString.WindowsDomain = _domain;
connectionString.Password = _password;
connectionString.SecurityLabelName = "K2"; //the default label
//K2 Connection
Connection connection = new Connection();
//open connection to K2 server
connection.Open(_serverName, connectionString.ToString());
return connection;
}
}

ShareThis